<?php

class Biztalk {

	private $smsurl = 'https://www.biztalk-api.com/v2/kko/sendAlimTalk';
    private $resulturl = 'https://www.biztalk-api.com/v2/kko/getResultAll';
	private $token_url = 'https://www.biztalk-api.com/v2/auth/getToken';
    private $bsid = "esmotors2300";
	private $passwd = "d8ad437905f0e52f147ae055c3690b731e5345b4";
    private $schema = "";
    private $accessToken = "";
	private $destinations = array();

	function __construct() {
	}
	/*
		Constructor
		@param
	*/
	function Biztalk(){

	}

	/*
		Set the devices to send to
		@param $deviceIds array of device tokens to send to
	*/
    function setDestination($destinations){
	    $dest = array();

        if(!is_array($destinations)){
            $destinations = array($destinations);
        }

        foreach ($destinations as $key => $value) {
            $dest[]['to'] = replace_phone_infobank($value);
        }
        // print_r($dest);
        // print_r($destinations);
		$this->destinations = $dest;

	}

    function setToken($token){
        // $this->accessToken = $token;
    }
    /*
		Send the message to the device
		@param $message The message to send
		@param $data Array of data to accompany the message
	*/
	function getToken(){

		$headers = array(
            //'X-IB-Client-Id: ' . $this->serviceId,
			//'X-IB-Client-Passwd: ' . $this->servicePassword,
            'Content-Type: application/json',
			'Accept: application/json'
		);

        $data = "{ 'bsid' : $this->bsid, 'passwd' => $this->passwd }";

		// Open connection
		$ch = curl_init();

		// Set the url, number of POST vars, POST data
		curl_setopt( $ch, CURLOPT_URL, $this->token_url );

		curl_setopt( $ch, CURLOPT_POST, true );
		curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );


        // echo json_encode(array($data));

		curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );

        $info = curl_getinfo($ch);
		// Execute post
		$result = curl_exec($ch);

        // echo json_encode($data);
        // print_r($info);
        // var_dump($result);        //결과 값 출력
        // print_r(curl_getinfo($ch)); //모든 정보 출력

		// Close connection
		curl_close($ch);

        $result = json_decode($result, true);

        // $this->schema  = $result['schema'];
        $this->accessToken  = $result['token'];
	}

	/*
		Send the message to the device
		@param $message The message to send
		@param $data Array of data to accompany the message
	*/
	function sendSms($data){

		return ;
		
        $fields = array(
			'msgIdx'  => uniqid("upro"),
            'countryCode'   => "82"
		);
        // print_r($fields);
        // exit;
        $debug = "N";
        if($data['debug'] == "Y") {
            $debug = "Y";
            unset($data['debug']);
        }


		if(is_array($data)){
			foreach ($data as $key => $value) {
				$fields[$key] = $value;
			}
		}


		$headers = array(
			'bt-token:'.$this->accessToken,
            'Content-Type: application/json'
		);

        // print_r($fields);
        // exit;

		// Open connection
		$ch = curl_init();

		// Set the url, number of POST vars, POST data
		curl_setopt( $ch, CURLOPT_URL, $this->smsurl );

		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));

        $info = curl_getinfo($ch);
        $result = curl_exec($ch);
        if($debug == "Y") {
            echo json_encode($fields);
            print_r($info);
            var_dump($result);        //결과 값 출력
            print_r(curl_getinfo($ch)); //모든 정보 출력
        }
		// Execute post






        // echo curl_errno($ch);       //에러 정보 출력

        // echo '<br/><br/>';
		// Close connection
		curl_close($ch);

        $result = json_decode($result, true);

		return $result;
	}


	function sendResult($data){

		if(is_array($data)){
			foreach ($data as $key => $value) {
				$fields[$key] = $value;
			}
		}


		$headers = array(
			'bt-token:'.$this->accessToken,
            'Content-Type: application/json'
		);

        // print_r($fields);
        // exit;

		// Open connection
		$ch = curl_init();

		// Set the url, number of POST vars, POST data
		curl_setopt( $ch, CURLOPT_URL, $this->resulturl );

		curl_setopt( $ch, CURLOPT_POST, false );
		curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


        $info = curl_getinfo($ch);
        $result = curl_exec($ch);
        // echo json_encode($fields);
        print_r($info);
        print_r($result);        //결과 값 출력
        // print_r(curl_getinfo($ch)); //모든 정보 출력
		// Execute post






        // echo curl_errno($ch);       //에러 정보 출력

        // echo '<br/><br/>';
		// Close connection
		curl_close($ch);

        $result = json_decode($result, true);

		return $result;
	}

	function error($msg){
		echo "Android send notification failed with error:";
		echo "\t" . $msg;
		exit(1);
	}
}
