<?php
namespace PEVFSLite;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! trait_exists( __NAMESPACE__ . '\\API' ) ) {
	trait API {
		private function call_api( $path, $method = 'GET', $params = array() ) {
			$path	= '/v1' . $path;
			$url	= 'https://api.freemius.com' . $path;
			$method	= \strtoupper( $method );
			$date	= \gmdate( 'r' );
			$content_md5 = '';
			$content_type = '';

			if ( \in_array( $method, array( 'POST', 'PUT' ) ) ) {
				$content_type = 'application/json';
				if ( ! empty( $params ) ) {
					$content_md5 = \md5( \json_encode( $params ) );
				}
			}

			$string_to_sign	= \implode( "\n", array( $method, $content_md5, $content_type, $date, $path ) );
			$signature		= $this->base64_url_encode( \hash_hmac( 'sha256', $string_to_sign, $this->secret_key ) );
			$auth_type		= ( $this->secret_key !== $this->public_key ) ? 'FS' : 'FSP';
			$authorization	= $auth_type . ' ' . $this->id . ':' . $this->public_key . ':' . $signature;

			$args = array(
				'method'	=> $method,
				'headers'	=> array( 
					'Content-Type'	=> $content_type, 
					'Content-MD5'	=> $content_md5, 
					'Date'			=> $date, 
					'Authorization'	=> $authorization 
				),
				'timeout'	=> 15,
			);
			if ( ! empty( $params ) ) {
				$args['body'] = \json_encode( $params );
			}

			$response = \wp_remote_request( $url, $args );
			return $this->decode_response( $response );
		}

		private function call_service_api( $params = array() ) {
			$url = 'https://wp.freemius.com/action/service/user/install/';
			
			$accounts = $this->get_accounts();
			$site_uid = $accounts['unique_id'] ?? $this->get_anonymous_id();

			$defaults = array(
				'public_key'                      => $this->public_key,
				'plugin_public_key'               => $this->public_key,
				'plugin_id'                       => $this->id,
				'plugin_slug'                     => $this->slug,
				'plugin_version'                  => $this->version,
				'plugin_type'                     => $this->type,
				'site_uid'                        => $site_uid,
				'site_url'                        => \home_url(),
				'site_name'                       => \get_bloginfo( 'name' ),
				'is_active'                       => '1',
				'is_uninstalled'                  => '0',
				'is_localhost'                    => ( \strpos( \home_url(), 'localhost' ) !== false || \strpos( \home_url(), '127.0.0.1' ) !== false ) ? '1' : '0',
				'is_diagnostic_tracking_allowed'  => '1',
				'sdk_version'                     => $this->sdk_version,
			);

			$install_id = $this->get_install_id();
			if ( $install_id && \strpos( $install_id, 'pending_' ) !== 0 ) {
				$defaults['id'] = $install_id;
			}

			$body = \array_merge( $defaults, $params );

			$args = array(
				'body'    => $body,
				'timeout' => 30,
			);

			$response = \wp_remote_post( $url, $args );
			return $this->decode_response( $response );
		}

		private function decode_response( $response ) {
			if ( \is_wp_error( $response ) ) {
				return $response;
			}
			return \json_decode( \wp_remote_retrieve_body( $response ), true );
		}

		private function base64_url_encode( $data ) {
			return \str_replace( array( '+', '/', '=' ), array( '-', '_', '' ), \base64_encode( $data ) );
		}

		public function add_action( $tag, $callback, $priority = 10, $accepted_args = 1 ) {
			return \add_action( "fs_{$tag}_{$this->slug}", $callback, $priority, $accepted_args );
		}

		public function add_filter( $tag, $callback, $priority = 10, $accepted_args = 1 ) {
			return \add_filter( "fs_{$tag}_{$this->slug}", $callback, $priority, $accepted_args );
		}
	}
}
