import xhyc from '@/api/controlCenter/controlPanel/670'
import fys from '@/api/controlCenter/controlPanel/index'


var dic = [{
    typeNames: ['670'],//星汉的配置
    sendMsg: {
        cfg: ['sendMsg', 'deviceIds', 'deviceImeiList', 'instructValue', 'batchId'],
        method: xhyc.SendMessage
    },
    SOS: {

        cfg: ["deviceIds", "typeName", "batchId", "deviceImeiList", "instructValue"],
        method: xhyc.SosSettingBatch
    },
    sendImg: {
        cfg: [],
        method: null
    }
},
{
    typeNames: ['6170', '210'],//富源晟的配置
    sendMsg: {
        cfg: ["deviceIds", "typeName", "batchId", "deviceImeiList", "sendMsg"],
        method: fys.deviceSendMessage
    },
    SOS: {
        cfg: ["deviceIds", "typeName", "batchId", "deviceImeiList", "instructValue"],
        method: fys.sendAlarmMessage
    },
    sendImg: {
        cfg: [],
        method: null
    }
},
{
    typeNames: null,//如果找不到，以富源晟的配置为准
    sendMsg: {
        cfg: ["deviceIds", "typeName", "batchId", "deviceImeiList", "sendMsg"],
        method: fys.deviceSendMessage
    },
    SOS: {
        cfg: ["deviceIds", "typeName", "batchId", "deviceImeiList", "instructValue"],
        method: fys.sendAlarmMessage
    },
    sendImg: {
        cfg: [],
        method: null
    }
    
}
];

function getCfg(json, type) {
    let f = dic.find(v => {

        let flag = false;
        for (let index = 0; index < v.typeNames.length; index++) {
            let i = v.typeNames[index];
            if (i.toLowerCase().indexOf(json.typeName.toLowerCase()) > -1 || json.typeName.toLowerCase().indexOf(i.toLowerCase()) > -1) {
                flag = true;
                break;
            }
        }
        return flag;
    });

    if (!f) {
        f = dic.find(v => {
            return !v.typeNames;
        });
    }
    if (f) {
        let keys = f[type].cfg;
        let method = f[type].method;

        let formData = {};
        keys.forEach(key => {
            formData[key] = json[key];
        });

        return {
            formData: formData,
            method: method
        }


    }
    return null;
}

//发送信息
function deviceSendMessage(json) {
    let cfg = getCfg(json, 'sendMsg');
    if (cfg.method) {
        return cfg.method(cfg.formData);
    }
    return Promise.reject({ code: 500, msg: '配置中找不到该类型的设备' });

}

//强制报警
function sendAlarmMessage(json) {
    
 let cfg = getCfg(json, 'SOS');
    if (cfg.method) {
        return cfg.method(cfg.formData);
    }
    return Promise.reject({ code: 500, msg: '配置中找不到该类型的设备' });
}
export default {
    deviceSendMessage: deviceSendMessage,
    sendAlarmMessage: sendAlarmMessage
}
