<?php
namespace CodeClouds\Unify\Model;

/**
 * Connection post type model.
 * @package CodeClouds\Unify
 */
class PlatformApiModel
{
    private static $platform_endpoint = UNIFY_PLATFORM_ENDPOINT;
    private static $testing_domain = '';


    public static function getDomain($testing_domain){
        self::$platform_endpoint = empty($testing_domain)?self::$platform_endpoint:$testing_domain;
    }

    /**
     * Http api call to validate license
     * @return array
     */

    public static function callPlatformToProcess($param,$testing_domain)
    {
        self::getDomain($testing_domain);
        $method = 'validate-wordpress-license';
        $curl_url = self::$platform_endpoint . $method;
		$args = array(
			'body'        => json_encode($param),
			'httpversion' => '1.0',
			'headers'     => [
				'Content-Type' => 'application/json'
			],
			'cookies'     => [],
		);		
		$response = wp_remote_post( $curl_url, $args );
        return $response;
    }

    /**
     * Http api call to validate Post Existing WP-config
     * @return array
     */

    public static function callToPostWpConfig($param,$testing_domain)
    {
        self::getDomain($testing_domain);
        $method = 'api/wordpress/import/products';
        
        $curl_url = self::$platform_endpoint . $method;
		$args = array(
			'body'        => $param,
			'httpversion' => '1.0',
			'headers'     => [
				'Content-Type' => 'application/json'
			],
			'cookies'     => [],
		);		
		$response = wp_remote_post( $curl_url, $args );
        return $response;
        
    }

    /**
     * Http api call to Post Cart Data
     * @return array
     */

    public static function sendStoreData($domain_name,$param)
    {
        self::$platform_endpoint = $domain_name;
        $method = 'checkout';
        
        $curl_url = self::$platform_endpoint . $method;
		$args = array(
			'body'        => json_encode($param),
			'httpversion' => '1.0',
			'headers'     => [
				'Content-Type' => 'application/json',
                'X-Requested-With'=> 'XMLHttpRequest'
			],
			'cookies'     => [],
		);		
		$response = wp_remote_post( $curl_url, $args );
        return $response;
    }


    /**
     * Http api call to Post Cart Data in Fallback
     * @return array
     */

    public static function sendStoreDataGet($param)
    {
        $method = 'checkout';
        
        $curl_url = self::$platform_endpoint . $method . '/?fallback=' . $param;
		$args = array(
			'body'        => [],
			'httpversion' => '1.0',
			'headers'     => [
				'Content-Type' => 'application/json',
                'X-Requested-With'=> 'XMLHttpRequest'
			],
			'cookies'     => [],
		);		
		$response = wp_remote_get( $curl_url, $args );
        echo esc_html($response);
    }

    public function requestCancellation($fields,$endpoint){
    

        $api_method = 'auth/cancel/checkout-pro';
        $curl_url = $endpoint.$api_method;

        $auth_token = md5($fields["email"]);
        
		$header = [
            'Content-Type' => 'application/json',
            'X-Auth-token' => $auth_token,
        ];
        $args = array(
            'timeout' => '5000',
            'httpversion' => '1.1',
            'cookies' => [],
            'data_format' => 'body',
        );

        $response = \Requests::post($curl_url, $header, json_encode($fields));
        return $response;
    }

}

