<?php namespace Webien\Elementor;

use function Mtphr\PostDuplicator\get_option_value;

class Setup
{
    public function init()
    {
        // Elementor widgets & extensions
        if (!defined('WP_INSTALLING') && did_action('elementor/loaded') && class_exists('acf')) {
            $this->register_scripts();
            $this->register_widgets();
            $this->register_extensions();

            // Register Dynamic tags
            add_action( 'elementor/dynamic_tags/register_tags', [$this, 'register_dynamic_tags'] );
        }


        // If in elementor editing mode, hide legacy widgets if option is set
        if (is_admin() && isset($_GET['action']) && $_GET['action'] == 'elementor') {
            $hide_legacy = filter_var(get_option('options_webien_hide_all_widgets'), FILTER_VALIDATE_BOOLEAN);
            if ($hide_legacy) {
                // Add js to hide by widget title, since widget class is not available in editor
                // Echo in footer
                add_action('elementor/editor/after_enqueue_scripts', function (){
                 ?>
                    <script>
                    let webienWidgets = ['Webien breadcrumbs', 'Webien tabbar', 'Webien videos', 'Webien TOC', 'Reaktiv lista'];

                    window.addEventListener('elementor/init', function () {
                            runYourLogic();
                            setTimeout(function () {
                                runYourLogic();
                            }, 1000);
                            setTimeout(function () {
                                runYourLogic();
                            }, 5000);
                            setTimeout(function () {
                                runYourLogic();
                            }, 10000);
                            setTimeout(function () {
                                runYourLogic();
                            }, 30000);
                        });
                        function runYourLogic() {
                            const container = document.querySelector('#elementor-panel-category-webien');

                            if (!container) return;

                            const titles = container.querySelectorAll('.title-wrapper .title');

                            titles.forEach(function (el) {
                                const text = el.textContent.trim();

                                if (webienWidgets.includes(text)) {
                                    const wrapper = el.closest('.elementor-element-wrapper');
                                    if (wrapper) {
                                        wrapper.style.display = 'none';
                                    }
                                }
                            });
                        }
                    </script>
                    <?php
                });
            }
        }



    }

    function register_scripts()
    {

        $ver = WEBIEN_PLUGIN_VERSION;
        // Register scripts here and include only when needed later with script_depend function in widget file
        wp_register_script( 'webien-tabs', plugin_dir_url(__FILE__) .'Widgets/Tabs/js/script.js', [ 'elementor-frontend' ], $ver, true );
        wp_register_script( 'webien-videos', plugin_dir_url(__FILE__) .'Widgets/Videos/js/script.js', [ 'elementor-frontend' ], $ver, true );

        // TOC Widget
        wp_register_style('webien-toc',  WEBIEN_PLUGIN_URL . 'dist/widget-toc.css', [], $ver);
        wp_register_script('webien-toc-waypoints',  WEBIEN_PLUGIN_URL . 'dist/noframework.waypoints.min.js', [], $ver, true);
        wp_register_script('webien-toc-waypoints-sticky',  WEBIEN_PLUGIN_URL . 'dist/sticky.min.js', ['webien-toc-waypoints'], $ver, true);
        wp_register_script('webien-toc',  WEBIEN_PLUGIN_URL . 'dist/widget-toc.js', ['webien-toc-waypoints','webien-toc-waypoints-sticky'], $ver, true);
        wp_register_script('webien-reactive-list',  WEBIEN_PLUGIN_URL . 'dist/reactive-list.js', [], $ver, true);

    }


    function register_widgets()
    {
        \Elementor\Plugin::instance()
            ->widgets_manager
            ->register(new \Webien\Elementor\Widgets\Breadcrumbs\Widget());

        \Elementor\Plugin::instance()
            ->widgets_manager
            ->register(new \Webien\Elementor\Widgets\Tabs\Widget());

        \Elementor\Plugin::instance()
            ->widgets_manager
            ->register(new \Webien\Elementor\Widgets\Videos\Widget());

        \Elementor\Plugin::instance()
            ->widgets_manager
            ->register(new \Webien\Elementor\Widgets\TOC\Widget());

        \Elementor\Plugin::instance()
            ->widgets_manager
            ->register(new \Webien\Elementor\Widgets\ReactiveList\Widget());

    }


    function register_extensions()
    {

        \Webien\Elementor\Extensions\TextEditor::init();
        (new \Webien\Elementor\Extensions\Animations\Animations());
        (new \Webien\Elementor\Extensions\ColumnSlider\ColumnSlider());
        //SectionLink generates notices in elementor editor, so make it optional
        if (!defined('WEBIEN_ENABLE_SECTION_LINK') || (defined('WEBIEN_ENABLE_SECTION_LINK') && WEBIEN_ENABLE_SECTION_LINK)) {
            (new \Webien\Elementor\Extensions\SectionLinks());
        }
        (new \Webien\Elementor\Extensions\ColumnWidth());
        (new \Webien\Elementor\Extensions\ContainerPadding());
        (new \Webien\Elementor\Extensions\Buttons\Buttons());
        (new \Webien\Elementor\Extensions\Videos());

    }

    function register_dynamic_tags($dynamic_tags)
    {
        // In our Dynamic Tag we use a group named request-variables so we need
        // To register that group as well before the tag
        \Elementor\Plugin::$instance->dynamic_tags->register_group( 'webien_tags', [
            'title' => 'Webien'
        ] );

        // Include the Dynamic tag class file
        include_once( WEBIEN_PLUGIN_PATH.'/src/Elementor/DynamicTags/WebienTagsSelectedPosts.php' );

        // Finally register the tag
        $dynamic_tags->register_tag( 'WebienTagsSelectedPosts' );
    }


}
