import Http from "../utils/http";
import { host, isDebug } from "../utils/constant";
const cHttp = new Http(host);

export function startCharge(sn: string, query?: string) {
  return cHttp.get<{
    ConnectorID: string;
    FailReason: number;
    StartChargeSeq: string;
    StartChargeSeqStat: number;
    SuccStat: number;
  }>(`/charge/startCharge/${sn}?${query}`, {
    statusCodeHandle: false,
  });
}

export async function cancelAppointmentCharge() {
  return cHttp.get("/charge/cancelBooking");
}

export async function startAppointmentCharge(connectorId: string) {
  return cHttp.get(`/charge/immediatelyCharge/${connectorId}`);
}

export async function searchQRCode(imgUrl: string) {
  return cHttp.get(`/charge/qrCode?imgUrl=${imgUrl}`);
}

export async function changeAppointmentTime(
  startChargeSeq: string,
  startTime: string
) {
  return cHttp.get(
    `/charge/modifyBookingTime?startChargeSeq=${startChargeSeq}&startTime=${startTime}`
  );
}

export function fetchStationPriceDesc(ConnectorID: string, StationID?: string) {
  return cHttp.get(`/charge/businessPolicy/${ConnectorID}`).then((res) => {
    const nowHour = new Date().getHours();
    let maxPrice = 0;
    let minPrice = 9;
    let minPriceTime = "00:00";
    let currentPrice = 0;
    let currentTime = "00:00～24:00";
    res.useTime = "";
    if (res && res.policyInfos && res.policyInfos.length) {
      res.policyInfos.forEach((item: any, index: number) => {
        const hour = item.startTime.substring(0, 2);
        const min = item.startTime.substring(3, 5);
        if (index === 0) {
          res.useTime = `${hour}:${min}~24:00`;
        }
        let tempPrice = Number(
          Number(item.elecPrice + item.servicePrice).toFixed(2)
        );
        item.totalPrice = tempPrice;
        if (tempPrice > maxPrice) {
          maxPrice = tempPrice;
        }
        if (index >= res.policyInfos.length - 1) {
          // 最后一个
          item.startTimeFormat = `${hour}:${min}~24:00`;
          if (Number(hour) <= nowHour) {
            currentPrice = tempPrice;
            currentTime = `${hour}:${min}~24:00`;
          }
          if (tempPrice < minPrice) {
            minPrice = tempPrice;
            minPriceTime = `${hour}:${min}~24:00`;
          }
        } else {
          const nhour = res.policyInfos[index + 1].startTime.substring(0, 2);
          const nmin = res.policyInfos[index + 1].startTime.substring(3, 5);
          item.startTimeFormat = `${hour}:${min}~${nhour}:${nmin}`;
          if (nowHour >= Number(hour) && nowHour < Number(nhour)) {
            currentPrice = tempPrice;
            currentTime = `${hour}:${min}~${nhour}:${nmin}`;
          }
          if (tempPrice < minPrice) {
            minPrice = tempPrice;
            minPriceTime = `${hour}:${min}~${nhour}:${nmin}`;
          }
        }
      });
    }
    res.maxPrice = maxPrice;
    res.minPrice = minPrice;
    res.minPriceTime = minPriceTime;
    res.currentPrice = currentPrice;
    res.currentTime = currentTime;
    if (StationID) {
      res.StationID = StationID;
    }
    return res;
  });
}

export function cancelCharge(sn: string) {
  return cHttp.get(`/charge/stopCharge/${sn}`, {
    statusCodeHandle: false,
  });
}

export function fetchChargeStatus(
  checkAppointment?: boolean,
  checkCharge?: boolean
) {
  return cHttp
    .get("/charge/chargeStatus", {
      statusCodeHandle: false,
    })
    .then((res) => {
      if (res) {
        res.isAppointment = [0].includes(res.chargeStatus);
        res.isStarted = [1, 2, 3].includes(res.chargeStatus);
      }
      // 充电状态：0：预约中 1：启动中 2：充电中 3：停止中 4：已结束 5：未知
      if (checkAppointment && res && res.isAppointment) {
        uni.hideLoading();
        uni.showModal({
          title: "温馨提示",
          content: "当前已有预约中的订单，请取消再扫码充电",
          showCancel: false,
          confirmText: "查看预约",
          confirmColor: "#2d9e95",
          success(modal) {
            if (modal.confirm) {
              uni.redirectTo({
                url: `/pages-charge/appointment/appointment?sn=${res.connectorId}`,
              });
            }
          },
        });
      }
      if (checkCharge && res && res.isStarted) {
        uni.hideLoading();
        uni.showModal({
          title: "温馨提示",
          content: "当前已有进行中的订单，请结束订单再扫码充电",
          showCancel: false,
          confirmText: "查看详情",
          confirmColor: "#2d9e95",
          success(modal) {
            if (modal.confirm) {
              uni.redirectTo({
                url: `/pages-charge/ordering/ordering?sn=${res.connectorId}&start=1`,
              });
            }
          },
        });
      }
      return res;
    });
}

export function fetchStations(
  page: number,
  pageSize: number,
  latitude?: number,
  longitude?: number,
  baseLatitude?: number,
  baseLongitude?: number,
  options?: {
    distance?: number;
    status?: number;
  }
): Promise<any[]> {
  return fetchAllStations()
    .then((res) => {
      // console.log(latitude, longitude);
      // let list = JSON.parse(JSON.stringify(res));
      const data: any[] = JSON.parse(JSON.stringify(res)); // []
      const start = (page - 1) * pageSize;
      const end = start + pageSize;
      // 暂时废弃距离范围
      // if (latitude && longitude) {
      //   list.forEach((item: any) => {
      //     item.stationLatDistance = _getDistance(
      //       latitude,
      //       longitude,
      //       item.location.stationLat,
      //       item.location.stationLng
      //     );
      //   });
      //   list.sort((item1: any, item2: any) => {
      //     return item1.stationLatDistance - item2.stationLatDistance;
      //   });
      // }
      // 暂时废弃距离筛选
      // if (options) {
      //   if (options.distance && !isDebug) {
      //     list = list.filter(
      //       (item: any) => item.stationLatDistance <= (options.distance || 20)
      //     );
      //   }
      // }
      // list.forEach((item: any, index: number) => {
      //   if (index >= start && index < end) {
      //     data.push(item);
      //   }
      // });
      // 暂时显示全部电站
      if (latitude && longitude) {
        data.forEach((item: any) => {
          item.stationLatDistance = _getDistance(
            latitude,
            longitude,
            item.location.stationLat,
            item.location.stationLng
          );
        });
        data.sort((item1: any, item2: any) => {
          return item1.stationLatDistance - item2.stationLatDistance;
        });
      }
      if (baseLatitude && baseLongitude) {
        data.forEach((item: any) => {
          item.stationLatDistance = _getDistance(
            baseLatitude,
            baseLongitude,
            item.location.stationLat,
            item.location.stationLng
          );
        });
      }
      return _fetchStationStatus(data);
    })
    .then((list) => {
      // 暂时废弃状态筛选
      // if (options && options.status) {
      //   let res = false;
      //   list = list.filter((item: any) => {
      //     res = false;
      //     item.equipmentInfos.forEach((eqInfo: any) => {
      //       eqInfo.connectorInfos.forEach((coInfo: any) => {
      //         if (
      //           !res &&
      //           options.status === 1 &&
      //           coInfo.connectorStatusInfo &&
      //           coInfo.connectorStatusInfo.status === 1
      //         ) {
      //           res = true;
      //         }
      //         if (
      //           !res &&
      //           options.status === 2 &&
      //           coInfo.connectorStatusInfo &&
      //           coInfo.connectorStatusInfo.status !== 1
      //         ) {
      //           res = true;
      //         }
      //       });
      //     });
      //     return res;
      //   });
      // }
      return list;
    });
}

export function fetchStation(id: number) {
  return fetchAllStations()
    .then((res) => {
      const findIndex = res.findIndex((item) => Number(item.StationID) === id);
      if (findIndex < 0) {
        throw {
          errMsg: "not found",
        };
      }
      return _fetchStationStatus([res[findIndex]]);
    })
    .then((list) => {
      return list[0];
    });
}

export function fetchStationByIds(ids: number[]) {
  return fetchAllStations().then((res) => {
    const list = res.filter((item) => ids.includes(Number(item.StationID)));
    return _fetchStationStatus(list);
  });
}

export function fetchStationByConnectorIdOrShortId(id: string) {
  let equipment = -1;
  return fetchAllStations()
    .then((res) => {
      let station: any = undefined;
      res.forEach((item) => {
        if (!station && item.equipmentInfos && item.equipmentInfos) {
          item.equipmentInfos.forEach(
            (equipmentInfo: any, equipmentIndex: number) => {
              if (id.length <= 16) {
                // 此处传入的是 shortId
                if (equipmentInfo.shortId === id) {
                  station = item;
                  equipment = equipmentIndex;
                }
              } else {
                if (
                  equipmentInfo.connectorInfos &&
                  equipmentInfo.connectorInfos
                ) {
                  // connectorId
                  equipmentInfo.connectorInfos.forEach((connectorInfo: any) => {
                    if (connectorInfo.connectorId === id) {
                      station = item;
                      equipment = equipmentIndex;
                    }
                  });
                }
              }
            }
          );
        }
      });
      if (station) {
        return _fetchStationStatus([station]);
      } else {
        return Promise.reject({});
      }
    })
    .then((res) => {
      if (res && res.length) {
        return {
          station: res[0],
          equipment: res[0].equipmentInfos[equipment],
        };
      } else {
        return Promise.reject({});
      }
    });
}

export function searchStation(keyword: string) {
  return fetchAllStations().then((res) => {
    const reg = new RegExp(keyword, "ig");
    const list = res.filter(
      (item) => reg.test(item.stationName) || reg.test(item.address)
    );
    return _fetchStationStatus(list);
  });
}

export function fetchAllStations(): Promise<any[]> {
  if (getApp<any>().globalData.stations.length > 0) {
    return Promise.resolve(getApp<any>().globalData.stations);
  }
  const page = 1;
  const page_size = 99;
  return new Promise((resolve, reject) => {
    _fetchAllStations(page, page_size, [])
      .then((list) => {
        // if (!isProduction) {
        //   list.push({
        //     ...list[0],
        //     StationName: '这是模拟数据，不要使用充电',
        //     Address: '这是模拟数据，测试一下附近的点',
        //     StationLat: 22.540545,
        //     StationLng: 113.942695,
        //     StationID: '0000'
        //   })
        // }
        console.log("所有电站数据", list);
        getApp<any>().globalData.stations = list;
        resolve(list);
      })
      .catch(reject);
  });
}

function _fetchAllStations(
  page: number,
  pageSize: number,
  list: any[]
): Promise<any[]> {
  return _fetchStations(page, pageSize).then((res) => {
    list = list.concat(res);
    if (res.length >= pageSize) {
      return _fetchAllStations(page + 1, pageSize, list);
    } else {
      // eslint-disable-next-line promise/no-return-wrap
      return Promise.resolve(list);
    }
  });
}

function _fetchStations(page: number, pageSize: number) {
  return cHttp
    .get(`/charge/listStation?pageNum=${page}&pageSize=${pageSize}`)
    .then((res) => {
      const data = res || [];
      data.forEach((item: any) => {
        item.StationID = item.stationId;
        item.fastEquipmentInfos = [];
        item.slowEquipmentInfos = [];
        item.totalFee = Number(
          Number(
            Number(item.electricityFee) +
              Number(item.parkFee) +
              Number(item.serviceFee)
          ).toFixed(2)
        );
      });
      return data.filter((item: any) => {
        return !["1657"].includes(item.StationID);
      });
    });
}

function _getDistance(lat1: number, lng1: number, lat2: number, lng2: number) {
  var radLat1 = (lat1 * Math.PI) / 180.0;
  var radLat2 = (lat2 * Math.PI) / 180.0;
  var a = radLat1 - radLat2;
  var b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;
  var s =
    2 *
    Math.asin(
      Math.sqrt(
        Math.pow(Math.sin(a / 2), 2) +
          Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
      )
    );
  s = s * 6378.137; // EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000;
  return s;
}

function _fetchStationStatus(list: any[]) {
  if (list.length <= 0) {
    return Promise.resolve([]);
  }
  let _list: any[] = [];
  return cHttp
    .get(
      `/charge/stationStatus?stationIds=${list
        .filter((item) => item.StationID !== "0000")
        .map((item) => item.StationID)
        .join(",")}`
    )
    .then((res) => {
      const StationStatusInfos = res || [];
      const ConnectorIDs: string[] = [];
      const StationIDs: string[] = [];
      list.forEach((item: any) => {
        item.fastEquipmentInfos = [];
        item.slowEquipmentInfos = [];
        const StationStatusInfo = StationStatusInfos.find(
          (status: any) => Number(status.stationId) === Number(item.StationID)
        );
        let ConnectorID = "";
        item.equipmentInfos.forEach((eqInfo: any) => {
          eqInfo.connectorInfos.forEach((coInfo: any) => {
            if (StationStatusInfo) {
              const connectorStatusInfo =
                StationStatusInfo.connectorStatusInfos.find(
                  (costatus: any) => costatus.connectorId === coInfo.connectorId
                );
              if (connectorStatusInfo) {
                // debug
                // connectorStatusInfo.status = 255
                coInfo.connectorStatusInfo = connectorStatusInfo;
              }
            }
            if (!ConnectorID) {
              ConnectorID = coInfo.connectorId;
            }
          });
          if ([1].includes(Number(eqInfo.equipmentType))) {
            item.fastEquipmentInfos.push(eqInfo);
          } else {
            item.slowEquipmentInfos.push(eqInfo);
          }
        });
        if (ConnectorID) {
          ConnectorIDs.push(ConnectorID);
          StationIDs.push(item.StationID);
        }
      });
      _list = list;
      return Promise.all(
        ConnectorIDs.map((cid, cindex) => {
          return fetchStationPriceDesc(cid, StationIDs[cindex]);
        })
      );
    })
    .then((res) => {
      if (res && res.length) {
        _list.forEach((item) => {
          const i = res.findIndex((r) => r.StationID === item.StationID);
          if (i >= 0) {
            item.totalFee = res[i].currentPrice;
          }
        });
      }
      return _list;
    });



}
