<?php
defined('BASEPATH') OR exit('No direct script access allowed');
if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400'); 
    }

	if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
		if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
			header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

		if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
			header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

		exit(0);
	}

class Webservice extends CI_Controller {
	var $auth_token;

	public function __construct() {
		parent::__construct();
		$this->load->model('Webservice_model');		
		$this->load->model('Validation_app_model');
		$method = $this->router->fetch_method();				
		$data = (array) json_decode(file_get_contents('php://input'));
		if($method == 'profile') {
			$data = $_POST;
		}
		if (isset(apache_request_headers()['Auth'])) {
            $this->auth_token = apache_request_headers()['Auth'];
            $data['auth_token'] = $this->auth_token;
        }
		$res = $this->Validation_app_model->validation_check($method, $data);
		if($res['state'] == 1) {
 			$this->errorResponse($res['response']['code'], $res['response']['message']);
 			die;
 		} 		
 	}

 	public function login() {
 		$data = (array) json_decode(file_get_contents('php://input'));
 		$res = $this->Webservice_model->login($data);
 		if($res['status']!=0){
 			$this->response($res['data']);
 		}
 		else{
 			$this->errorResponse($res['code'],$res['message']);
 		}
 	}


 	public function check_email_availability() {
 		$data = (array) json_decode(file_get_contents('php://input'));
 		$res = $this->Webservice_model->availability($data);
 		if($res['status']!=0){
 			$this->response($res['data']);
 		}
 		else{
 			$this->errorResponse($res['code'],$res['message']);
 		}
 	}

 	public function registration(){
 		$data = (array) json_decode(file_get_contents('php://input'));
 		$res = $this->Webservice_model->register($data);
 		if($res['status']!=0){
 			$this->response($res['data']);
 		}
 		else{
 			$this->errorResponse($res['code'],$res['message']);
 		}
 	}

 	public function get_places_list($cat_id = null) {
 		$data['cat_id'] = $cat_id;
 		$data['auth_token'] = $this->auth_token;
 		$res = $this->Webservice_model->discover($data);
 		if($res['status']!=0){
 			$this->response($res['data']);
 		}
 		else{
 			$this->errorResponse($res['code'],$res['message']);
 		}
 	}

 	public function place_details($event_id = null) {
 		if($event_id == null) {
 			$this->errorResponse("ER16","Event id is null or empty");
 			die;
 		}
 		$data['event_id'] = $event_id;
 		$data['auth_token'] = $this->auth_token;
 		$res = $this->Webservice_model->event($data);
 		if($res['status']!=0){
 			$this->response($res['data']);
 		}
 		else{
 			$this->errorResponse($res['code'],$res['message']);
 		}
 	}

 	public function response($data) {
 		$result =  array(
 			'status' => 'Success',
 			'data' =>$data
 		);
 		print json_encode($result);
 	}

 	public function errorResponse($errorCode, $errorDesc) {
 		$result =  array(
 			'status' => 'error',
 			'error'=> $errorCode,
 			'message'=> $errorDesc
 		);
 		print json_encode($result);
 	}
}