<?php

namespace App\Http\Helpers;
use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class  Sender
{
    // var $host;
    // var $port;
    /*
        * Username that is to be used for submission
        */
    // var $strUserName;
    /*
        * password that is to be used along with username
        */
    // var $strPassword;
    /*
        * Sender Id to be used for submitting the message
        */
    // var $strSender;
    /*
        * Message content that is to be transmitted
        */
    // var $strMessage;
    /*
        * Mobile No is to be transmitted.
        */
    // var $strMobile;
    /*
        * What type of the message that is to be sent
        * <ul>
        * <li>0:means plain text</li>
        * <li>1:means flash</li>
        * <li>2:means Unicode (Message content should be in Hex)</li>
        * <li>6:means Unicode Flash (Message content should be in Hex)</li>
        * </ul>
        */
    // var $strMessageType;
    /*
        * Require DLR or not *
        <ul>
        * <li>0:means DLR is not Required</li>
        * <li>1:means DLR is Required</li>
        * </ul>
        */

    // var $strDlr;
    // $obj = new Sender("IP","Port","","","Tester"," "ةي برع لا ","xxxxxxx,"2","1");


    private function sms_unicode($message)
    {
        $hex1 = '';
        if (function_exists('iconv')) {
            $latin = @iconv('UTF-8', 'ISO-8859-1', $message);
            if (strcmp($latin, $message)) {
                $arr = unpack('H*hex', @iconv(
                    'UTF-8',
                    'UCS-2BE',
                    $message
                ));
                $hex1 = strtoupper($arr['hex']);
            }
            if ($hex1 == '') {
                $hex2 = '';
                $hex = '';
                for ($i = 0; $i < strlen($message); $i++) {
                    $hex = dechex(ord($message[$i]));
                    $len = strlen($hex);
                    $add = 4 - $len;
                    if ($len < 4) {
                        for ($j = 0; $j < $add; $j++) {
                            $hex = "0" . $hex;
                        }
                    }
                    $hex2 .= $hex;
                }
                return $hex2;
            } else {
                return $hex1;
            }
        } else {
            print 'iconv Function Not Exists !';
        }
    }


    public static function sendSmsRouteMobile($host, $port, $sender, $message, $mobile, $msgType, $dlr)
    {
        // $this->host = $host;
        // $this->port = $port;
        // $this->strUserName = $username;
        // $this->strPassword = $password;
        // $this->strSender = $sender;
        // $this->strMessage = $message; //URL Encode The Message..
        // $this->strMobile = $mobile;
        // $this->strMessageType = $msgtype;
        // $this->strDlr = $dlr;
        // $obj = new Sender("IP","Port","","","Tester"," "ةي برع لا ","xxxxxxx,"2","1");

        $universite = DB::connection('tenant')
            ->table('etablissement')->select('idUniversite')->where('idEtablissement', '=', Session::get('idEtablissement'))->get()->all();
        // $universite = $bdd->prepare('SELECT idUniversite FROM etablissement WHERE idEtablissement=?');
        // $universite->execute(array($_SESSION['idEtablissement']));
        // $idUniv = $universite->fetch();
        $idUniversite = $universite['idUniversite'];
        $username = '';
        $password = '';

        if ($idUniversite== 1) {
            $username = 'sonou';
            $password = 'Iitech2016';
        }elseif ($idUniversite== 2) {
            $username = 'hecmafrica';
            $password = 'Iitech2016';
        }else {
            $username = 'iitech';
            $password = 'goumidro2007';
        }

        if ($msgType == "2" || $msgType == "6") {
            //Call The Function Of string To HEX.
            // $message = $this->sms_unicode($message);
            try {
                //Smpp http Url to send sms.
                $live_url = "http://" . $host . ":" . $port . "/bulksms/bulksms?username=" . $username . "&password=" . $password . "&type=" . $msgType . "&dlr=" . $dlr . "&destination=" . $mobile . "&source=" . $sender . "&message=" . $message . "";
                $parse_url = file($live_url);
                echo $parse_url[0];
            } catch (Exception $e) {
                echo 'Message:' . $e->getMessage();
            }
        } else {
            $message = urlencode($message);
            try {
                // http Url to send sms.
                $live_url = "http://" . $host . ":" . $port . "/bulksms/bulksms?username=" . $username . "&password=" . $password . "&type=" . $msgType . "&dlr=" . $dlr . "&destination=" . $mobile . "&source=" . $sender . "&message=" . $message . "";
                $parse_url = file($live_url);
                echo $parse_url[0];
            } catch (Exception $e) {
                echo 'Message:' . $e->getMessage();
            }
        }
    }
};
