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

class SchematicInstallation {

    public function __construct() {
        add_action('admin_menu', array($this, 'installation_page'));
        add_action('admin_notices', array($this, 'notice'));
        add_action('admin_head', array($this, 'admin_scripts'));
        add_action('wp_ajax_schematic_save_api_key', array($this, 'schematic_save_api_key'));
    }

    public function schematic_save_api_key() {
        if (current_user_can('administrator')) {
            check_ajax_referer('schematic-nonce');
            
            delete_option('_schematic_key_error');
            update_option('_schematic_api_key', sanitize_text_field($_POST['_schematic_api_key']));
            update_option('_schematic_d_l', serialize(array('key' => sanitize_text_field($_POST['key']), 'flag' => false)));
            update_option('_schematic_installed', '1');
            wp_send_json_success();
        }
    }

    public function notice() {
        if ((isset($_GET['page']) && $_GET['page'] === 'wp-schema-installation') or ( isSchematicInstalled())) {
            return false;
        }
        ?>
        <div class="notice notice-success is-dismissible">
            <p>WP Schema Plugin is activated. Please continue with the installation of
                <a href="<?php echo admin_url('admin.php?page=wp-schema-installation') ?>">WP Schema Plugin</a> </p>
        </div>
        <?php
    }

    public function admin_scripts() {
        global $schematic_file;

        if (isset($_GET['page']) && in_array($_GET['page'], array('wp-schema-installation'))) {

            wp_enqueue_style('schematic-schematic-style', plugins_url('/css/schematic-admin.css', $schematic_file), array(), WPSCHEMA_VERSION);

            wp_enqueue_script('schematic-admin-installation', plugins_url('/js/wp-schema-installation.js', $schematic_file), array('jquery'), WPSCHEMA_VERSION);
        }
    }

    function installation_page() {
        if (isset($_GET['action']) && $_GET['action'] === "schematic-key") {
            delete_option('_schematic_api_key');
        }

        if (!isSchematicInstalled()) {
            global $schematicIcon;

            add_menu_page('Schema', 'Schema', 'administrator', 'wp-schema-installation', array($this, 'install_page'), $schematicIcon, 8);
        }
    }

    public function install_page() {
        // step 1 verify
        if (!get_option('_schematic_api_key')) {
            $this->verifyApiKeyStep();
        }
    }

    public function verifyApiKeyStep() {
        $nonce = wp_create_nonce("schematic-nonce");
        ?>
        <div class="wrap">

            <h2>WP Schema Plugin Installation</h2>

            <div id="apikey_box"> <h2> PLEASE VERIFY PLUGIN LICENSE KEY </h2>

                <div class="container">
                    <p> Don't know what is LICENSE KEY? Please visit <a target="_blank" href="https://wpschemaplugin.com">wpschemaplugin.com </a> for more info</p>

                    <input type="text" id="schematic_key" placeholder="API KEY" />
                    <input id="schematic_verify_api_key" type="button" value="VERIFY KEY" class="button button-primary" />
                    <input id="redirect_url" type="hidden" value="<?php echo(admin_url('admin.php?page=wp-schema&installed=true')); ?>" />
                    <input id="schematicNonce" type="hidden" value="<?php echo($nonce); ?>" />

                </div>
            </div>

            <div id="api_key_success"></div>
            <div style="display: none" id="api_key_success_proceed">
                You now may <span id="schematic_api_key_save" class="button">Proceed</span> to the settings page
            </div>
            <div id="api_key_error"></div>
        </div>

        <?php
    }

}

new SchematicInstallation();
