<?php

namespace EmailKitPro\Admin;

defined( 'ABSPATH' ) || exit;

/**
 * EmailKit pro license & auto-update class
 *
 * @version 1.0.0
 */
class Base {

    private static $instance;
    
    /**
     * A static method to return an instance of the class.
     *
     * @since 1.0.0
     * @access public
     * @return self
     */
    public static function instance()
    {
        if (!self::$instance) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * Get the parent slug
     * 
     * @since 1.0.0
     * @access public
     * @return string
     */
    public static function parent_slug(){
        return 'emailkit-menu';
    }

    /**
     * Initiate the class
     * 
     * @since 1.0.0
     * @access public
     * @return void
     */
    public function init(){

        $this->auto_updater();
        add_action('admin_menu', [$this, 'register_menus'], 999);
        add_action('admin_init', [$this, 'register_actions'], 999);
    }

    /**
     * Register menus
     * 
     * @since 1.0.0
     * @access public
     * @return void
     */
    public function register_menus(){
        add_submenu_page( self::parent_slug(), esc_html__( 'License', 'emailkit-pro' ), esc_html__( 'License', 'emailkit-pro' ), 'manage_options', self::parent_slug().'-license', [$this, 'register_settings_contents__license'], 11);
    }

    /**
     * Register settings contents
     * 
     * @since 1.0.0
     * @access public
     * @return void
     */
    public function register_settings_contents__license(){
        include('License/views/license.php');
    }

    /**
     * Register actions
     * 
     * @since 1.0.0
     * @access public
     * @return void
     */
    public function register_actions(){

        if(isset( $_POST['emailkit_pro_license_setting_action'])) {
            
            // Check if the nonce is set and valid
            if ( !isset( $_POST['emailkit_pro_license_nonce'] ) || !wp_verify_nonce( sanitize_key( $_POST['emailkit_pro_license_nonce'] ), 'emailkit-pro-license-page' ) ) {
                die( 'Security check failed. Please try again.' );
            }
                        
            $key = !isset($_POST['emailkit_pro_settings_page_key']) ? '' : sanitize_text_field(wp_unslash($_POST['emailkit_pro_settings_page_key']));

            switch($_POST['emailkit_pro_license_setting_action']){
                case 'activate':
                    \EmailKitPro\Admin\License\License::instance()->activate($key);
                break;
                case 'deactivate':
                    \EmailKitPro\Admin\License\License::instance()->deactivate();
                break;
            }
        }
    }

    /**
     * Auto updater
     * 
     * @since 1.0.0
     * @access public
     * @return void
     */
    public function auto_updater(){

        $license_key = !empty( \EmailKitPro\Admin\License\License::instance()->get_license() ) ? explode('-', trim( \EmailKitPro\Admin\License\License::instance()->get_license() )) : '';
        $license_key = !isset( $license_key[0] ) ? '' : $license_key[0];
        $plugin_dir_and_filename = EMAILKITPRO_DIR . 'emailkit-pro.php';
        $active_plugins = get_option( 'active_plugins' );

        foreach ( $active_plugins as $active_plugin ) {
            if ( false !== strpos( $active_plugin, 'emailkit-pro.php' ) ) {
                $plugin_dir_and_filename = $active_plugin;
                break;
            }
        }
        if ( !isset( $plugin_dir_and_filename ) || empty( $plugin_dir_and_filename) ) {
            throw new \Exception( 'Plugin not found! Check the name of your plugin file in the if check above' );
        }

        new \EmailKitPro\Admin\License\PluginUpdater(
            \EmailKitPro::account_url(),
            $plugin_dir_and_filename,
            array(
                'version' => EMAILKITPRO_VERSION, // current version number.
                'license' => $license_key, // license key 
                'item_id' => \EmailKitPro::product_id(), // id of this product
                'author'  => \EmailKitPro::author_name(), // author of this plugin.
                'url'     => home_url(),
            )
        );
    }
}