<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Api_Controller extends MY_Controller
{

    private $PARAM_RECEIVING = 1;
    private $PARAM_PICKUP = 2;
    private $PARAM_RETURN = 3;
    private $PARAM_FINISHED = 4;


    protected $user_data;
    protected $user_list;
    protected $cat_list;
    protected $store_list;
    protected $country_list;

    public function __construct()
    {
        parent::__construct();

        if ($this->router->method != "GetNotification") {
            logFile($this->router->class . "/" . $this->router->method, json_encode($this->input->post()));
        }
    }

    protected function getTypeString($type){
        if ($type == $this->PARAM_RECEIVING) {
            $val_type = TYPE_RECEIVING;
        } elseif ($type == $this->PARAM_RETURN) {
            $val_type = TYPE_RETURN;
        } elseif ($type == $this->PARAM_FINISHED) {
            $val_type = TYPE_FINISHED;
        } elseif ($type == $this->PARAM_PICKUP) {
            $val_type = TYPE_PICKUP;
        } else {
            return null;
        }
        return $val_type;
    }

    protected function SuccessResponse($data = array('message' => ''))
    {
        header('Content-Type: application/json');
//        echo json_encode(array('response' => $data));
        echo json_encode($data);
        die();
    }

    protected function TextResponse($data)
    {
        header('Content-Type: application/json');
//        echo json_encode(array('response' => $data));
        echo json_encode(array(
            'message' => $data
        ));
        die();
    }

    protected function FailedResponse($message)
    {
        header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
        header('Content-Type: application/json');
        echo json_encode(array(
            'message' => $message,
            'code' => 500
        ));
        die();
    }

    protected function sendMessage($title, $body, $group)
    {
        $fields = array(
            'notification' => array(
                'title' => $title,
                'body' => $body
            ),
            'to' => "/topics/" . $group
        );

        $headers = array(
            'Authorization: key=' . 'AIzaSyDhYg_g_HXHM-L-Hn_fY7WTNEWIPAwQIuw',
            'Content-Type: application/json'
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($ch);
        if ($result === FALSE) {
            return false;
        }
        curl_close($ch);
        return $result;
    }


 protected function callUrl($url, $has_return = true){
        $ch = curl_init();


       
        $header = array('User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36');
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, $has_return);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        // curl_setopt($ch, CURLOPT_HEADER, false);
     

        $data = curl_exec($ch);

        curl_close($ch);
	return $data;

   
    }




}