<?php 

    class WK_Pro_License_Activate{
        
        private static $instance;
        private $license_info = '';
        private $message = 'No action happen';
        private $license_key = '';

        public static function init(){
            if(null === self::$instance ){
                self::$instance = new self;
            }
            return self::$instance;
        }

        public function initiate($license=''){
            global $wp_version;
            // check_admin_referer('wk-pro-license'); 
    
            $api_params = array(
                'edd_action' => 'activate_license',
                'license'    => $license,
                'item_name'  => urlencode( WKP_PRODUCT_NAME ), // the name of our product in EDD
                'url'        => home_url()
            );
    
            // Call the custom API.
            $response = wp_remote_post( WKP_STORE_URL, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
            
            // make sure the response came back okay
            if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
    
            	if ( is_wp_error( $response ) ) {
            		$this->message = $response->get_error_message();
            	} else {
            		$this->message = __( 'An error occurred, please try again.' );
                }
                return false;
    
            } else {
    
                $license_data = json_decode( wp_remote_retrieve_body( $response ) );
    
                if ( false === $license_data->success ) {
    
                    switch( $license_data->error ) {
                        case 'expired' :
                            $this->message = sprintf(
                                __( 'Your license key expired on %s.' ),
                                date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) )
                            );
                            break;
                        case 'disabled' :
                        case 'revoked' :
                            $this->message = __( 'Your license key has been disabled.' );
                            break;
                        case 'missing' :
                            $this->message = __( 'Invalid license.' );
                            break;
                        case 'invalid' :
                        case 'site_inactive' :
                            $this->message = __( 'Your license is not active for this URL.' );
                            break;
                        case 'item_name_mismatch' :
                            $this->message = sprintf( __( 'This appears to be an invalid license key for %s.' ), PRODUCT_NAME );
                            break;
                        case 'no_activations_left':
                            $this->message = __( 'Your license key has reached its activation limit.' );
                            break;
                        default :
                            $this->message = __( 'An error occurred, please try again.' );
                            break;
                    }
                    return $this->message;
    
                }

                if( true === $license_data->success ){
                    $this->license_info = [
                        'customer_name' => $license_data->customer_name,
                        'customer_email' => $license_data->customer_email,
                        'payment_id' => $license_data->payment_id,
                        'price_id' => $license_data->price_id,
                        'site_count' => $license_data->site_count,
                        'license_limit' => $license_data->license_limit,
                        'expires' => $license_data->expires,
                        'activations_left' => $license_data->activations_left,
                    ];
                    $this->license_key = $license;
                    /**
                     * set and update license key
                     */
                    update_option( 'wk_pro_license_key', $this->license_key );
                    /**
                     * update license info
                     * for user
                     */
                    update_option( 'wk_pro_user_license_info', $this->license_info );
                    update_option( 'wk_pro_user_license_status', $license_data->license );
                    return true;
                }
    
            }
    
        }

    }

?>