<?php
if (! defined('ABSPATH')) {
    exit;
}

class PCAFE_SPF_Admin_Menu {

    public function __construct() {
        add_action('admin_menu', [$this, 'pcafe_spf_add_plugin_page'], 9999);
        add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'), 2);
        add_action('wp_ajax_spf_save_plugins_data', [$this, 'spf_save_plugins_data']);
        add_action('wp_ajax_spf_global_setting', [$this, 'spf_global_setting']);
        add_action('wp_ajax_spf_install_manage_plugin', [$this, 'spf_install_manage_plugin']);

        $this->get_required_files();
        $this->is_offer_active();
    }

    public function pcafe_spf_add_plugin_page() {

        $icon_64 = 'PHN2ZyB3aWR0aD0iODAiIGhlaWdodD0iODAiIHZpZXdCb3g9IjAgMCA4MCA4MCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTQzLjQ2NjIgNjMuMDM5MkM0My4wMzQxIDYzLjQ3MTYgNDIuNDc2MiA2My43NTYxIDQxLjg3MjQgNjMuODUxOUM0MS4yNjg3IDYzLjk0NzcgNDAuNjUwMSA2My44NSA0MC4xMDUzIDYzLjU3MjZDMzUuMDU1NCA2MC45NjkzIDMwLjQ0NTUgNTcuNTg5MiAyNi40NDQxIDUzLjU1NThDMjIuNDEwMSA0OS41NTUgMTkuMDI5OSA0NC45NDUxIDE2LjQyNzQgMzkuODk0NkMxNi4xNSAzOS4zNDk5IDE2LjA1MjIgMzguNzMxMyAxNi4xNDgxIDM4LjEyNzVDMTYuMjQzOSAzNy41MjM4IDE2LjUyODQgMzYuOTY1OSAxNi45NjA4IDM2LjUzMzhMMjQuNTc0NyAyOC45MTgyTDIuNDYxNzkgNi44MDUzQy0zLjc2ODEgMjMuMzg2OCAxLjk0NDM4IDQ1LjQzMjQgMTguMjU2OCA2MS43NDMyQzM0LjU2NzUgNzguMDU1NiA1Ni42MTMyIDgzLjc2OCA3My4xOTQ2IDc3LjUzODJMNTEuMDgxOCA1NS40MjUyTDQzL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kgMjAuNDYzNCA0OS40MzgzIDIwLjc1MjdMNTMuODMxMiAyNS4xNDY4TDY0LjAwMDYgMTQuOTc3NEM2NC4yOTAyIDE0LjY4OTQgNjQuNjgyMiAxNC41Mjc5IDY1LjA5MDcgMTQuNTI4NUM2NS40OTkyIDE0LjUyOTEgNjUuODkwOCAxNC42OTE3IDY2LjE3OTYgMTQuOTgwN0M2Ni40NjgzIDE1LjI2OTYgNjYuNjMwNyAxNS42NjEzIDY2LjYzMTEgMTYuMDY5OEM2Ni42MzE0IDE2LjQ3ODMgNjYuNDY5OCAxNi44NzAyIDY2LjE4MTUgMTcuMTU5N0w1NC45MjIzIDI4LjQxOUM1NC43NzkxIDI4LjU2MjIgNTQuNjA5MSAyOC42NzU4IDU0LjQyMiAyOC43NTMzQzU0LjIzNDkgMjguODMwOCA1NC4wMzQzIDI4Ljg3MDcgNTMuODMxOCAyOC44NzA3QzUzLjYyOTMgMjguODcwNyA1My40Mjg3IDI4LjgzMDggNTMuMjQxNiAyOC43NTMzQzUzLjA1NDUgMjguNjc1OCA1Mi44ODQ1IDI4LjU2MjIgNTIuNzQxMyAyOC40MTlMNDcuMjU2MSAyMi45MzVDNDYuOTY2OCAyMi42NDU1IDQ2LjgwNDMgMjIuMjUzMSA0Ni44MDQzIDIxLjg0MzhDNDYuODA0MyAyMS40MzQ2IDQ2Ljk2NjggMjEuMDQyMiA0Ny4yNTYxIDIwLjc1MjdaIiBmaWxsPSJ3aGl0ZSIvPgo8L3N2Zz4K';
        $icon_data_uri = 'data:image/svg+xml;base64,' . $icon_64;

        add_menu_page(
            __('Smart Phone Field', 'smart-phone-field-for-wp-forms'),
            __('SmartPhone Field', 'smart-phone-field-for-wp-forms'),
            'manage_options',
            'smart-phone-field-pro',
            array($this, 'pcafe_spf_admin_page'),
            $icon_data_uri
        );
    }

    public function pcafe_spf_admin_page() {
        include plugin_dir_path(__FILE__) . 'sections/general.php';
    }

    public function get_required_files() {
        include PCAFE_SPF_PATH . 'includes/admin/utils.php';
        include PCAFE_SPF_PATH . 'includes/addons/addons.php';
    }

    public function enqueue_admin_scripts($screen) {

        $current_screen = get_current_screen();

        if (strpos($current_screen->base, 'smart-phone-field') !== false) {
            wp_enqueue_style('pcafe_spf_select2', PCAFE_SPF_URL . 'assets/css/select2.min.css', array(), PCAFE_SPF_VERSION);
            wp_enqueue_style('pcafe_spf_admin_style', PCAFE_SPF_URL . 'assets/css/admin_style.css', array(), PCAFE_SPF_VERSION);

            wp_enqueue_script('pcafe_spf_freemius', 'https://checkout.freemius.com/js/v1/', array(), PCAFE_SPF_VERSION, true);
            wp_enqueue_script('pcafe_spf_select2', PCAFE_SPF_URL . '/assets/js/select2.min.js', array(), PCAFE_SPF_VERSION, true);
            wp_enqueue_script('pcafe_spf_admin', PCAFE_SPF_URL . '/assets/js/admin.js', array(), PCAFE_SPF_VERSION, true);
            wp_localize_script('pcafe_spf_admin', 'pcafe_spf_admin', array(
                'ajaxurl' => admin_url('admin-ajax.php'),
                'nonce' => wp_create_nonce('pcafe_spf_nonce'),
                'logo_url' => PCAFE_SPF_URL . 'assets/img/SPF.svg',
            ));
        }

        wp_enqueue_script('pcafe_spf_forms', PCAFE_SPF_URL . '/assets/js/spf_forms.js', array(), PCAFE_SPF_VERSION, true);
    }

    public function spf_save_plugins_data() {
        // phpcs:ignore WordPress.Security.NonceVerification.Missing
        if (!isset($_POST['spf_plugin_addon']) || !wp_verify_nonce(sanitize_key(wp_unslash($_POST['spf_plugin_addon'])), 'spf_plugin_addon')) {
            return;
        }

        // phpcs:ignore WordPress.Security.NonceVerification.Missing
        if (isset($_POST['addon_list'])) {
            $sanitized_addon_list = is_array($_POST['addon_list'])
                ? array_map('sanitize_text_field', wp_unslash($_POST['addon_list']))
                : sanitize_text_field(wp_unslash($_POST['addon_list']));

            update_option('pcafe_spf_plugin_list', $sanitized_addon_list);
        } else {
            $sanitized_addon_list = [];
            update_option('pcafe_spf_plugin_list', $sanitized_addon_list);
        }
        wp_die();
    }

    public function spf_global_setting() {
        // phpcs:ignore WordPress.Security.NonceVerification.Missing
        if (!isset($_POST['spf_setting_nonce']) || !wp_verify_nonce(sanitize_key(wp_unslash($_POST['spf_setting_nonce'])), 'spf_setting_nonce')) {
            return;
        }

        // phpcs:ignore WordPress.Security.NonceVerification.Missing
        if (isset($_POST['action'])) {
            $data = wp_unslash($_POST);
            unset($data['spf_setting_nonce']);
            unset($data['_wp_http_referer']);
            unset($data['action']);

            $sanitized_settings = map_deep($data, 'sanitize_text_field');

            update_option('pcafe_spf_global_setting', $sanitized_settings);
        }
        wp_die();
    }

    public function spf_install_manage_plugin() {
        check_ajax_referer('pcafe_spf_nonce', 'security');

        if (!current_user_can('install_plugins')) {
            wp_send_json_error('You do not have permission to perform this action.');
        }

        // phpcs:ignore WordPress.Security.NonceVerification.Missing
        $plugin_slug     = isset($_POST['plugin_slug']) ? sanitize_text_field(wp_unslash($_POST['plugin_slug'])) : '';
        // phpcs:ignore WordPress.Security.NonceVerification.Missing
        $filename = isset($_POST['filename']) ? sanitize_text_field(wp_unslash($_POST['filename'])) : '';
        // phpcs:ignore WordPress.Security.NonceVerification.Missing
        $plugin_action   = isset($_POST['plugin_action']) ? sanitize_text_field(wp_unslash($_POST['plugin_action'])) : '';


        include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
        include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
        include_once ABSPATH . 'wp-admin/includes/plugin.php';

        if ($plugin_action === 'install') {
            $api = plugins_api('plugin_information', ['slug' => $plugin_slug]);

            if (is_wp_error($api)) {
                wp_send_json_error($api->get_error_message());
            }

            $upgrader = new \Plugin_Upgrader(new \WP_Ajax_Upgrader_Skin());
            $install_result = $upgrader->install($api->download_link);

            if (is_wp_error($install_result)) {
                wp_send_json_error($install_result->get_error_message());
            }

            wp_send_json_success(['message' => 'Installed successfully.']);
        }

        if ($plugin_action === 'activate') {
            $plugin_path = WP_PLUGIN_DIR . '/' . $plugin_slug . '/' . $filename . '.php';

            if (!file_exists($plugin_path)) {
                wp_send_json_error('Plugin file not found.');
            }

            $activate_result = activate_plugin($plugin_path);

            if (is_wp_error($activate_result)) {
                wp_send_json_error($activate_result->get_error_message());
            }

            wp_send_json_success(['message' => 'Activated successfully.']);
        }

        wp_send_json_error('Invalid action.');
    }

    public function is_offer_active() {
        $transient_key = 'pcafe_spf_offer_notice';
        $notice_array = get_transient($transient_key);

        if ($notice_array === false) {
            // Fetch from remote only if cache expired
            $endpoint  = 'https://api.pluginscafe.com/wp-json/pcafe/v1/offers?id=5';
            $response  = wp_remote_get($endpoint, array('timeout' => 10));

            if (!is_wp_error($response) && $response['response']['code'] === 200) {
                $notice_array = json_decode($response['body'], true);

                // Save in cache for 3 hours (change as needed)
                set_transient($transient_key, $notice_array, 12 * HOUR_IN_SECONDS);
            }
        }
    }
}

new PCAFE_SPF_Admin_Menu();
