<?php

namespace App\Service;

use App\Core\Service\Logger;
use App\Service\Api;
use Exception;

class Dial
{
    //  ---------------------  SANIPPBX  ------------------
    public function callSANIPPBX(String $url, String $method, $headers, $postData, array $refData = [])
    {
        try {
            $postData['userfield'] = json_encode($refData);
            $api_headers = array('Authorization' => $headers['Authorization']);
            /******************** CALL CURL API ------------- STARTS ****************************/
            $api = new Api();
            $callrs = $api->call($url, $method, $api_headers, json_encode($postData));
            if (!$callrs['status']) {
                throw new Exception($callrs['error']);
            }
            /******************** CALL CURL API -------------- ENDS ****************************/
            $resp = json_decode($callrs['data']);
            if ($resp->status == 'failed') {
                throw new Exception($resp->msg);
            } else {
                return ['status' => true, 'code' => $resp->statuscode, 'msg' => $resp->msg, 'data' => $resp->id];
            }
        } catch (\Exception $e) {
            Logger::error($e->getMessage(), ['File' => $e->getFile(), 'Line' => $e->getLine()]);
            return ["status" => false, "error" => $e->getMessage()];
        }
    }
    //  ----------------------  EASYGOIVR  --------------------
    public function callEasyGoIVR(String $url, String $method, $headers, $postData, array $refData = [])
    {
        try {
            // URL : https://client.easygoivr.com
            // Request : {"username": "jagwant@sansoftwares.com","token": "13030c022047e3dc6c4e40f9f9887904"}
            $tokenResp = $this->genrateEasyGoIVRToken($url, $method, $headers);
            if(!$tokenResp['status']){
                throw new Exception($tokenResp['error']);
            }
            $postData['variable'] = $refData;
            /******************** CALL CURL API ------------- STARTS ****************************/
            $api = new Api();
            $api_headers = array('API_TOKEN' => $tokenResp['data'], 'Content-Type' => 'application/json');
            $callrs = $api->call($url . "/easygoapiJwt/request/dial", $method, $api_headers, json_encode($postData));
            $resp = json_decode($callrs['data']);
            if ($resp->status == 'failed' || $resp->code == 400) {
                throw new Exception($resp->msg);
            } else {
                return ['status' => true, 'code' => $resp->code, 'msg' => $resp->msg, 'data' => ''];
            }
            /******************** CALL CURL API -------------- ENDS ****************************/
        } catch (\Exception $e) {
            Logger::error($e->getMessage(), ['File' => $e->getFile(), 'Line' => $e->getLine()]);
            return ["status" => false, "error" => $e->getMessage()];
        }
    }
    //  ------------------  GENERATE TOKEN FOR EASYGOIVR  -------------------
    public function genrateEasyGoIVRToken($url, $method, $headers)
    {
        try {
            // base64_encode = amFnd2FudEBzYW5zb2Z0d2FyZXMuY29tOjEzMDMwYzAyMjA0N2UzZGM2YzRlNDBmOWY5ODg3OTA0
            /******************** CALL CURL API ------------- STARTS ****************************/
            $api = new Api();
            $api_headers = array('Authorization' => $headers['Authorization'], 'Content-Type' => 'application/json');
            $callrs = $api->call($url . "/masterapiJwt/gentoken", $method, $api_headers);
            if (!$callrs['status']) {
                throw new Exception($callrs['error']);
            }
            $resp = json_decode($callrs['data']);
            if ($resp->status == 'failed' || $resp->code == 400) {
                throw new Exception($resp->msg);
            } else {
                return ['status' => true, 'code' => $resp->code, 'msg' => $resp->msg, 'data' => $resp->API_TOKEN];
            }
            /******************** CALL CURL API -------------- ENDS ****************************/
        } catch (\Exception $e) {
            Logger::error($e->getMessage(), ['File' => $e->getFile(), 'Line' => $e->getLine()]);
            return ["status" => false, "error" => $e->getMessage()];
        }
    }
}
