var system = require("../system")
var RPCClient = require('@alicloud/pop-core').RPCClient;

class AliApiClient {
    constructor() {
        this.client = new RPCClient({
            accessKeyId: 'LTAI4FvNyYTUtvfUsuoMzy7D',
            accessKeySecret: '1B4YqlWxNUtMpifbtmExQwY7m3Caj4',
            endpoint: 'https://trademark.aliyuncs.com',
            apiVersion: '2018-07-24'
        });
    }

    async reqbyget(obj,cbk) {
        var self = this;
        var action = obj.action;
        var reqbody = obj.reqbody;
        return self.client.request(action, reqbody, {
            timeout: 3000, // default 3000 ms
            formatAction: true, // default true, format the action to Action
            formatParams: true, // default true, format the parameter name to first letter upper case
            method: 'GET', // set the http method, default is GET
            headers: {}, // set the http request headers
        });
    }
    async reqbypost(obj,cbk) {
        var self = this;
        var action = obj.action;
        var reqbody = obj.reqbody;
     
        return self.client.request(action, reqbody, {
            timeout: 3000, // default 3000 ms
            formatAction: true, // default true, format the action to Action
            formatParams: true, // default true, format the parameter name to first letter upper case
            method: 'POST', // set the http method, default is GET
            headers: {}, // set the http request headers
        });
    }



    //-----------阿里云商标交易服务单 双呼能力接口接入------start--------chensicong----2022.02.24----------------------

    // 商标交易（商标优选） 双呼客户端
    getAliDoubleCallClientForTMTRADE() {
        return new RPCClient({
            accessKeyId: 'LTAI4FmyipY1wuLHjLhMWiPa', // 对应的aliuid 是 1525276744866147
            accessKeySecret: 'hp4FF18IDCSym1prqzxrAjnnhNH3ju',
            endpoint: "https://companyreg.aliyuncs.com",
            apiVersion: "2020-03-06"
        });
    }

    // 获取可用外呼号码列表
    async queryAvailabelNumbers(){
        let res = null;
        try{
            console.log(`开始调用阿里云-获取商标交易可用外呼号码列表接口。`)
            let reqAliclient = this.getAliDoubleCallClientForTMTRADE()
            let obj = { bizType:"TM_TRADE" }
            res = await reqAliclient.request("QueryAvailableNumbers", obj, {
                timeout: 3000, // default 3000 ms
                formatAction: true, // default true, format the action to Action
                formatParams: true, // default true, format the parameter name to first letter upper case
                method: 'POST', // set the http method, default is GET
                headers: {}, // set the http request headers
            });
            console.log(`结束调用阿里云-获取商标交易可用外呼号码列表接口。结果=${JSON.stringify(res)}`)
            return system.getResultSuccess(res.Data||res.data);
        }catch (e){
            console.log(`调用异常：阿里云-获取商标交易可用外呼号码列表接口。结果=${JSON.stringify(res)},异常信息=${e.stack}`)
            return system.getResultFail(-200, "api调用出现异常，error：" + e.stack);
        }
    }

    /**
     * 发起双呼
     * @param reqbody
     * @returns {Promise<{msg: *, data: *, status: *}|{msg: *, data, status: number}>}
     */
    async startBackToBackCall(reqbody){
        let res = null;
        try{
            console.log(`开始调用阿里云-发起双呼接口。参数reqbody`,reqbody)

            if(!reqbody.bizType){
                return system.getResultFail(-200, "bizType不能为空");
            }

            let reqAliclient = this.getAliDoubleCallClientForTMTRADE()

            res = await reqAliclient.request("StartBackToBackCall",reqbody, {
                timeout: 10000, // default 3000 ms 2020 0916 lin修改3000为10000，原因ConfirmIcpIntention BizIds 超过5条会超时
                formatAction: true, // default true, format the action to Action
                formatParams: true, // default true, format the parameter name to first letter upper case
                method: 'POST', // set the http method, default is GET
                headers: {}, // set the http request headers
            });
            console.log(`结束调用阿里云-发起双呼接口。参数reqbody=`,reqbody,'结果=',JSON.stringify(res))
            return system.getResultSuccess(res.Data||res.data);
        }catch (e){
            console.log(`调用异常：阿里云-发起双呼接口。参数reqbody=`,reqbody,'结果=',JSON.stringify(res),`异常信息=${e.stack}`)
            return system.getResultFail(-200,  JSON.stringify(e.data));
        }
    }

    //-----------阿里云商标交易服务单 双呼能力接口接入------end--------chensicong----2022.02.24----------------------
}

module.exports = AliApiClient;