import ocpp from '../../http/api/ocpp'
import chargeProfile from '../../http/api/chargingProfile'

const operation = {
  getOperationParams: (operation, checkedList) => {
    return new Promise(async (resolve, reject) => {
      const result = {
        idTags: [],
        idTagAll: [],
        connectorIds: [],
        connectorActIds: [],
        needConnector: false,
        needActConnector: false,
        selectMultConfig: false,
        confKeyOption: [],
        keyTypeOption: [],
        availTypeOption: [],
        resetTypeOption: [],
        filterTypeOption: [],
        needCustomCoinfig: false,
        needChargingProfile: false,
        chargingProfileOption: [],
        chargingPurposeOption: [],
        needChargingProfilePurpose: false
      }
      switch (operation.value) {
        case 'CA': { //Change Availability
          result.needConnector = true;
          let res = await ocpp.getAvailabilityTypeList();
          if (res.success) {
            result.availTypeOption = res.data
          }
          break;
        }
        case 'CC': { //Change Configuration
          let res1 = await ocpp.getKeyTypeList();
          if (res1.success) {
            result.keyTypeOption = res1.data
          }
          let res2 = await ocpp.getConfigurationKeyList();
          if (res2.success) {
            const list = []
            for (let i in res2.data) {
              list.push({
                name: res2.data[i],
                value: i
              })
            }
            result.confKeyOption = list;
          }
          break;
        }
        case 'GC': { //Get Configuration
          result.selectMultConfig = true;
          let res = await ocpp.getConfigurationKeyList();
          if (res.success) {
            const list = []
            for (let i in res.data) {
              list.push({
                name: res.data[i],
                value: i
              })
            }
            result.confKeyOption = list;
          }
          break;
        }
        
        case 'RSTART': { //Remote Start Transaction
          result.needConnector = true;
          let res = await ocpp.getActiveIdTags()
          if (res.success) {
            const list = []
            res.data.forEach(item => {
              list.push({
                name: item.name,
                value: JSON.stringify({
                  phone: item.name,
                  idTag: item.value
                })
              })
            })
            result.idTagAll.push(...list);
            result.idTags.push(...list);
          }
          break;
        }
        case 'RSTOP': { //Remote Stop Transaction
          result.needActConnector = true;
          break;
        }
        case 'RS': { //Reset
          let res = await ocpp.getResetTypeList()
          if (res.success) {
            result.resetTypeOption = res.data;
          }
          break;
        }
        case 'UC': { //Unlock Connector
          result.needConnector = true;
          break;
        }
        case 'CCP': { //Clear Charging Profile
          result.needConnector = true;
          result.needChargingProfile = true;
          result.needChargingProfilePurpose = true;
          let res = await ocpp.getClearChargingProfileFilterType()
          if (res.success) {
            result.filterTypeOption = res.data;
          }
          let ress = await ocpp.getChargingProfileList()
          if (ress.success) {
            result.chargingProfileOption = ress.data
          }
          let res1 = await chargeProfile.getPurposeOptionList();
          if (res.success) {
            result.chargingPurposeOption = res1.data;
          }
          break;
        }
        case 'SCP': {//Set Charging Profile
          result.needConnector = true;
          result.needChargingProfile = true;
          let res = await ocpp.getChargingProfileList()
          if (res.success) {
            result.chargingProfileOption = res.data
          }
          break;
        }
      }
      /*if (result.needConnector && checkedList && checkedList.length > 0) {
        let ress = await ocpp.getConnectorIds(checkedList[0].stationId);
        if (ress.success) {
          result.connectorIds = ress.data;
        }
      }*/
      resolve(result);
    })
  },
  getConnectorIds: (stationIds) => {
    return new Promise(async (resolve, reject) => {
      if (stationIds && stationIds.length > 0) {
        let ress = await ocpp.getConnectorIds(stationIds[0]);
        if (ress.success) {
          resolve(ress.data);
        } else {
          resolve([]);
        }
      } else {
        resolve([]);
      }
    })
  },
  getActiveConnectorIds: (stationIds) => {
    return new Promise(async (resolve, reject) => {
      if (stationIds && stationIds.length > 0) {
        let ress = await ocpp.getActiveConnectorIds(stationIds[0]);
        if (ress.success) {
          resolve(ress.data);
        } else {
          resolve([]);
        }
      } else {
        resolve([]);
      }
    })
  },
  filterMobile: (mobile, back) => {
    ocpp.getActiveIdTags({phone: mobile}).then(res => {
      const list = []
      if (res.success) {
        res.data.forEach(item => {
          list.push({
            name: item.name,
            value: JSON.stringify({
              phone: item.name,
              idTag: item.value
            })
          })
        })
      }
      back(list)
    }).catch(err => {
      back([])
    })
  }
}

export default operation;