import React, {
  createContext,
  useContext,
  useState,
  useEffect,
  FC,
} from "react";
import { AS } from "../storageControl";
import { useAllTrainDiagram } from "./useAllTrainDiagram";
const initialState = {
  busAndTrainData: [],
  setBusAndTrainData: (e) => {},
  trainPairData: [],
  setTrainPairData: (e) => {},
  initializeTrainPairList: (e) => {},
  getInfluencedTrainData: (e) => {},
};

const BusAndTrainDataContext = createContext(initialState);

export const useBusAndTrainData = () => {
  return useContext(BusAndTrainDataContext);
};
type props = { children: React.ReactNode };
export const BusAndTrainDataProvider: FC<props> = ({ children }) => {
  const { allTrainDiagram } = useAllTrainDiagram();
  const [busAndTrainData, setBusAndTrainData] = useState<
    {
      address: string;
      name: string;
      type: "station-data" | "bus-stop" | "train-info";
    }[]
  >([]);
  const [trainPairData, setTrainPairData] = useState<any[]>([]);
  useEffect(() => {
    AS.getItem("busAndTrain202403")
      .then((d) => {
        const returnData = JSON.parse(d);
        setBusAndTrainData(returnData);
      })
      .catch(() => {
        fetch(
          "https://script.google.com/macros/s/AKfycbw0UW6ZeCDgUYFRP0zxpc_Oqfy-91dBdbWv-cM8n3narKp14IyCd2wy5HW7taXcW7E/exec"
        )
          .then((d) => d.json())
          .then((d) => {
            setBusAndTrainData(d);
            AS.setItem("busAndTrain202403", JSON.stringify(d));
          });
      });
  }, []);
  useEffect(() => {
    fetch(
      "https://script.google.com/macros/s/AKfycbyoBH7_rBwzPmhU1ghRBNTAVuvGltIrZtWxE07gDdhGGlDL9Ip2qk3pFM5u2xtRBl8/exec"
    )
      .then((d) => d.json())
      .then((d) => {
        setTrainPairData(d);
      });
  }, []);
  const initializeTrainPairList = () => {
    const trainPairList = {};
    trainPairData.forEach((d) => {
      trainPairList[Object.keys(d)[0]] = d[Object.keys(d)[0]];
    });
    return trainPairList;
  };
  const getInfluencedTrainData = (trainNum: string) => {
    const trainPairList = initializeTrainPairList();

    // 9253M ラマル
    // 9256M ラマル

    // 4125m-545M
    //143M-147M
    // 1261M-4261M
    // 3621DWANMAN
    //8091D-IYO-OZU
    //8093D-IYO-YAWA
    //8092D-IYO-FUTAMI
    //8094-IYO-DOGO

    const returnArray = [];
    if (!trainNum) return;
    if (trainPairList[trainNum]) {
      returnArray.push(Object.keys(trainPairList[trainNum])[0]);
    }
    switch (trainNum) {
      case "143M":
        returnArray.push("139M");
        break;
      case "139M":
        returnArray.push("143M");
        break;
      // 土讃線琴平界隈
      case "1263M":
        returnArray.push("4263M");
        break;
      case "4263M":
        returnArray.push("1263M");
        break;
      // 徳島界隈接続一覧
      //下り
      case "4430D":
        returnArray.push("525D");
        break;
      case "951D":
        returnArray.push("527D");
        break;
      case "434D":
        returnArray.push("529D");
        break;
      case "4452D":
        returnArray.push("4545D");
        break;
      case "4466D":
        returnArray.push("561D");
        break;
      case "468D":
        returnArray.push("563D");
        break;
      case "4470D":
        returnArray.push("565D");
        break;
      case "4472D":
        returnArray.push("567D");
        break;
      case "474D":
        returnArray.push("569D");
        break;
      case "476D":
        returnArray.push("573D");
        break;
      case "478D":
        returnArray.push("575D");
        break;
      case "480D":
        returnArray.push("579D");
        break;
      case "484D":
        returnArray.push("583D");
        break;
      // reverse.
      case "525D":
        returnArray.push("4430D");
        break;
      case "527D":
        returnArray.push("951D");
        break;
      case "529D":
        returnArray.push("434D");
        break;
      case "4545D":
        returnArray.push("4452D");
        break;
      case "561D":
        returnArray.push("4466D");
        break;
      case "563D":
        returnArray.push("468D");
        break;
      case "565D":
        returnArray.push("4470D");
        break;
      case "567D":
        returnArray.push("4472D");
        break;
      case "569D":
        returnArray.push("474D");
        break;
      case "573D":
        returnArray.push("476D");
        break;
      case "575D":
        returnArray.push("478D");
        break;
      case "579D":
        returnArray.push("480D");
        break;
      case "583D":
        returnArray.push("484D");
        break;
      //上り
      case "4520D":
        returnArray.push("4314D");
        break;
      case "522D":
        returnArray.push("952D");
        break;
      case "526D":
        returnArray.push("4954D");
        break;
      case "534D":
        returnArray.push("4326D");
        break;
      case "536D":
        returnArray.push("4447D");
        break;
      case "538D":
        returnArray.push("4334D");
        break;
      case "540D":
        returnArray.push("451D");
        break;
      case "4544D":
        returnArray.push("4455D");
        break;
      case "4550D":
        returnArray.push("4342D");
        break;
      case "4554D":
        returnArray.push("4350D");
        break;
      case "4556D":
        returnArray.push("5467D");
        break;
      case "560D":
        returnArray.push("5471D");
        break;
      case "562D":
        returnArray.push("358D");
        break;
      case "564D":
        returnArray.push("475D");
        break;
      case "568D":
        returnArray.push("5479D");
        break;
      case "4572D":
        returnArray.push("4978D");
        break;
      case "576D":
        returnArray.push("4368D");
        break;
      // reverse.
      case "4314D":
        returnArray.push("4520D");
        break;
      case "952D":
        returnArray.push("522D");
        break;
      case "4954D":
        returnArray.push("526D");
        break;
      case "4326D":
        returnArray.push("534D");
        break;
      case "4447D":
        returnArray.push("536D");
        break;
      case "4334D":
        returnArray.push("538D");
        break;
      case "451D":
        returnArray.push("540D");
        break;
      case "4455D":
        returnArray.push("4544D");
        break;
      case "4342D":
        returnArray.push("4550D");
        break;
      case "4350D":
        returnArray.push("4554D");
        break;
      case "5467D":
        returnArray.push("4556D");
        break;
      case "5471D":
        returnArray.push("560D");
        break;
      case "358D":
        returnArray.push("562D");
        break;
      case "475D":
        returnArray.push("564D");
        break;
      case "5479D":
        returnArray.push("568D");
        break;
      case "4978D":
        returnArray.push("4572D");
        break;
      case "4368D":
        returnArray.push("576D");
        break;
      case "3710D":
        returnArray.push("710D");
        break;
      case "710D":
        returnArray.push("3710D");
        break;
      case "3751D":
        returnArray.push("751D");
        break;
      case "751D":
        returnArray.push("3751D");
        break;
    }
    if (new RegExp(/^4[1-9]\d\d[DM]$/).test(trainNum)) {
      if (allTrainDiagram["5" + trainNum.substring(1)])
        returnArray.push("5" + trainNum.substring(1));
    }
    if (new RegExp(/^5[1-7]\d\d[DM]$/).test(trainNum)) {
      if (allTrainDiagram["4" + trainNum.substring(1)])
        returnArray.push("4" + trainNum.substring(1));
    }
    if (
      // 列番が4xxDまたは5xxDの場合はxxDの列番を検索
      new RegExp(/^4[1-9]\d\d[DM]$/).test(trainNum) ||
      new RegExp(/^5[1-7]\d\d[DM]$/).test(trainNum)
    ) {
      if (allTrainDiagram[trainNum.substring(1)]) {
        returnArray.push(trainNum.substring(1));
      }
    }
    if (new RegExp(/^[1-9]\d\d[DM]$/).test(trainNum)) {
      // 列番がxxDの場合は4xxDと5xxDの列番を検索
      if (allTrainDiagram["4" + trainNum]) returnArray.push("4" + trainNum);
      if (allTrainDiagram["5" + trainNum]) returnArray.push("5" + trainNum);
    }

    if (!returnArray.length) return [];
    const TDArray = returnArray
      .filter((d) => allTrainDiagram.hasOwnProperty(d))
      .map((d) => {
        const TD = allTrainDiagram[d];
        if (!TD) return;
        return { id: d, TrainData: TD.split("#").filter((d) => d != "") };
      });
    return TDArray;
  };
  return (
    <BusAndTrainDataContext.Provider
      value={{
        busAndTrainData,
        setBusAndTrainData,
        trainPairData,
        setTrainPairData,
        initializeTrainPairList,
        getInfluencedTrainData,
      }}
    >
      {children}
    </BusAndTrainDataContext.Provider>
  );
};
