<?php
// Silence may be golden, but sometimes words paint the canvas of understanding.

namespace Plugli\Linksy\SearchAndReplace\Admin;

use Exception;
use Linksy\SearchAndReplace\Plugli\Standard\Admin\Admin as AI_Admin;

use Plugli\Linksy\SearchAndReplace\Admin\Partials\SearchAndReplace\SearchAndReplace;
use Linksy\SearchAndReplace\Plugli\Linksy\Common\Helpers\WP_Cron_Manager;
use Linksy\SearchAndReplace\Plugli\Standard\Helpers\Option;

define('LINKSYAI_SAR_PLUGIN_ADMIN_URL', plugin_dir_url(__FILE__));
define('LINKSYAI_SAR_PLUGIN_ADMIN_DIR', plugin_dir_path(__FILE__));
define('LINKSYAI_SAR_PLUGIN_SITE_URL', get_site_url());

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @link  mailto:reztomoloju@gmail.com
 * @since 1.0.0
 *
 * @author Linksy Search and Replace
 */
class Admin extends AI_Admin
{

    /**
     * used to auto-update the plugin
     *
     * @var    object|null
     * @access protected
     */
    protected $plugin_updater;

    protected function setup()
    {
        $this->add_page('search_and_replace', new SearchAndReplace($this->plugin_name, $this->version, $this->plugin_base_dir, $this->plugin_base_path));
    }

    protected function add_menu_page()
    {
        $this->initialize();

        foreach ($this->pages as $page) {
            $page->register_admin_page();
        }
    }

    public function enqueue_styles()
    {
        parent::enqueue_styles();

        $dir = plugin_dir_url(__FILE__) . 'assets/css/';

        if ($this->is_current_page()) {
            wp_enqueue_style($this->plugin_name . '-main', $dir . 'admin.css', array(), $this->version, 'all');
        }
    }

    public function enqueue_scripts()
    {
        parent::enqueue_scripts();

        $dir = plugin_dir_url(__FILE__) . 'assets/js/';

        if ($this->is_current_page()) {
            // Enqueue global Linksy scripts
            wp_enqueue_script($this->plugin_name . '-custom-keyword', $dir . 'custom-keyword.vue.js', [], $this->version, false);
            wp_enqueue_script($this->plugin_name . '-main', $dir . 'admin.js', [$this->plugin_name], $this->version, false);

            // Enqueue inline scripts
            $this->admin_globals['site_url'] = LINKSYAI_SAR_PLUGIN_SITE_URL;
            $this->admin_globals['tools_url'] = LINKSYAI_SAR_PLUGIN_SITE_URL . '/wp-admin/tools.php';
        }

        wp_localize_script($this->plugin_name, 'LINKSYAI_SAR', $this->admin_globals);

    }

    public function add_plugin_cron_schedules($schedules)
    {
        $custom_schedules = [
            'search_and_replace_analyze_links_interval' => 60 * 5
        ];

        foreach ($custom_schedules as $key => $value) {
            WP_Cron_Manager::add_schedule($key, $value);
        }

        return array_merge(
            is_null($schedules) ? [] : $schedules,
            WP_Cron_Manager::get_schedules()
        );
    }

    protected function initialize()
    {
        if (!Option::get(LINKSYAI_SAR_OPTION_PLUGIN_OPENED, false)) {
            Option::set(LINKSYAI_SAR_OPTION_PLUGIN_OPENED, true);
            Option::set(LINKSYAI_SAR_OPTION_SETUP_STARTED, true);
            
            $redirect_url = esc_url_raw(admin_url("tools.php?page={$this->plugin_name}-search-and-replace&setup=0"));
            wp_redirect($redirect_url);
            exit;
        }
    }

    public function is_current_page($custom_page = true)
    {
        if (!empty($this->plugin_name) && isset($_GET['page'])) {
            $page_slug = sanitize_key(wp_unslash($_GET['page']));
        
            if (strpos(strtolower($page_slug), strtolower($this->plugin_name)) !== false) {
                return true;
            }
        }

        if ($custom_page && (!empty($this->pages['post']) && $this->pages['post']->is_current_page())) {
            return true;
        }

        return false;
    }
}
