"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InteliquentSMS = void 0;
const APISMSBase_1 = require("./APISMSBase");
const RawView_1 = require("../lib/view/RawView");
const numberApi_1 = require("./numberApi");
const Session_1 = require("../lib/model/Session");
const RawNonQuery_1 = require("../lib/model/RawNonQuery");
const RawQuery_1 = require("../lib/model/RawQuery");
const _406_1 = require("../lib/view/406");
const Str = require('@supercharge/strings');
class InteliquentSMS extends APISMSBase_1.SMS {
    constructor() {
        super();
        this.key = "eFluR1hOVERZZGY3dHc5Tno4dzFaemhtbFpnQzpEendnQjZCMVVKc2JwaUZxRXBieW9MODY0dFR3";
    }
    SendSms(req, res, next, Data, sms_provider, callback) {
        let sessdata = Data.sessdata;
        let sdata = req.body;
        let TO = "" + sdata.TO + "";
        let len = TO.length - 10;
        let number_type = this.getType(sdata.TO);
        const uuid = Str.random(30);
        var npa = TO.slice(len, len + 3);
        let iQry = "SELECT `MT_rates` FROM `sms_rate` WHERE `id_tariff`='" + sessdata.idtariff + "'  AND `subcategory`='SMS' LIMIT 1;\n\n";
        iQry += "SELECT loc_state FROM `did_location_info` WHERE `npa`='" + npa + "' LIMIT 1;";
        let obj = new RawQuery_1.ModelRawQuery(req, res);
        obj.qrysql = iQry;
        obj.prepare();
        obj.execute((err, resp) => {
            let MT_rates = Array.isArray(resp[0]) && resp[0].length > 0 ? resp[0][0].MT_rates : 0;
            let loc_state = Array.isArray(resp[1]) && resp[1].length > 0 ? resp[1][0].loc_state : "";
            // To check MT Rates
            if (MT_rates > 0) {
                // To check DID state
                if (loc_state) {
                    let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                    obj.nonqrysql = "INSERT INTO `mdr` (`batch_id`, `to_number`, `MT_rates`,`from_number`, `messsage_body`,`status`,`npa`,`state`,`idaccount`,`iduser`,`number_type`) VALUES('" + uuid + "','" + sdata.TO + "','" + MT_rates + "','" + sdata.FROM + "','" + sdata.BODY + "','dispatched','" + npa + "','" + loc_state + "','" + sessdata.idaccount + "','" + sessdata.iduser + "','" + number_type + "')";
                    obj.prepare();
                    obj.execute((error, responce) => {
                        let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                        obj.nonqrysql = "INSERT INTO `send_sms` (`sender`, `receiver`,`msgdata`,`time`,`smsc_id`,`dlr_mask`, `service`) VALUES('" + sdata.FROM + "','" + sdata.TO + "','" + sdata.BODY + "',UNIX_TIMESTAMP(),'sinch','11','" + responce.insertId + "')";
                        obj.prepare();
                        obj.execute((msg_err, msg_resp) => {
                            if (msg_err == 1) {
                                let objv = new RawView_1.RawView(res);
                                objv.prepare({ status: 201, result: "SMS Submit Successfully", batch_id: uuid });
                                objv.execute();
                            }
                            else {
                                let objv = new RawView_1.RawView(res);
                                objv.prepare({ result: "somthing went wrong", status: 502 });
                                objv.execute();
                            }
                        });
                    });
                }
                else {
                    let objv = new RawView_1.RawView(res);
                    objv.prepare({ result: "DID State Not Found!", status: 502 });
                    objv.execute();
                }
            }
            else {
                let objv = new RawView_1.RawView(res);
                objv.prepare({ result: "MT Rates Not Found!", status: 502 });
                objv.execute();
            }
        });
    }
    SendMms(req, res, next, callback) {
        let sdata = req.body;
        let TO = "" + sdata.TO + "";
        let len = TO.length - 10;
        let number = new numberApi_1.Number();
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((error, sessdata) => {
            if (error == 1) {
                number.verifyDid(req, res, (error, responce) => {
                    if (responce == -1) {
                        let obj = new RawView_1.RawView(res);
                        obj.prepare({ "message": "Invalid DID" });
                        obj.execute();
                    }
                    else {
                        let number_type = this.getType(sdata.TO);
                        this.getRate(req, res, sessdata.idtariff, number_type, "SMS", (sms_error, sms_rate) => {
                            if (sms_rate) {
                                var npa = TO.slice(len, len + 3);
                                let obj = new RawQuery_1.ModelRawQuery(req, res);
                                obj.qrysql = "SELECT loc_state FROM `did_location_info` WHERE `npa`='" + npa + "' LIMIT 1";
                                obj.prepare();
                                obj.execute((err, resp) => {
                                    if (resp.length > 0) {
                                        let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                                        obj.nonqrysql = "INSERT INTO `mdr` (`batch_id`, `to_number`, `MT_rates`,`from_number`, `messsage_body`,`status`,`npa`,`state`,`idaccount`,`iduser`,`number_type`,`connection_key`,`sms_outbound_key`) VALUES('','" + sdata.TO + "','" + sms_rate[0].MT_rates + "','" + sdata.FROM + "','" + sdata.BODY + "','dispatched','" + npa + "','" + resp[0].loc_state + "','" + sessdata.idaccount + "','" + sessdata.iduser + "','" + number_type + "','" + sdata.Connection + "','" + sdata.OutboundSMS + "')";
                                        obj.prepare();
                                        obj.execute((error, responce) => {
                                            let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                                            obj.nonqrysql = "INSERT INTO `send_sms` (`sender`, `receiver`,`msgdata`,`time`,`smsc_id`,`dlr_mask`, `service`) VALUES('" + sdata.FROM + "','" + sdata.TO + "','" + sdata.BODY + "',UNIX_TIMESTAMP(),'sinch','11','" + responce.insertId + "')";
                                            obj.prepare();
                                            obj.execute((msg_err, msg_resp) => {
                                                if (msg_resp.insertId) {
                                                    let objv = new RawView_1.RawView(res);
                                                    objv.prepare({ result: "message sumbit successfully" });
                                                    objv.execute();
                                                }
                                                else {
                                                    let objv = new RawView_1.RawView(res);
                                                    objv.prepare({ result: "somthing went wrong" });
                                                    objv.execute();
                                                }
                                            });
                                        });
                                    }
                                    else {
                                        callback(err, '');
                                    }
                                });
                            }
                            else {
                                callback(sms_error, '');
                            }
                        });
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare("No session data there");
                objv.execute();
            }
        });
    }
    getSMS(from, to, content, callback) {
    }
    getShortCode(short, callback) {
    }
    SendSms_Old(req, res, next, callback) {
        let sdata = req.body;
        let TO = "" + sdata.TO + "";
        let len = TO.length - 10;
        let number = new numberApi_1.Number();
        let session = new Session_1.SessionManagment(req, res, next);
        session.GetSession((error, sessdata) => {
            if (error == 1) {
                number.verifyDid(req, res, (error, responce) => {
                    if (responce == -1) {
                        let obj = new RawView_1.RawView(res);
                        obj.prepare({ "message": "Invalid DID" });
                        obj.execute();
                    }
                    else {
                        let number_type = this.getType(sdata.TO);
                        this.getRate(req, res, sessdata.idtariff, number_type, "SMS", (sms_error, sms_rate) => {
                            if (sms_rate) {
                                var npa = TO.slice(len, len + 3);
                                let obj = new RawQuery_1.ModelRawQuery(req, res);
                                obj.qrysql = "SELECT loc_state FROM `did_location_info` WHERE `npa`='" + npa + "' LIMIT 1";
                                obj.prepare();
                                obj.execute((err, resp) => {
                                    if (resp.length > 0) {
                                        let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                                        obj.nonqrysql = "INSERT INTO `mdr` (`batch_id`, `to_number`, `MT_rates`,`from_number`, `messsage_body`,`status`,`npa`,`state`,`idaccount`,`iduser`,`number_type`) VALUES('','" + sdata.TO + "','" + sms_rate[0].MT_rates + "','" + sdata.FROM + "','" + sdata.BODY + "','dispatched','" + npa + "','" + resp[0].loc_state + "','" + sessdata.idaccount + "','" + sessdata.iduser + "','" + number_type + "')";
                                        obj.prepare();
                                        obj.execute((error, responce) => {
                                            let obj = new RawNonQuery_1.ModelRawNonQuery(req, res);
                                            obj.nonqrysql = "INSERT INTO `send_sms` (`sender`, `receiver`,`msgdata`,`time`,`smsc_id`,`dlr_mask`, `service`) VALUES('" + sdata.FROM + "','" + sdata.TO + "','" + sdata.BODY + "',UNIX_TIMESTAMP(),'sinch','11','" + responce.insertId + "')";
                                            obj.prepare();
                                            obj.execute((msg_err, msg_resp) => {
                                                if (msg_resp.insertId) {
                                                    let objv = new RawView_1.RawView(res);
                                                    objv.prepare({ result: "message sumbit successfully", status: 201 });
                                                    objv.execute();
                                                }
                                                else {
                                                    let objv = new RawView_1.RawView(res);
                                                    objv.prepare({ result: "somthing went wrong", status: 502 });
                                                    objv.execute();
                                                }
                                            });
                                        });
                                    }
                                    else {
                                        callback(err, '');
                                    }
                                });
                            }
                            else {
                                callback(sms_error, '');
                            }
                        });
                    }
                });
            }
            else {
                let objv = new _406_1.Res406(res);
                objv.prepare("No session data there");
                objv.execute();
            }
        });
    }
}
exports.InteliquentSMS = InteliquentSMS;
