<?php

/**
 * Panelchi SMS API Class - version 8.6.2
 */
if(!class_exists('digits_Panelchi')){
    class digits_Panelchi {
        private $apikey;
        private $sender;
        private $patterncode;
        private $patternvar;
        public function __construct($arg ,array $options = array()) {
            if (empty($arg)) {
                error_log("Username and Password and Sender can't be blank");
            }
            else {
                $this->apikey = trim($arg['apikey']);
                $this->sender = trim($arg['sender']);
                $this->patterncode = trim($arg['patterncode']);
                $patternvar_raw = trim($arg['patternvars']);
                $patternvar_raw = preg_replace('/\s+/', '', $patternvar_raw);
                if (preg_match('/^([^:=]+)/', $patternvar_raw, $matches)) {
                    $this->patternvar = trim($matches[1]);
                }
                else {
                    $this->patternvar = $patternvar_raw;
                }
            }
        }
        public function send(array $sms) {
            if (!is_array($sms)) {
                error_log("sms parameter must be an array");
            }
            if(!is_callable('curl_init')){
                die('CURL function does not exist in your server. contact your host service!');
            }
            if (substr($sms['message'], 0, 11) === "patterncode") {
                return $this->sendPattern($sms);
            }
            $params = [ "sending_type" => "webservice", "from_number" => $this->sender, "message"=> $sms['message'], "params"=> [ "recipients" => [ $sms['to'] ] ] ];
            $args = array(
                'method' => 'POST',
                'timeout' => 10,
                'headers' => array(
                    'Content-Type' => 'application/json',
                    'Authorization' => 'Bearer ' .$this->apikey ,
                ),
                'body' => json_encode(array(
                    'message' => str_replace(["{OTP}", "{NAME}", "{DOMAIN}"], [$sms['otp'], get_bloginfo('name'), get_home_url()], $sms['message']),
                    'recipients' => [$sms['to']],
                    'sourceNumber' => $this->sender,
                )),
            );
            // $response = wp_remote_get('https://api.panelchi.com/sms/pattern', $args);
            $response = wp_remote_get('https://api.panelchi.com/sms/send', $args);
            if (!is_wp_error($response)) {
                $response_decode = wp_remote_retrieve_body($response);
               return $response_decode;
            }
            return $response;
        }
        public function sendPattern(array $sms) {
            $params = [ "sending_type" => "pattern", "from_number" => $this->sender, "code" => $this->patterncode, "recipients" => [$sms['to']], "params" => [ $this->patternvar => $sms['otp'] ] ];
            $args = array(
                'method' => 'POST',
                'timeout' => 10,
                'headers' => array(
                    'Content-Type' => 'application/json',
                    'Authorization' => 'Bearer ' .$this->apikey ,
                ),
                'body' => json_encode(array(
                        'pattern' => $this->patterncode,
                        'variables' => [$this->patternvar => $sms['otp']],
                        'sourceNumber' => $this->sender,
                        'recipient' => $sms['to'],

                )),
            );
            $response = wp_remote_get('https://api.panelchi.com/sms/pattern', $args);
            if (!is_wp_error($response)) {
                $response_decode = wp_remote_retrieve_body($response);
                return $response_decode;
            }
            return $response;
        }
    }
}
