<?php

class enetonlinesolutions {

    var $username;
    var $password;
    var $sender_id;

    function __construct($username, $password, $sender_id) {
        $this->username = $username;
        $this->password = $password;
        $this->sender_id = $sender_id;
    }

    function sendMessage($to, $text) {
        $token = base64_encode("{$this->username}:{$this->password}");
        $data = array("from" => $this->sender_id, "to" => $to, 'text' => $text);

        $data_string = json_encode($data);
        $ch = curl_init('https://sms.enetonlinesolutions.co.ke/sms/1/text/single');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Authorization: Basic ' . $token,
            'Content-Type: application/json',
            'Accept: application/json')
        );

        $results = json_decode(curl_exec($ch));
        $resonses = array();
        if (is_array($results)) {
            foreach ($results->messages as $message) {
                $class = new stdClass();
                $class->status = $message->status->groupName;
                $class->number = $message->to;
                $class->messageId = $message->messageId;
                $resonses[] = $class;
            }
        }

        return $resonses;
    }

    function getAccountBalance() {
        $token = base64_encode("{$this->username}:{$this->password}");
        $data = array("username" => $this->username, "password" => $this->password);

        $data_string = json_encode($data);
        $ch = curl_init('https://sms.enet.co.ke/account/1/balance');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        //curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Authorization: Basic ' . $token,
            'Content-Type: application/json',
            'Accept: application/json')
        );

        $result = curl_exec($ch);
        $bal = json_decode($result);
        return $bal;
    }

    function sendFullFeaturedSms($sheduled_sms_s, $schedule_time, $ref, $day) {
        $token = base64_encode("{$this->username}:{$this->password}");
        $data = new stdClass();
        $data->bulkId = 'BULK-ID-' . $ref;
        $data->messages = array();
        foreach ($sheduled_sms_s as $sheduled_sms) {
            $single_text = new stdClass();
            $single_text->from = $this->sender_id;
            $destinastions = new stdClass();
            $destinastions->to = $sheduled_sms['to'];
            $single_text->destinations = array($destinastions);
            $single_text->text = $sheduled_sms['text'];
            $single_text->sendAt = $schedule_time;
            //$single_text->deliveryTimeWindow = new stdClass();
//            $from = new stdClass();
//            $from->hour = 6;
//            $from->minute = 0;
//            $single_text->deliveryTimeWindow->from = $from;
//            $to = new stdClass();
//            $to->hour = 15;
//            $to->minute = 30;
//            $single_text->deliveryTimeWindow->to = $to;
//            $single_text->deliveryTimeWindow->days = Array(strtoupper($day));
            array_push($data->messages, $single_text);
        }

        $data_string = json_encode($data);
        //var_dump($data_string);
        $ch = curl_init('https://sms.enet.co.ke/sms/1/text/advanced');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Authorization: Basic ' . $token,
            'Content-Type: application/json',
            'Accept: application/json')
        );

        $result = curl_exec($ch);
        //var_dump(json_decode($result));
        //die();
        return json_decode($result);
    }

}
