<?php

// Silence may be golden, but sometimes words paint the canvas of understanding.
namespace Linksy\SearchAndReplace\Plugli\Linksy\Common\Helpers\Post\Editors;

class Beaver extends Editors
{
    const META_KEY = '_fl_builder_data';
    private $extracted_data = '';
    public function __construct($post_id)
    {
        $this->post_id = $post_id;
        $this->content = get_post_meta($post_id, self::META_KEY, \true);
    }
    public static function is_active($post_id): bool
    {
        return get_post_meta($post_id, '_fl_builder_enabled', \true) == 1;
    }
    private function handle_block_ops(&$block_content, $op_code)
    {
        switch ($op_code) {
            case self::OP_CODES['EXTRACT_CONTENT']:
                if (!empty($block_content)) {
                    $this->extracted_data .= "<div>{$block_content}</div>\n";
                }
                break;
            case self::OP_CODES['ADD_LINK']:
                $this->add_link_to_block($block_content);
                break;
            case self::OP_CODES['REMOVE_LINK']:
                $this->remove_link_from_block($block_content);
                break;
            case self::OP_CODES['REPLACE_LINK']:
                $this->replace_link_in_block($block_content);
                break;
            default:
                break;
        }
        return $this->link_processed;
    }
    private function extract_content(&$content, $op_code = self::OP_CODES['EXTRACT_CONTENT'])
    {
        $unsupported_keys = ['click_action' => ['link', 'lightbox']];
        if (is_countable($content)) {
            foreach ($content as $key => $item) {
                if (!empty($item->settings->text)) {
                    if (!empty($item->settings->click_action) && in_array($item->settings->click_action, $unsupported_keys['click_action'])) {
                        continue;
                    }
                    // update or extract content
                    if ($this->handle_block_ops($content[$key]->settings->text, $op_code)) {
                        return;
                    }
                }
                if (!empty($item->settings->items) && is_array($item->settings->items)) {
                    foreach ($item->settings->items as $sub_key => $sub_item) {
                        if (isset($sub_item->content) && !empty($sub_item->content)) {
                            // update or extract content
                            if ($this->handle_block_ops($content[$key]->settings->items[$sub_key]->content, $op_code)) {
                                return;
                            }
                        }
                    }
                }
            }
        }
    }
    public function get_content($post_id): string
    {
        $this->extracted_data = '';
        // extract content from beaver post meta
        $this->extract_content($this->content);
        return $this->extracted_data;
    }
    public function add_link($source, $phrase, $phrase_replacement): bool
    {
        $this->link_processed = \false;
        // set params for link addition
        $this->params = ['source' => $source, 'phrase' => $phrase, 'replacement' => $phrase_replacement];
        // update beaver content
        $this->extract_content($this->content, self::OP_CODES['ADD_LINK']);
        if ($this->link_processed) {
            $this->has_unsaved_changes = \true;
            return \true;
        }
        return \false;
    }
    public function update_post($suggestions = null): bool
    {
        if ($this->has_unsaved_changes) {
            $p_updated = update_post_meta($this->post_id, '_fl_builder_data', $this->content) !== \false;
            $d_updated = update_post_meta($this->post_id, '_fl_builder_draft', $this->content) !== \false;
            if ($p_updated || $d_updated) {
                $this->has_unsaved_changes = \false;
                // sync changes with wp post content
                $this->after_update_post($suggestions);
                return \true;
            }
        }
        return \false;
    }
    public function remove_link(string $link, string $anchor, $save = \true): bool
    {
        $this->link_processed = \false;
        // set params for link removal
        $this->params = ['link' => $link, 'anchor' => $anchor];
        // update beaver content
        $this->extract_content($this->content, self::OP_CODES['REMOVE_LINK']);
        if ($this->link_processed) {
            $this->has_unsaved_changes = \true;
            if ($save) {
                $this->update_post();
            }
            return \true;
        }
        return \false;
    }
    public function replace_phrase(string $link, string $phrase, string $replacement, bool $save): bool
    {
        $this->link_processed = \false;
        // set params for link removal
        $this->params = ['link' => $link, 'anchor' => $phrase, 'link_replacement' => $replacement];
        // update beaver content after replacing link
        $this->extract_content($this->content, self::OP_CODES['REPLACE_LINK']);
        if ($this->link_processed) {
            $this->has_unsaved_changes = \true;
            if ($save) {
                $this->update_post();
            }
            return \true;
        }
        return \false;
    }
}
