<?php 

    class WK_Pro_Activate_License{

        private $license_check;
        private $license_activation;
        private $license_deactivation;
        private $wk_pro_updater;

        public function __construct(){
            $this->load_license_resource();
            add_action('wp_ajax_wk_pro_activate_license_key', [$this, 'wk_pro_activate_license']);
            add_action('wp_ajax_wk_pro_deactivate_license', [$this, 'wk_pro_deactivate_license']);
        }

        public function get_license_key() {
            return trim( get_option( 'wk_pro_license_key' ) );
        }

        public function set_license_key( $license_key ) {
            return update_option( 'wk_pro_license_key', $license_key );
        }

        public function load_license_resource(){
            require_once WKP_PATH . '/license/check.php';
            require_once WKP_PATH . '/license/activate.php';
            require_once WKP_PATH . '/license/deactivate.php';
            require_once WKP_PATH . '/license/updater.php';

            $this->license_check = WK_Pro_Check_License::init();
            $this->license_activation = WK_Pro_License_Activate::init();
            $this->license_deactivation = WK_Pro_License_Deactivate::init();

            add_action( 'admin_init', [$this, "wk_pro_updater"] );

        }

        public function wk_pro_updater(){

            $license_key = $this->get_license_key();

            $this->wk_pro_updater = new WidgetKitPro_Updater(
                WKP_STORE_URL, 
                WKP_PLUGIN_BASE, 
                array(
                    'version' => WKP_VERSION,      // current version number
                    'license' => $license_key,              // license key (used get_option above to retrieve from DB)
                    'item_id' => WKP_ITEM_ID,      // ID of the product
                    'author'  => 'Themesgrove',             // author of this plugin
                    'beta'    => false,
                )
            );
            
        }

        /**
         * check pro license
         */
        public function wk_pro_activate_license(){

            if (! empty($_POST['license'])) {

                $license = $_POST['license'];
                $activation_response = $this->license_activation->initiate($license);

                if(true === $activation_response){
                    echo $response_array['status'] = 'true';
                }else{
                    ?>
                    <div class="fallback-message"><?php echo $activation_response; ?></div>
                    <?php 
                }

            } 
            die();
        }

        public function wk_pro_deactivate_license(){
            $this->get_license_key();
            $deactivation_response = $this->license_deactivation->initiate($this->get_license_key());
            if( true === $deactivation_response ){
                echo $response_array['status'] = 'true';
            }else{
                ?>
                <div class="fallback-message"><?php echo $deactivation_response; ?></div>
                <?php
                //return $deactivation_response;
            }
            die();
        }

    }
    new WK_Pro_Activate_License();


?>