import { setallLampHttp, getStatusHttp } from '@/services/sagacare_service/equipment';
// import { message } from 'antd';

// 定时调用 查询指令接口
export const checkChangeLightStatusSuccess = (paramsArr, resultArr, fn) => {
    let checkParams = paramsArr;
    checkParams.forEach((item) => {
        for (let i = 0; i < resultArr.length; i++) {
            if ((item.id = resultArr[i].id)) {
                item.orderSeqNum = resultArr[i].orderSeqNum;
            }
        }
    });
    getStatusHttp(checkParams).then((res) => {
        judgeChangeResponeseSuccess(res, paramsArr, fn);
    });
};

//   检查开关指令下发状态
export const judgeChangeResponeseSuccess = (response, paramsArr, fn) => {
    let resFlag = false;
    let resultArr = JSON.parse(JSON.stringify(response.content));
    resultArr &&
        resultArr.forEach((item) => {
            if (
                (item.state === 200 && item.exeResult === 'processing:rcvd') ||
                (item.state === 202 && !item.exeResult)
            ) {
                resFlag = true;
            } else {
                resultArr.splice(item, 1);
            }
        });
    // if (resFlag) {
    //     setTimeout(() => {
    //         checkChangeLightStatusSuccess(paramsArr, resultArr, fn);
    //     }, 500);
    // } else {
    fn && fn(); // 查询灯设备
    // }
    /*
    if (result.result === 'success') {
      if (!resFlag && result.state === 200 && result.exeResult === 'success') {
        console.log('成功了');
        fn(); // 查询灯设备
      } else if (
        (resFlag && result.state === 200 && result.exeResult === 'processing:rcvd') ||
        (resFlag && result.state === 202 && !result.exeResult)
      ) {
        setTimeout(() => {
          checkChangeLightStatusSuccess(paramsArr, resultArr, fn);
        }, 500);
      } else if (result.state === 200 && result.exeResult === 'fail:timeout') {
        message.error('网络超时，请重试');
      } else {
        message.error('操作失败，请重试');
      }
    } else {
      message.error('操作失败，请重试');
    }
    */
};

//  开关
export const setallLamps = (paramsArr, fn, waitSetResultFlag) => {
    //debugger;
    setallLampHttp(paramsArr).then((res) => {
        //debugger;
        waitSetResultFlag && (waitSetResultFlag.current = false);
        judgeChangeResponeseSuccess(res, paramsArr, fn);
    });
};

