<?php
namespace ONLINE_ADAv4\Services;

use ONLINE_ADAv4\Plugin;

class License {
    
    use \ONLINE_ADAv4\Traits\Singleton;

    public function __construct(){
    }
    public function check_license($manifest = []) {
        $data = Plugin::instance()->info;
        if(!empty($manifest)){ 
            $data = $manifest;
        }
        
        $response = wp_remote_post($data['license_url'].$data["routes"]["check_license"]["slug"], [
            'method' => $data["routes"]["check_license"]["method"],
            'headers' => [
                'Content-Type' => 'application/x-www-form-urlencoded'
            ],
            'sslverify' => false,
            'body' => [
                "site_url" => $data["site_url"],
                "license" => $data["license"],
                "version" => $data["version"]
            ],
            'data_format' => 'body'
        ]);
        
        if(is_wp_error($response)){

            $error_message = $response->get_error_message();
            
            update_option($data['license_status_key'], 0);
            return 'invalid';

        }else{
            $code = wp_remote_retrieve_response_code( $response );
            $parsedResponse = json_decode(wp_remote_retrieve_body( $response ), true);
            
            if($parsedResponse['status'] === "OK"){
                update_option($data['license_status_key'], true);
                return 'valid';
            }else{
                update_option($data['license_status_key'], 0);
                return 'invalid';
            }
        }
    }
    public function check($request,$skip_return = false) {
        $data = Plugin::instance()->info;
        $data['license'] = $request['license'];
        
        $response = wp_remote_post($data['license_url'].$data["routes"]["check_license"]["slug"], [
            'method' => $data["routes"]["check_license"]["method"],
            'headers' => [
                'Content-Type' => 'application/x-www-form-urlencoded'
            ],
            'sslverify' => false,
            'body' => [
                "site_url" => $data["site_url"],
                "license" => $data["license"],
                "version" => $data["version"]
            ],
            'data_format' => 'body'
        ]);
        
        if(is_wp_error($response)){
            $error_message = $response->get_error_message();
            
            update_option($data['license_status_key'], 0);
            if(!$skip_return){
                return [
                    "status" => "There Was an Error",
                    "message" => $error_message,
                    "url" => $data['license_url'].$data["routes"]["check_license"]["slug"],
                ];
            }

        }else{
            $code = wp_remote_retrieve_response_code( $response );
            $parsedResponse = json_decode(wp_remote_retrieve_body( $response ), true);
            
            if(isset($parsedResponse['message']) && strpos($parsedResponse['message'], 'Domain(s) Inactive') !== FALSE) {
                $this->activate($request);
            }else{
                if($parsedResponse['status'] === "OK"){
                    update_option($data['license_status_key'], true);
                }else{
                    update_option($data['license_status_key'], 0);
                }
                if(!$skip_return) {
                    return $parsedResponse;
                }
            }
        }
    }
    public function activate($data) {
        $response = wp_remote_post($data['license_url'].$data["routes"]["activate_license"]["slug"], [
            'method' => $data["routes"]["activate_license"]["method"],
            'headers' => [
                'Content-Type' => 'application/x-www-form-urlencoded'
            ],
            'sslverify' => false,
            'body' => [
                "site_url" => $data["site_url"],
                "license" => $data["license"],
                "version" => $data["version"]
            ],
            'data_format' => 'body'
        ]);

        if(is_wp_error($response)){

            $error_message = $response->get_error_message();

            return [
                "status" => "",
                "message" => $error_message
            ];
            
        }else{

            $code = wp_remote_retrieve_response_code( $response );

            $parsedResponse = json_decode(wp_remote_retrieve_body( $response ), true);
            
            update_option($data['license_key'], $data['license']);

            if($parsedResponse['status'] === "OK"){
                update_option($data['license_status_key'], true);
            }else{
                update_option($data['license_status_key'], 0);
            }

            return $parsedResponse;
        }
    }
    public function get_product_info() {
        $data = Plugin::instance()->info;
        $response = wp_remote_post($data['license_url'].$data["routes"]["product_info"]["slug"], [
            'method' => $data["method"],
            'headers' => [
                'Content-Type' => 'application/x-www-form-urlencoded'
            ],
            'sslverify' => false,
            'body' => [
                "site_url" => $data["site_url"],
                "license" => $data["license"],
                "version" => $data["version"],
                "item_name" => $data["item_name"]
            ],
            'data_format' => 'body'
        ]);

        if(is_wp_error($response)){

            $error_message = $response->get_error_message();

            return [
                "status" => "",
                "message" => $error_message
            ];
            
        }else{

            $code = wp_remote_retrieve_response_code( $response );
            $parsedResponse = json_decode(wp_remote_retrieve_body( $response ), true);
            
            return $parsedResponse;
        }
    }
}