<?php

namespace SMSGateway\Gateway;

class Mimsms implements GatewayInterface
{
    protected $defaultConfig = array(
        'api_id'    => '',
        'auth_key'    => '',
        'sender_id'    => '',
        'username'    => '',
        'type'   => 'sms',
        //'url' => 'https://www.mimsms.com.bd/smsAPI',
        'url' => 'https://api.mimsms.com/api/SmsSending/SMS',
    );

    protected $apikey;
    protected $username;
    protected $apitoken;
    protected $senderid;
    protected $type;
    protected $url;

    public function __construct ($config = array()) 
    {
        //print_r($config);exit;
        extract(mergeArray($this->defaultConfig, $config));
        $this->username = $username;
        $this->apikey = $api_id;
        $this->apitoken = $auth_key;
        $this->senderid = $sender_id;
        $this->url =  $url;
    }

    public function send($to, $message) 
    {
        $to = '88'.preg_replace('/88/', '', str_replace(array('+',''), '', $to), 1); 
        // $postData = array(
        //     'sendsms' => $this->apikey,
        //     'apikey' => $this->apikey,
        //     'apitoken' => $this->apitoken,
        //     'type' => $this->type,
        //     'from' => $this->senderid,
        //     'to' => $to,
        //     'text' => $message,
        // );

        /**
         *{
    "ApiKey": "VTSJEXXXXXXXXXX9CL",
    "MobileNumber": "8801844XXXXXX",
    "SenderName": "iSMS",
    "CampaignName": "",
    "UserName": "abcd@example.com",
    "TransactionType": "T",
    "MessageId": "",
    "Message": "Welcome to MiM Digital",
    "CampaignId": "null",
    "SmsData": null
}
         */
        $postData = [
            'Apikey' => $this->apikey,
            'MobileNumber' => $to,
            'SenderName' =>$this->senderid,
            'CampaignName' =>'',
            'UserName' => $this->username,
            'TransactionType' =>'T',
            'MessageId' =>'',
            'Message' => $message,
            'CampaignId' =>"null",
            'SmsData' =>null
        ];
//echo json_encode($postData);exit;
        $ch = curl_init();

        curl_setopt_array($ch,[
        CURLOPT_URL => 'https://api.mimsms.com/api/SmsSending/SMS',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 20,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS =>json_encode($postData),
        CURLOPT_HTTPHEADER => [
            'Content-Type: application/json'
        ],
        ]
    );

        $response = curl_exec($ch);

        curl_close($ch);
//         echo $response;
// //print_r($postData);exit;
//         // init the resource
//         $ch = curl_init();
//         curl_setopt_array($ch,[
//             CURLOPT_URL => $this->url,
//             CURLOPT_RETURNTRANSFER => true,
//             CURLOPT_POST => true,
//             CURLOPT_POSTFIELDS => $postData,
//         ]);
//         //Ignore SSL certificate verification
//         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//         curl_setopt($ch, CURLOPT_TIMEOUT, 0);

//         $response = curl_exec($ch);

        $err = curl_error($ch);
        curl_close($ch);
        // var_dump($response);
        // var_dump($err);exit;
        if ($err) {
            return "cURL Error #:" . $err;
        } else {
            return $response;
        }
    }

    public function deliveryStatus($groupid)
    {
        $postData = array(
            'groupstatus' => '',
            'apikey' => $this->apikey,
            'apitoken' => $this->apitoken,
            'groupid' => $groupid,
        );

        // init the resource
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => $this->url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $postData,
        ));


        //Ignore SSL certificate verification
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);


        //get response
        $output = curl_exec($ch);

        if(curl_errno($ch))
        {
            echo 'error:' . curl_error($ch);
        }

        curl_close($ch);
        return $output;
    }

    public function getBalance()
    {
        $postData = array(
            'balance' => '',
            'apikey' => $this->apikey,
            'apitoken' => $this->apitoken,
        );

        // init the resource
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => $this->url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $postData,
        ));


        //Ignore SSL certificate verification
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);


        //get response
        $output = curl_exec($ch);

        if(curl_errno($ch))
        {
            return 'error:' . curl_error($ch);
        }

        curl_close($ch);
        $output = json_decode($output,true);
        return isset($output['balance']) ? floor($output['balance']) : 'Error!';
    }
}