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

const BusAndTrainDataContext = createContext(initialState);

export const useBusAndTrainData = () => {
  return useContext(BusAndTrainDataContext);
};

export const BusAndTrainDataProvider = ({ children }) => {
  const [busAndTrainData, setBusAndTrainData] = useState([]);
  const [trainPairData, setTrainPairData] = useState([]);
  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(() => {
    AS.getItem("trainPairData202403")
      .then((d) => {
        const returnData = JSON.parse(d);
        setTrainPairData(returnData);
      })
      .catch(() => {
        fetch(
          "https://script.google.com/macros/s/AKfycbyoBH7_rBwzPmhU1ghRBNTAVuvGltIrZtWxE07gDdhGGlDL9Ip2qk3pFM5u2xtRBl8/exec"
        )
          .then((d) => d.json())
          .then((d) => {
            setTrainPairData(d);
            AS.setItem("trainPairData202403", JSON.stringify(d));
          });
      });
  }, []);
  const initializeTrainPairList = () => {
    const trainPairList = {};
    trainPairData.forEach((d) => {
      trainPairList[Object.keys(d)[0]] = d[Object.keys(d)[0]];
    });
    return trainPairList;
  };
  const getInfluencedTrainData = (trainNum) => {
    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("147M");
        break;
      case "147M":
        returnArray.push("143M");
        break;
      // 徳島界隈接続一覧
      //下り
      case "4430D":
        returnArray.push("525D");
        break;
      case "951D":
        returnArray.push("527D");
        break;
      case "434D":
        returnArray.push("529D");
        break;
      case "5444D":
        returnArray.push("4537D");
        break;
      case "4452D":
        returnArray.push("4545D");
        break;
      case "4458D":
        returnArray.push("4551D");
        break;
      case "4460D":
        returnArray.push("553D");
        break;
      case "4464D":
        returnArray.push("559D");
        break;
      case "4466D":
        returnArray.push("561D");
        break;
      case "468D":
        returnArray.push("563D");
        break;
      case "4472D":
        returnArray.push("567D");
        break;
      case "474D":
        returnArray.push("569D");
        break;
      case "351D":
        returnArray.push("573D");
        break;
      case "478D":
        returnArray.push("575D");
        break;
      case "480D":
        returnArray.push("577D");
        break;
      case "484D":
        returnArray.push("581D");
        break;
      // reverse.
      case "525D":
        returnArray.push("4430D");
        break;
      case "527D":
        returnArray.push("951D");
        break;
      case "529D":
        returnArray.push("434D");
        break;
      case "4537D":
        returnArray.push("5444D");
        break;
      case "4545D":
        returnArray.push("4452D");
        break;
      case "4551D":
        returnArray.push("4458D");
        break;
      case "553D":
        returnArray.push("4460D");
        break;
      case "559D":
        returnArray.push("4464D");
        break;
      case "561D":
        returnArray.push("4466D");
        break;
      case "563D":
        returnArray.push("468D");
        break;
      case "567D":
        returnArray.push("4472D");
        break;
      case "569D":
        returnArray.push("474D");
        break;
      case "573D":
        returnArray.push("351D");
        break;
      case "575D":
        returnArray.push("478D");
        break;
      case "577D":
        returnArray.push("480D");
        break;
      case "581D":
        returnArray.push("484D");
        break;
      //上りむろと
      case "530D":
        returnArray.push("8052D");
        break;
      case "8052D":
        returnArray.push("530D");
        break;
      //上り
      case "522D":
        returnArray.push("952D");
        break;
      case "526D":
        returnArray.push("4954D");
        break;
      case "536D":
        returnArray.push("4330D");
        break;
      case "538D":
        returnArray.push("4447D");
        break;
      case "540D":
        returnArray.push("4336D");
        break;
      case "542D":
        returnArray.push("451D");
        break;
      case "4546D":
        returnArray.push("4455D");
        break;
      case "4552D":
        returnArray.push("4346D");
        break;
      case "4556D":
        returnArray.push("4352D");
        break;
      case "4558D":
        returnArray.push("5467D");
        break;
      case "562D":
        returnArray.push("360D");
        break;
      case "564D":
        returnArray.push("362D");
        break;
      case "566D":
        returnArray.push("475D");
        break;
      case "570D":
        returnArray.push("5479D");
        break;
      // reverse.
      case "952D":
        returnArray.push("522D");
        break;
      case "4954D":
        returnArray.push("526D");
        break;
      case "4330D":
        returnArray.push("536D");
        break;
      case "4447D":
        returnArray.push("538D");
        break;
      case "4336D":
        returnArray.push("540D");
        break;
      case "451D":
        returnArray.push("542D");
        break;
      case "4455D":
        returnArray.push("4546D");
        break;
      case "4346D":
        returnArray.push("4552D");
        break;
      case "4352D":
        returnArray.push("4556D");
        break;
      case "5467D":
        returnArray.push("4558D");
        break;
      case "360D":
        returnArray.push("562D");
        break;
      case "362D":
        returnArray.push("564D");
        break;
      case "475D":
        returnArray.push("566D");
        break;
      case "5479D":
        returnArray.push("570D");
        break;
    }
    if(new RegExp(/^4[1-9]\d\d[DM]$/).test(trainNum)){
      if (trainList["5" + trainNum.substring(1)])
        returnArray.push("5" + trainNum.substring(1));
    }
    if(new RegExp(/^5[1-7]\d\d[DM]$/).test(trainNum)){
      if (trainList["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 (trainList[trainNum.substring(1)]) {
        returnArray.push(trainNum.substring(1));
      }
    }
    if (new RegExp(/^[1-9]\d\d[DM]$/).test(trainNum)) {
      // 列番がxxDの場合は4xxDと5xxDの列番を検索
      if (trainList["4" + trainNum]) returnArray.push("4" + trainNum);
      if (trainList["5" + trainNum]) returnArray.push("5" + trainNum);
    }

    if (!returnArray.length) return [];
    const TDArray = returnArray.map((d) => {
      const TD = trainList[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>
  );
};
