<?php namespace App\Modules\Server;

use View, Lang, App, Route, Alert;


/**
 * Esta clase se encarga de gestion todas las transacciones con el servidor remoto.
 */
class Server {

	// Server information
	protected $server_base_url 	= 'http://187.141.174.110/';
	protected $server_user 		= 'admin-api';
	protected $server_pass 		= 'v4nw0rmer4p1**!';
	protected $response;


	public function __construct()
	{

	}

	public function checkAvailability($hotel_code, $room_code, $checkin, $checkout)
	{
		$this->response = $this->get('availability/' . $hotel_code . '/' . $room_code . '/' . $checkin . '/' . $checkout);
		$this->checkResult();
		return $this->response;
	}

	public function getHotelRooms($hotel_code)
	{
		$this->response = $this->get('rooms/' . $hotel_code);
		$this->checkResult();
		return $this->response;
	}

	public function makeReservation($data)
	{
		$this->response = $this->post('make-reservation', $data);
		$this->checkResult();
		return $this->response;
	}
	
	public function confirmReservation($res_code)
	{
		$this->response = $this->post('confirm-reservation', ['res_code' => $res_code]);
		$this->checkResult();
		return $this->response;
	}


	/*
	public function getAllHotels()
	{
		return $this->get('available-hotels');
	}

	


	public function getAvailableHotelRooms($hotel_code, $checkin, $checkout)
	{
		return $this->get('rooms/' . $hotel_code . '/' . $checkin . '/' . $checkout);
	}


	public function getRoom($room_code, $hotel_code)
	{
		return $this->get('room/' . $room_code . '/' . $hotel_code);
	}

	*/


	/**
	 * Do an action with GET method
	 * @param  [type] $action_url URL of action
	 * @return [type] string Json
	 */
	protected function get($action_url)
	{
		try
		{
			$curl = curl_init();
			curl_setopt_array($curl, array(
			    CURLOPT_RETURNTRANSFER 	=> 1,
			    CURLOPT_URL 			=> $this->server_base_url . $action_url,
			    CURLOPT_USERAGENT 		=> '',
			    CURLOPT_USERPWD 		=> $this->server_user . ':' . $this->server_pass
			));
			$resp = curl_exec($curl);
			curl_close($curl);
		}
		catch(\Exception $e)
		{
			return $this->showError($e);
		}


		return json_decode($resp);
	}

	/**
	 * Do an action with POST method
	 * @param  [type] $action_url URL of action
	 * @return [type] string Json
	 */
	protected function post($action_url, $data)
	{
		try
		{			
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_USERAGENT, '');
			curl_setopt($ch, CURLOPT_URL, $this->server_base_url . $action_url);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_USERPWD, $this->server_user . ':' . $this->server_pass);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
			$result = curl_exec ($ch);
			curl_close ($ch); 
		}
		catch(\Exception $e)
		{
			return $this->showError($e);
		}

		return json_decode($result);
	}

	protected function showError($error)
	{
		return json_encode(['error' => true]);
	}


	protected function checkResult()
	{
		/*if(!isset($this->response) or !isset($this->response->success) or !$this->response->success)
		{
			Alert::message(trans('principal::reservation.server_error'), 'danger');
			return \Redirect::to('hotels')->withInput();
		}*/
	}
}