import dayjs from "dayjs";
import { trainDataType, trainPosition } from "@/lib/trainPositionTextArray";
import { eachTrainDiagramType, StationProps } from "@/lib/CommonTypes";
type trainDataProps = {
  d: eachTrainDiagramType;
  currentTrain: trainDataType[];
  station: StationProps[];
};
export const trainTimeFiltering: (x: trainDataProps) => boolean = (props) => {
  const { d, currentTrain, station } = props;
  const baseTime = 2; // 何時間以内の列車を表示するか
  if (currentTrain.filter((t) => t.num == d.train).length == 0) {
    const date = dayjs();
    const trainTime = date
      .hour(parseInt(d.time.split(":")[0]))
      .minute(parseInt(d.time.split(":")[1]));

    if (date.isAfter(trainTime)) {
      return false;
    } else if (trainTime.diff(date) < baseTime * 60 * 60 * 1000) {
      return true;
    }
    return false;
  } else {
    const Pos = trainPosition(currentTrain.filter((t) => t.num == d.train)[0]);
    let nextPos = "";
    let PrePos = "";
    //
    if (Pos.isBetween == true) {
      nextPos = Pos.Pos.to;
      PrePos = Pos.Pos.from;
    } else {
      nextPos = Pos.Pos.Pos;
    }
    const stationData = station[0].Station_JP;
    switch (stationData) {
      case nextPos:
        if (d.lastStation != stationData) return true;
        break;
      case PrePos:
        return false;
      default:
        break;
    }
    const [h, m] = d.time.split(":");
    const delayData = currentTrain.filter((t) => t.num == d.train)[0].delay;
    let delay = delayData === "入線" ? 0 : delayData;
    const date = dayjs();
    const IntH = parseInt(h) < 4 ? parseInt(h) + 24 : parseInt(h);
    const IntM = parseInt(m);
    const targetDate = date.hour(IntH).minute(IntM + delay);
    if (date.isAfter(targetDate)) return false;
    if (targetDate.diff(date) < baseTime * 60 * 60 * 1000) return true;
    return false;
  }
};
type getTimeProps = (
  stationDiagram: { [key: string]: string },
  station: StationProps
) => eachTrainDiagramType[];
export const getTime: getTimeProps = (stationDiagram, station) => {
  const returnData = Object.keys(stationDiagram)
    .map((trainNum) => {
      let trainData: eachTrainDiagramType = {
        time: "",
        lastStation: "",
        isThrough: false,
        train: trainNum,
      };
      stationDiagram[trainNum].split("#").forEach((data) => {
        const [stationName, type, time] = data.split(",");
        if (!type) return;
        if (type.match("着")) {
          trainData.lastStation = stationName;
        }
        if (stationName === station.Station_JP) {
          if (type.match("発")) {
            trainData.time = time;
          } else if (type.match("通")) {
            trainData.time = time;
            trainData.isThrough = true;
          } else if (type.match("着")) {
            trainData.time = time;
          }
        }
      });
      return {
        train: trainNum,
        time: trainData.time,
        lastStation: trainData.lastStation,
        isThrough: trainData.isThrough,
      };
    })
    .filter((d) => d.time);
  return returnData.sort((a, b) => {
    let [aH, aM] = a.time.split(":");
    let [bH, bM] = b.time.split(":");
    if (parseInt(aH) < 4) aH = (parseInt(aH) + 24).toString();
    if (parseInt(bH) < 4) bH = (parseInt(bH) + 24).toString();
    const baseTime = dayjs();
    const aTime = baseTime.hour(parseInt(aH)).minute(parseInt(aM));
    const bTime = baseTime.hour(parseInt(bH)).minute(parseInt(bM));
    if (aTime.isBefore(bTime)) return -1;
    if (aTime.isAfter(bTime)) return 1;
    return 0;
  });
};
