<?php

class Pakrco_Merlin_Config {

    private $wizard;

    public function __construct() {
        $this->init();
        add_action('merlin_import_files', [$this, 'import_files']);
        add_action('merlin_after_all_import', [$this, 'after_import_setup'], 10, 1);
        add_filter('merlin_generate_child_functions_php', [$this, 'render_child_functions_php']);

        add_action('import_start', function () {
            add_filter('wxr_importer.pre_process.post_meta', [$this, 'fiximport_elementor'], 10, 1);
        });

        // Setup HF
        if (function_exists('hfe_header_enabled')) {
            add_filter('hfe_settings_tabs', [$this, 'setup_hf_theme']);
            add_action('admin_menu', [$this, 'register_setuphf_page']);
            add_action('admin_post_custom_setup_hf', [$this, 'action_setup_hf']);
        }

        // Custom
        add_filter('merlin_get_base_content', [$this, 'setup_melin_content'], 10, 2);
        add_filter('pakrco_custom_import_data_info', [$this, 'get_custom_import_data_info']);
        add_filter('pakrco_get_import_steps_text', [$this, 'get_import_steps_text']);

    }

    public function get_import_steps_text($list) {
        $list['all-page'] = esc_html__('Pages + Menu', 'pakrco');
        return $list;
    }

	public function get_custom_import_data_info( $import_data ) {
		$import_data = array(
			'content'       => false,
			'all-page'      => false,
			'homepage'      => false,
			'blog'          => true,
			'woocommerce'   => false,
			'header-footer' => true,
			'widgets'       => false,
			'options'       => false,
			'sliders'       => false,
			'redux'         => false,
			'after_import'  => false,
		);

		return $import_data;
	}

    public function setup_melin_content($content, $merlin) {
        $content['header-footer'] = array(
            'title'            => esc_html__('Header Footer Builder', 'pakrco'),
            'description'      => esc_html__('Install hf.xml', 'pakrco'),
            'pending'          => esc_html__('Pending', 'pakrco'),
            'installing'       => esc_html__('Installing', 'pakrco'),
            'success'          => esc_html__('Success', 'pakrco'),
            'install_callback' => array($merlin->importer, 'import'),
            'checked'          => $merlin->is_possible_upgrade() ? 0 : 1,
            'data'             => get_theme_file_path('dummy-data/hf.xml'),
        );

        $content['blog'] = array(
            'title'            => esc_html__('Blog Content', 'pakrco'),
            'description'      => esc_html__('Install woocommerce.xml', 'pakrco'),
            'pending'          => esc_html__('Pending', 'pakrco'),
            'installing'       => esc_html__('Installing', 'pakrco'),
            'success'          => esc_html__('Success', 'pakrco'),
            'install_callback' => array($merlin->importer, 'import'),
            'checked'          => $merlin->is_possible_upgrade() ? 0 : 1,
            'data'             => get_theme_file_path('dummy-data/blog.xml'),
        );

        return $content;
    }

    public function import_files(){
            return array(
            array(
                'import_file_name'           => 'home 1',
                'home'                       => 'home-1',
                'local_import_file'          => get_theme_file_path('/dummy-data/content.xml'),
                'homepage'                   => get_theme_file_path('/dummy-data/homepage/home-1.xml'),
                'local_import_widget_file'   => get_theme_file_path('/dummy-data/widgets.json'),
                'import_preview_image_url'   => get_theme_file_uri('/assets/images/oneclick/home_1.jpg'),
            ),

            array(
                'import_file_name'           => 'home 10',
                'home'                       => 'home-10',
                'local_import_file'          => get_theme_file_path('/dummy-data/content.xml'),
                'homepage'                   => get_theme_file_path('/dummy-data/homepage/home-10.xml'),
                'local_import_widget_file'   => get_theme_file_path('/dummy-data/widgets.json'),
                'import_preview_image_url'   => get_theme_file_uri('/assets/images/oneclick/home_10.jpg'),
            ),

            array(
                'import_file_name'           => 'home 2',
                'home'                       => 'home-2',
                'local_import_file'          => get_theme_file_path('/dummy-data/content.xml'),
                'homepage'                   => get_theme_file_path('/dummy-data/homepage/home-2.xml'),
                'local_import_widget_file'   => get_theme_file_path('/dummy-data/widgets.json'),
                'import_preview_image_url'   => get_theme_file_uri('/assets/images/oneclick/home_2.jpg'),
            ),

            array(
                'import_file_name'           => 'home 3',
                'home'                       => 'home-3',
                'local_import_file'          => get_theme_file_path('/dummy-data/content.xml'),
                'homepage'                   => get_theme_file_path('/dummy-data/homepage/home-3.xml'),
                'local_import_widget_file'   => get_theme_file_path('/dummy-data/widgets.json'),
                'import_preview_image_url'   => get_theme_file_uri('/assets/images/oneclick/home_3.jpg'),
            ),

            array(
                'import_file_name'           => 'home 4',
                'home'                       => 'home-4',
                'local_import_file'          => get_theme_file_path('/dummy-data/content.xml'),
                'homepage'                   => get_theme_file_path('/dummy-data/homepage/home-4.xml'),
                'local_import_widget_file'   => get_theme_file_path('/dummy-data/widgets.json'),
                'import_preview_image_url'   => get_theme_file_uri('/assets/images/oneclick/home_4.jpg'),
            ),

            array(
                'import_file_name'           => 'home 5',
                'home'                       => 'home-5',
                'local_import_file'          => get_theme_file_path('/dummy-data/content.xml'),
                'homepage'                   => get_theme_file_path('/dummy-data/homepage/home-5.xml'),
                'local_import_widget_file'   => get_theme_file_path('/dummy-data/widgets.json'),
                'import_preview_image_url'   => get_theme_file_uri('/assets/images/oneclick/home_5.jpg'),
            ),

            array(
                'import_file_name'           => 'home 6',
                'home'                       => 'home-6',
                'local_import_file'          => get_theme_file_path('/dummy-data/content.xml'),
                'homepage'                   => get_theme_file_path('/dummy-data/homepage/home-6.xml'),
                'local_import_widget_file'   => get_theme_file_path('/dummy-data/widgets.json'),
                'import_preview_image_url'   => get_theme_file_uri('/assets/images/oneclick/home_6.jpg'),
            ),

            array(
                'import_file_name'           => 'home 7',
                'home'                       => 'home-7',
                'local_import_file'          => get_theme_file_path('/dummy-data/content.xml'),
                'homepage'                   => get_theme_file_path('/dummy-data/homepage/home-7.xml'),
                'local_import_widget_file'   => get_theme_file_path('/dummy-data/widgets.json'),
                'import_preview_image_url'   => get_theme_file_uri('/assets/images/oneclick/home_7.jpg'),
            ),

            array(
                'import_file_name'           => 'home 8',
                'home'                       => 'home-8',
                'local_import_file'          => get_theme_file_path('/dummy-data/content.xml'),
                'homepage'                   => get_theme_file_path('/dummy-data/homepage/home-8.xml'),
                'local_import_widget_file'   => get_theme_file_path('/dummy-data/widgets.json'),
                'import_preview_image_url'   => get_theme_file_uri('/assets/images/oneclick/home_8.jpg'),
            ),

            array(
                'import_file_name'           => 'home 9',
                'home'                       => 'home-9',
                'local_import_file'          => get_theme_file_path('/dummy-data/content.xml'),
                'homepage'                   => get_theme_file_path('/dummy-data/homepage/home-9.xml'),
                'local_import_widget_file'   => get_theme_file_path('/dummy-data/widgets.json'),
                'import_preview_image_url'   => get_theme_file_uri('/assets/images/oneclick/home_9.jpg'),
            ),
            );           
        }

    public function after_import_setup($selected_import) {

        $selected_import = ($this->import_files())[$selected_import];
        $check_oneclick  = get_option('pakrco_check_oneclick', []);

        $this->set_demo_menus();

        // setup Home page
        $home = get_page_by_path($selected_import['home']);
        if ($home) {
            update_option('show_on_front', 'page');
            update_option('page_on_front', $home->ID);
        }

        // Header Footer Builder
        $this->reset_header_footer();
        $this->set_hf($selected_import['home']);
        $options = $this->get_all_options();
        // Elementor
        if (!isset($check_oneclick['elementor-options'])) {
	        update_option('elementor_load_fa4_shim', 'yes');
	        update_option('elementor_experiment-e_dom_optimization', 'active');
	        update_option('elementor_experiment-e_optimized_assets_loading', 'active');
	        update_option('elementor_experiment-elementor_experiment-a11y_improvements', 'active');
	        update_option('elementor_experiment-additional_custom_breakpoints', 'active');

            $active_kit_id = Elementor\Plugin::$instance->kits_manager->get_active_id();
            update_post_meta($active_kit_id, '_elementor_page_settings', $options['elementor']);
            $check_oneclick['elementor-options'] = true;
        }

        // Options
        if (!isset($check_oneclick['theme-options'])) {
            foreach ($options['options'] as $key => $option) {
                update_option($key, $option);
            }
            $check_oneclick['theme-options'] = true;
        }

        //Mailchimp
        if (!isset($check_oneclick['mailchip'])) {
            $mailchimp = $this->get_mailchimp_id();
            if ($mailchimp) {
                update_option('mc4wp_default_form_id', $mailchimp);
                update_option('test_mailchimp', $mailchimp);
                $check_oneclick['mailchip'] = true;
            }
        }
        if (!isset($check_oneclick['logo'])) {
            set_theme_mod('custom_logo', $this->get_attachment('_logo'));
            $check_oneclick['logo'] = true;
        }
        update_option('pakrco_check_oneclick', $check_oneclick);
    }

    private function get_mailchimp_id() {
        $params = array(
            'post_type'      => 'mc4wp-form',
            'posts_per_page' => 1,
        );
        $post   = get_posts($params);

        return isset($post[0]) ? $post[0]->ID : 0;
    }

    private function get_attachment($key) {
        $params = array(
            'post_type'      => 'attachment',
            'post_status'    => 'inherit',
            'posts_per_page' => 1,
            'meta_key'       => $key,
        );
        $post   = get_posts($params);
        if ($post) {
            return $post[0]->ID;
        }
        return 0;
    }

    private function init() {
        $this->wizard = new Merlin(
            $config = array(
                // Location / directory where Merlin WP is placed in your theme.
                'merlin_url'         => 'merlin',
                // The wp-admin page slug where Merlin WP loads.
                'parent_slug'        => 'themes.php',
                // The wp-admin parent page slug for the admin menu item.
                'capability'         => 'manage_options',
                // The capability required for this menu to be displayed to the user.
                'dev_mode'           => true,
                // Enable development mode for testing.
                'license_step'       => false,
                // EDD license activation step.
                'license_required'   => false,
                // Require the license activation step.
                'license_help_url'   => '',
                'directory'          => '/includes/merlin',
                // URL for the 'license-tooltip'.
                'edd_remote_api_url' => '',
                // EDD_Theme_Updater_Admin remote_api_url.
                'edd_item_name'      => '',
                // EDD_Theme_Updater_Admin item_name.
                'edd_theme_slug'     => '',
                // EDD_Theme_Updater_Admin item_slug.
            ),
            $strings = array(
                'admin-menu'          => esc_html__('Theme Setup', 'pakrco'),

                /* translators: 1: Title Tag 2: Theme Name 3: Closing Title Tag */
                'title%s%s%s%s'       => esc_html__('%1$s%2$s Themes &lsaquo; Theme Setup: %3$s%4$s', 'pakrco'),
                'return-to-dashboard' => esc_html__('Return to the dashboard', 'pakrco'),
                'ignore'              => esc_html__('Disable this wizard', 'pakrco'),

                'btn-skip'                 => esc_html__('Skip', 'pakrco'),
                'btn-next'                 => esc_html__('Next', 'pakrco'),
                'btn-start'                => esc_html__('Start', 'pakrco'),
                'btn-no'                   => esc_html__('Cancel', 'pakrco'),
                'btn-plugins-install'      => esc_html__('Install', 'pakrco'),
                'btn-child-install'        => esc_html__('Install', 'pakrco'),
                'btn-content-install'      => esc_html__('Install', 'pakrco'),
                'btn-import'               => esc_html__('Import', 'pakrco'),
                'btn-license-activate'     => esc_html__('Activate', 'pakrco'),
                'btn-license-skip'         => esc_html__('Later', 'pakrco'),

                /* translators: Theme Name */
                'license-header%s'         => esc_html__('Activate %s', 'pakrco'),
                /* translators: Theme Name */
                'license-header-success%s' => esc_html__('%s is Activated', 'pakrco'),
                /* translators: Theme Name */
                'license%s'                => esc_html__('Enter your license key to enable remote updates and theme support.', 'pakrco'),
                'license-label'            => esc_html__('License key', 'pakrco'),
                'license-success%s'        => esc_html__('The theme is already registered, so you can go to the next step!', 'pakrco'),
                'license-json-success%s'   => esc_html__('Your theme is activated! Remote updates and theme support are enabled.', 'pakrco'),
                'license-tooltip'          => esc_html__('Need help?', 'pakrco'),

                /* translators: Theme Name */
                'welcome-header%s'         => esc_html__('Welcome to %s', 'pakrco'),
                'welcome-header-success%s' => esc_html__('Hi. Welcome back', 'pakrco'),
                'welcome%s'                => esc_html__('This wizard will set up your theme, install plugins, and import content. It is optional & should take only a few minutes.', 'pakrco'),
                'welcome-success%s'        => esc_html__('You may have already run this theme setup wizard. If you would like to proceed anyway, click on the "Start" button below.', 'pakrco'),

                'child-header'         => esc_html__('Install Child Theme', 'pakrco'),
                'child-header-success' => esc_html__('You\'re good to go!', 'pakrco'),
                'child'                => esc_html__('Let\'s build & activate a child theme so you may easily make theme changes.', 'pakrco'),
                'child-success%s'      => esc_html__('Your child theme has already been installed and is now activated, if it wasn\'t already.', 'pakrco'),
                'child-action-link'    => esc_html__('Learn about child themes', 'pakrco'),
                'child-json-success%s' => esc_html__('Awesome. Your child theme has already been installed and is now activated.', 'pakrco'),
                'child-json-already%s' => esc_html__('Awesome. Your child theme has been created and is now activated.', 'pakrco'),

                'plugins-header'         => esc_html__('Install Plugins', 'pakrco'),
                'plugins-header-success' => esc_html__('You\'re up to speed!', 'pakrco'),
                'plugins'                => esc_html__('Let\'s install some essential WordPress plugins to get your site up to speed.', 'pakrco'),
                'plugins-success%s'      => esc_html__('The required WordPress plugins are all installed and up to date. Press "Next" to continue the setup wizard.', 'pakrco'),
                'plugins-action-link'    => esc_html__('Advanced', 'pakrco'),

                'import-header'      => esc_html__('Import Content', 'pakrco'),
                'import'             => esc_html__('Let\'s import content to your website, to help you get familiar with the theme.', 'pakrco'),
                'import-action-link' => esc_html__('Advanced', 'pakrco'),

                'ready-header'      => esc_html__('All done. Have fun!', 'pakrco'),

                /* translators: Theme Author */
                'ready%s'           => esc_html__('Your theme has been all set up. Enjoy your new theme by %s.', 'pakrco'),
                'ready-action-link' => esc_html__('Extras', 'pakrco'),
                'ready-big-button'  => esc_html__('View your website', 'pakrco'),
                'ready-link-1'      => sprintf('<a href="%1$s" target="_blank">%2$s</a>', 'https://wordpress.org/support/', esc_html__('Explore WordPress', 'pakrco')),
                'ready-link-2'      => sprintf('<a href="%1$s" target="_blank">%2$s</a>', 'https://themebeans.com/contact/', esc_html__('Get Theme Support', 'pakrco')),
                'ready-link-3'      => sprintf('<a href="%1$s">%2$s</a>', admin_url('customize.php'), esc_html__('Start Customizing', 'pakrco')),
            )
        );

        add_action('widgets_init', [$this, 'widgets_init']);
    }

    public function render_child_functions_php() {
        $output
            = "<?php
/**
 * Theme functions and definitions.
 */
		 ";

        return $output;
    }

    public function widgets_init() {
        if (pakrco_is_elementor_activated()) {
            require_once get_theme_file_path('/includes/merlin/includes/template.php');
            register_widget('Pakrco_WP_Template');
        }
        require_once get_parent_theme_file_path('/includes/merlin/includes/recent-post.php');
        register_widget('Pakrco_WP_Widget_Recent_Posts');
    }

    private function set_hf($home) {
        $all_hf = $this->get_all_header_footer();
        $datas  = $all_hf[$home];
        foreach ($datas as $item) {
            foreach ($item as $object) {
                $hf = get_page_by_path($object['slug'], OBJECT, 'elementor-hf');
                if ($hf) {
                    update_post_meta($hf->ID, 'ehf_target_include_locations', $object['ehf_target_include_locations']);
                    if (isset($object['ehf_target_exclude_locations'])) {
                        update_post_meta($hf->ID, 'ehf_target_exclude_locations', $object['ehf_target_exclude_locations']);
                    }
                }
            }
        }
    }

    private function get_page_id_by_slug($slug) {
        $page = get_page_by_path($slug);
        if ($page) {
            return $page->ID;
        }
        return '0';
    }

    private function get_all_header_footer() {
        return [
            'home-1'  => [
                'header' => [
                    [
                        'slug'                         => 'header-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ],
                'footer' => [
                    [
                        'slug'                         => 'footer-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ]
            ],
            'home-2'  => [
                'header' => [
                    [
                        'slug'                         => 'header-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ],
                'footer' => [
                    [
                        'slug'                         => 'footer-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ]
            ],
            'home-3'  => [
                'header' => [
                    [
                        'slug'                         => 'header-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ],
                'footer' => [
                    [
                        'slug'                         => 'footer-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ]
            ],
            'home-4'  => [
                'header' => [
                    [
                        'slug'                         => 'header-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ],
                'footer' => [
                    [
                        'slug'                         => 'footer-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ]
            ],
            'home-5'  => [
                'header' => [
                    [
                        'slug'                         => 'header-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ],
                'footer' => [
                    [
                        'slug'                         => 'footer-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ]
            ],
            'home-6'  => [
                'header' => [
                    [
                        'slug'                         => 'header-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ],
                'footer' => [
                    [
                        'slug'                         => 'footer-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ]
            ],
            'home-7'  => [
                'header' => [
                    [
                        'slug'                         => 'header-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ],
                'footer' => [
                    [
                        'slug'                         => 'footer-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ]
            ],
            'home-8'  => [
                'header' => [
                    [
                        'slug'                         => 'header-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ],
                'footer' => [
                    [
                        'slug'                         => 'footer-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ]
            ],
            'home-9'  => [
                'header' => [
                    [
                        'slug'                         => 'header-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ],
                'footer' => [
                    [
                        'slug'                         => 'footer-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ]
            ],
            'home-10' => [
                'header' => [
                    [
                        'slug'                         => 'header-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ],
                'footer' => [
                    [
                        'slug'                         => 'footer-1',
                        'ehf_target_include_locations' => ['rule' => ['basic-global'], 'specific' => []],
                    ]
                ]
            ],
        ];
    }

    private function reset_header_footer() {
        $args = array(
            'post_type'      => 'elementor-hf',
            'posts_per_page' => -1,
            'meta_query'     => array(
                array(
                    'key'     => 'ehf_template_type',
                    'compare' => 'IN',
                    'value'   => ['type_footer', 'type_header']
                ),
            )
        );
        $hf   = new WP_Query($args);
        while ($hf->have_posts()) : $hf->the_post();
            update_post_meta(get_the_ID(), 'ehf_target_include_locations', []);
            update_post_meta(get_the_ID(), 'ehf_target_exclude_locations', []);
        endwhile;
        wp_reset_postdata();
    }

    public function set_demo_menus() {
        $main_menu = get_term_by('name', 'Main menu', 'nav_menu');
        set_theme_mod(
            'nav_menu_locations',
            array(
                'primary'  => $main_menu->term_id,
                'handheld' => $main_menu->term_id,
            )
        );
    }

    public function register_setuphf_page() {
        add_submenu_page(
            'themes.php',
            esc_html__('Setup Header Footer', 'pakrco'),
            esc_html__('Setup Header Footer', 'pakrco'),
            'manage_options',
            'hfe-setuphf',
            [$this, 'setuphf_template']
        );
    }

    public function setuphf_template() {
        echo '<h1 class="hfe-heading-inline">';
        esc_attr_e('Elementor Header & Footer Builder ', 'pakrco');
        echo '</h1>';
        ?>

        <div class="nav-tab-wrapper">
            <?php
            if (!isset(HFE\Themes\HFE_Settings_Page::$hfe_settings_tabs)) {
                HFE\Themes\HFE_Settings_Page::$hfe_settings_tabs['hfe_templates'] = [
                    'name' => esc_html__('All Templates', 'pakrco'),
                    'url'  => admin_url('edit.php?post_type=elementor-hf'),
                ];
            }

            HFE\Themes\HFE_Settings_Page::$hfe_settings_tabs['hfe_about'] = [
                'name' => esc_html__('About Us', 'pakrco'),
                'url'  => admin_url('themes.php?page=hfe-about'),
            ];

            $tabs = apply_filters('hfe_settings_tabs', HFE\Themes\HFE_Settings_Page::$hfe_settings_tabs);

            foreach ($tabs as $tab_id => $tab) {

                $tab_slug = str_replace('_', '-', $tab_id);

                $active_tab = ((isset($_GET['page']) && $tab_slug == $_GET['page']) || (!isset($_GET['page']) && 'hfe_templates' == $tab_id)) ? $tab_id : '';

                $active = ($active_tab == $tab_id) ? ' nav-tab-active' : '';

                echo '<a href="' . esc_url($tab['url']) . '" class="nav-tab' . esc_attr($active) . '">';
                echo esc_html($tab['name']);
                echo '</a>';
            }

            ?>
        </div>
        <?php
        $headers = get_posts([
            'posts_per_page' => -1,
            'post_type'      => 'elementor-hf',
            'post_status'    => 'publish',
            'meta_query'     => array(
                array(
                    'key'   => 'ehf_template_type',
                    'value' => 'type_header',
                ),
            ),
        ]);
        $footers = get_posts([
            'posts_per_page' => -1,
            'post_type'      => 'elementor-hf',
            'post_status'    => 'publish',
            'meta_query'     => array(
                array(
                    'key'   => 'ehf_template_type',
                    'value' => 'type_footer',
                ),
            ),
        ]);
        ?>
        <form method="post" action="<?php echo admin_url('admin-post.php'); ?>">
            <table class="form-table">
                <?php if (isset($_GET['saved'])) { ?>
                    <tr>
                        <td>
                            <div class="updated">
                                <p><?php esc_html_e('Success! Have been setup for your website', 'pakrco'); ?></p>
                            </div>
                        </td>
                    </tr>
                <?php } ?>

                <tr>
                    <td>
                        <fieldset>
                            <ul>
                                <li>
                                    <label><?php esc_html_e('Header', 'pakrco') ?>:
                                        <select name="setup-header">
                                            <option value="" selected>Select Header</option>
                                            <?php foreach ($headers as $header): ?>
                                                <option value="<?php echo esc_attr($header->ID) ?>"><?php echo esc_html($header->post_title); ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </label>
                                </li>
                            </ul>
                        </fieldset>
                        <fieldset>
                            <ul>
                                <li>
                                    <label><?php esc_html_e('Footer', 'pakrco') ?>:
                                        <select name="setup-footer">
                                            <option value="" selected>Select Footer</option>
                                            <?php foreach ($footers as $footer): ?>
                                                <option value="<?php echo esc_attr($footer->ID) ?>"><?php echo esc_html($footer->post_title); ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </label>
                                </li>
                            </ul>
                        </fieldset>
                    </td>
                </tr>
            </table>

            <?php wp_nonce_field('setup_custom_hf'); ?>
            <input type="hidden" name="action" value="custom_setup_hf">
            <?php submit_button(esc_html('Setup Now!')); ?>
        </form>
        <?php
    }

    private function reset_header() {
        $args = array(
            'post_type'      => 'elementor-hf',
            'posts_per_page' => -1,
            'meta_query'     => array(
                array(
                    'key'     => 'ehf_template_type',
                    'compare' => 'IN',
                    'value'   => ['type_header']
                ),
            )
        );
        $hf   = new WP_Query($args);
        while ($hf->have_posts()) : $hf->the_post();
            update_post_meta(get_the_ID(), 'ehf_target_include_locations', []);
            update_post_meta(get_the_ID(), 'ehf_target_exclude_locations', []);
        endwhile;
        wp_reset_postdata();
    }

    private function reset_footer() {
        $args = array(
            'post_type'      => 'elementor-hf',
            'posts_per_page' => -1,
            'meta_query'     => array(
                array(
                    'key'     => 'ehf_template_type',
                    'compare' => 'IN',
                    'value'   => ['type_footer']
                ),
            )
        );
        $hf   = new WP_Query($args);
        while ($hf->have_posts()) : $hf->the_post();
            update_post_meta(get_the_ID(), 'ehf_target_include_locations', []);
            update_post_meta(get_the_ID(), 'ehf_target_exclude_locations', []);
        endwhile;
        wp_reset_postdata();
    }

    public function action_setup_hf() {
        $retrieved_nonce = $_REQUEST['_wpnonce'];
        if (!wp_verify_nonce($retrieved_nonce, 'setup_custom_hf')) {
            die('Failed security check');
        }

        if (isset($_REQUEST['setup-header']) && $_REQUEST['setup-header']) {
            $this->reset_header();
            update_post_meta(absint($_REQUEST['setup-header']), 'ehf_target_include_locations', [
                'rule'     => ['basic-global'],
                'specific' => []
            ]);
        }

        if (isset($_REQUEST['setup-footer']) && $_REQUEST['setup-footer']) {
            $this->reset_footer();
            update_post_meta(absint($_REQUEST['setup-footer']), 'ehf_target_include_locations', [
                'rule'     => ['basic-global'],
                'specific' => []
            ]);
        }

        wp_redirect(admin_url('themes.php?page=hfe-setuphf&saved=1'));
        exit;
    }

    public function setup_hf_theme($hfe_settings_tabs) {
        if (!current_theme_supports('header-footer-elementor')) {
            $hfe_settings_tabs['hfe_setuphf'] = [
                'name' => esc_html__('Setup Header Footer', 'pakrco'),
                'url'  => admin_url('themes.php?page=hfe-setuphf'),
            ];
        }

        return $hfe_settings_tabs;
    }

    public function fiximport_elementor($post_meta) {
        if ('_elementor_data' === $post_meta['key']) {
            $post_meta['value'] = wp_slash($post_meta['value']);
        }

        return $post_meta;
    }

    public function get_all_options(){
        $options = [];
        $options['options']   = json_decode('{"pakrco_options_blog_style":"standard","pakrco_options_blog_sidebar":"right"}', true);
        $options['elementor']   = json_decode('{"system_colors":[{"_id":"primary","title":"Primary","color":"#CFA336"},{"_id":"primary_hover","title":"Primary Hover","color":"#ba9230"},{"_id":"secondary","title":"Secondary","color":"#2B3446"},{"_id":"secondary_hover","title":"Secondary","color":"#262e3f"},{"_id":"text","title":"Text","color":"#767676"},{"_id":"accent","title":"Heading","color":"#253046"},{"_id":"border","title":"Border","color":"#e5e5e5"},{"_id":"dark","title":"Dark","color":"#253046"},{"_id":"light","title":"Light","color":"#999"},{"_id":"background","title":"Background","color":"#fff"},{"_id":"background2","title":"Background 2","color":"#FBFAF8"}],"custom_colors":[],"system_typography":[{"_id":"heading_title","title":"Heading","typography_typography":"custom","typography_font_family":"Arsenal","typography_font_weight":"700","typography_font_size":{"unit":"px","size":42,"sizes":[]},"typography_font_size_tablet":{"unit":"px","size":35,"sizes":[]},"typography_font_size_mobile":{"unit":"px","size":30,"sizes":[]},"typography_line_height":{"unit":"em","size":1.25,"sizes":[]}},{"_id":"sub_heading","title":"Sub Heading","typography_typography":"custom","typography_font_family":"Rubik","typography_font_weight":"500","typography_font_size":{"unit":"px","size":13,"sizes":[]},"typography_line_height":{"unit":"px","size":30,"sizes":[]},"typography_letter_spacing":{"unit":"px","size":10,"sizes":[]}},{"_id":"heading_title_large","title":"Heading Large","typography_typography":"custom","typography_font_family":"Arsenal","typography_font_weight":"700","typography_font_size":{"unit":"px","size":62,"sizes":[]},"typography_font_size_tablet":{"unit":"px","size":40,"sizes":[]},"typography_font_size_mobile":{"unit":"px","size":32,"sizes":[]},"typography_line_height":{"unit":"em","size":1.065,"sizes":[]}},{"_id":"heading_title_small","title":"Heading Small","typography_typography":"custom","typography_font_family":"Arsenal","typography_font_weight":"700","typography_font_size":{"unit":"px","size":24,"sizes":[]},"typography_font_size_tablet":{"unit":"px","size":24,"sizes":[]},"typography_font_size_mobile":{"unit":"px","size":24,"sizes":[]},"typography_line_height":{"unit":"em","size":1.4285,"sizes":[]}},{"_id":"text","title":"Text","typography_typography":"custom","typography_font_family":"Rubik","typography_font_weight":"400","typography_font_size":{"unit":"px","size":16,"sizes":[]},"typography_line_height":{"unit":"em","size":1.8,"sizes":[]}}],"container_width":{"unit":"px","size":1410,"sizes":[]},"custom_typography":[],"default_generic_fonts":"Sans-serif","site_name":"Pakrco","site_description":"Just another WordPress site","page_title_selector":"h1.entry-title","activeItemIndex":1,"active_breakpoints":["viewport_mobile","viewport_mobile_extra","viewport_tablet","viewport_tablet_extra","viewport_laptop"],"viewport_md":768,"viewport_lg":1025,"button_typography_typography":"custom","button_typography_font_family":"Arsenal","button_typography_font_size":{"unit":"px","size":16,"sizes":[]},"button_typography_font_weight":"700","button_typography_text_transform":"capitalize","button_typography_line_height":{"unit":"px","size":28,"sizes":[]},"button_text_color":"#FFFFFF","button_hover_text_color":"#FFFFFF","button_padding":{"unit":"px","top":"12","right":"34","bottom":"12","left":"34","isLinked":false},"__globals__":{"button_background_color":"globals/colors?id=primary","button_hover_background_color":"globals/colors?id=primary_hover"},"viewport_mobile":767,"viewport_mobile_extra":880,"viewport_tablet":1024,"viewport_tablet_extra":1200,"viewport_laptop":1440,"blog_style":"standard"}', true);
        return $options;
    } // end get_all_options

}

return new Pakrco_Merlin_Config();
