<?php

include_once( $path_to_root . "/modules/external/includes/enetonlinesolutions.php");
include_once( $path_to_root . "/modules/external/includes/AfricasTalkingGateway.php");
include_once( $path_to_root . "/modules/external/includes/db/notification_template_db.php");
include_once($path_to_root . "/admin/db/company_db.inc");

class SMS {

    public $gateway;

    public function __construct() {
        $prefs = get_company_prefs();
        if ($prefs['sms_gateway'] == 'AfricasTalkingGateway') {
            $this->gateway = new AfricasTalkingGateway($prefs['sms_api_username'], $prefs['sms_api_key']);
        } elseif ($prefs['sms_gateway'] == 'enetonlinesolutions') {
            $this->gateway = new enetonlinesolutions($prefs['sms_api_username'], $prefs['sms_api_key'], $prefs['sms_api_sender_id']);
        }
    }

    public function send($recipients, $message) {
        $results = array();
        try {
            $results = $this->gateway->sendMessage($recipients, $message);
            //display_warning(json_encode($results));
            return $results;
//            foreach ($results as $result) {
//                // status is either "Success" or "error message"
//                echo " Number: " . $result->number;
//                echo " Status: " . $result->status;
//                echo " MessageId: " . $result->messageId;
//                echo " Cost: " . $result->cost . "\n";
//            }
        } catch (Exception $e) {
            //display_warning("Encountered an error while sending: " . $e->getMessage());
            error_log("Encountered an error while sending: " . $e->getMessage());
            return $results;
        }
    }
    
    public function getAccountBalance() {
        try {
            $results = $this->gateway->getAccountBalance();
//            display_warning(json_encode($results));
            return $results;
        } catch (Exception $e) {
            error_log("Encountered an error while sending: " . $e->getMessage());
            return $results;
        }
    }

    public function send_template_notification($data) {
        $temp = get_template('sms', $data['trans_type']);
        $message = $this->bindContent($temp['body'], $data);
        if ($results = $this->send($data['phone_contacts'], $message)) {
            $_SESSION['sms_notification'] = $results;
        }
    }

    public function bindContent($string, $data = Array()) {
        $content = '';
        $variables = $this->extractAllVariables($string);
        if (count($variables[0]) > 0) {
            foreach ($variables[0] as $variable) {
                $var = str_replace('{', '', $variable);
                $var = str_replace('}', '', $var);
                //echo $var . ' : '. $data[$var] . '<br/>';
                if (isset($data[$var])) {
                    $content = str_replace($variable, $data[$var], $string);
                    $string = $content;
                } else {
                    $content = str_replace($variable, '--', $string);
                    $string = $content;
                }
            }
        } else {
            $content = $string;
        }
        $content = str_replace('\n', '<br/>', $content);
        //echo $content;
        return $content;
    }

    public function extractAllVariables($string) {
        $regex = '~\{([^}]*)\}~';
        $matches = array();
        preg_match_all('/{\w*}/', $string, $matches);
        //preg_match_all($regex, $string, $matches);
        return $matches;
    }

}
