<?php

defined('BASEPATH') or exit('No direct script access allowed');

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH . '/libraries/REST_Controller.php';

/**
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array
 *
 * @package         CodeIgniter
 * @subpackage      Rest Server
 * @category        Controller
 * @author          Hector Luna
 * @license         MIT
 * @link            https://github.com/chriskacerguis/codeigniter-restserver
 */
class Instagrambot extends REST_Controller
{
	function __construct()
	{
		header('Access-Control-Allow-Origin: *');
		header("Access-Control-Allow-Headers: Content-Type, Cache-Control");
		header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT");
		$method = $_SERVER['REQUEST_METHOD'];
		if ($method == "OPTIONS") {
			die();
		}

		parent::__construct();
		$this->load->model('Md_instagram');
	}


	public function ticket_post() {

		$post = $this->input->raw_input_stream;

		$cleanPost = $this->security->xss_clean($post);

		$cleanPost = json_decode($cleanPost);

		$data = $this-> Md_instagram->putTicket($cleanPost->cliente, $cleanPost->channel, $cleanPost->account_id, $cleanPost->msg, $cleanPost->key, $cleanPost->asunto, $cleanPost->chatId, $cleanPost->group);

		$this->set_response(['status' => TRUE, 'codigo' => 1, 'message' => 'All Data', 'data' => $data], REST_Controller::HTTP_OK);
		
	}

	public function messages_post() {

		$post = $this->input->raw_input_stream;
		$cleanPost = $this->security->xss_clean($post);
		$data = $this-> Md_instagram->getMessages( $cleanPost );

		$this->set_response(['status' => TRUE, 'codigo' => 1, 'message' => 'All Data', 'data' => $data], REST_Controller::HTTP_OK);
		
	}

	public function sendMessage_post() {

		$post = $this->input->raw_input_stream;
		$cleanPost = $this->security->xss_clean($post);
		$cleanPost = json_decode($cleanPost);
//$msg, $url, $token, $userID, $botones )
		if(isset($cleanPost->msg) 
		&& isset($cleanPost->url) 
		&& isset($cleanPost->token) 
		&& isset($cleanPost->userID)
		) {
			$data = $this-> Md_instagram->sendMessage( $cleanPost->msg, $cleanPost->url, $cleanPost->token, $cleanPost->userID, "" );
		} else {
			$data = FALSE;
		}
		
		$this->set_response(['status' => TRUE, 'codigo' => 1, 'message' => 'WhatsApp', 'data' => $data], REST_Controller::HTTP_OK);
		
	}
}
