<?php if (!defined('ABSPATH'))
	exit; ?>
<?php

class polylai_Cloud
{
	/**
	 * Summary of requestTranslation
	 * @param polylai_CloudRequest $cloudRequest
	 * @return void
	 */
	public static function requestTranslation($cloudRequest)
	{
		$json = json_encode($cloudRequest);
		polylai_Utils::db_log('info', 'translate cloud', 'start', $cloudRequest->post_id, $cloudRequest->post_title, $json);
		// wp remote call to POLYLAI_WS_URL
		$response = wp_remote_post(POLYLAI_WS_URL . "/translate", [
			'timeout' => 100000,
			'method' => 'POST',
			'blocking' => true,
			'body' => json_encode($cloudRequest),
			'headers' => [
				"Content-Type" => "application/json",
				"Accept" => "application/json"
			]
		]);

		// throw new Exception(json_encode($cloudRequest));

		if (is_wp_error($response)) {
			$response = new stdClass();
			$response->success = false;
			$response->error = $response->get_error_message();
			return $response;
		}
		//		throw new Exception(json_encode($response));
		return json_decode($response['body']);
	}

	/**
	 * Summary of getStatus
	 * @param polylai_CloudStatus $request
	 */
	public static function getStatus($request)
	{
		$response = wp_remote_get(POLYLAI_WS_URL . "/status", [
			'timeout' => 100000,
			'method' => 'POST',
			'blocking' => true,
			'body' => json_encode($request),
			'headers' => [
				"Content-Type" => "application/json"
			]
		]);
		if (is_wp_error($response)) {
			//print_r($response);
			return 'error';
		}

		$response = json_decode($response['body']);
		if ($response->success) {
			return $response->status;
		}

		return [];
	}
}
