import 予讃線 from "../assets/四国旅客鉄道予讃線.json";
import 土讃線 from "../assets/四国旅客鉄道土讃線.json";
import 高徳線 from "../assets/四国旅客鉄道高徳線.json";
import 内子線 from "../assets/四国旅客鉄道内子線.json";
import 徳島線 from "../assets/四国旅客鉄道徳島線.json";
import 鳴門線 from "../assets/四国旅客鉄道鳴門線.json";
import yosan from "../assets/originData/yosan";
import uwajima from "../assets/originData/uwajima";
import uwajima2 from "../assets/originData/uwajima2";
import dosan from "../assets/originData/dosan";
import dosan2 from "../assets/originData/dosan2";
import koutoku from "../assets/originData/koutoku";
import tokushima from "../assets/originData/tokushima";
import naruto from "../assets/originData/naruto";
import seto from "../assets/originData/seto";
import between from "../assets/originData/between";
import train_lang from "../assets/originData/train_lang";

let status = undefined;

export const lineList = [
  "予讃線(高松-松山間)[Y]",
  "予讃線(松山-宇和島間)[U]",
  "予讃線/愛ある伊予灘線(向井原-伊予大洲間)[S]",
  "土讃線(多度津-高知間)[D]",
  "土讃線(高知-窪川間)[K]",
  "高徳線(高松-徳島間)[T]",
  "徳島線(徳島-阿波池田間)[B]",
  "鳴門線(池谷-鳴門間)[N]",
  "瀬戸大橋線(児島-宇多津間)[M]",
];
export const lineListPair = {
  Y: "予讃線(高松-松山間)[Y]",
  U: "予讃線(松山-宇和島間)[U]",
  S: "予讃線/愛ある伊予灘線(向井原-伊予大洲間)[S]",
  D: "土讃線(多度津-高知間)[D]",
  K: "土讃線(高知-窪川間)[K]",
  T: "高徳線(高松-徳島間)[T]",
  B: "徳島線(徳島-阿波池田間)[B]",
  N: "鳴門線(池谷-鳴門間)[N]",
  M: "瀬戸大橋線(児島-宇多津間)[M]",
};
export const lineList_LineWebID = {
  "予讃線(高松-松山間)[Y]" : "yosan",
  "予讃線(松山-宇和島間)[U]" : "uwajima",
  "予讃線/愛ある伊予灘線(向井原-伊予大洲間)[S]" : "uwajima2",
  "土讃線(多度津-高知間)[D]" : "dosan",
  "土讃線(高知-窪川間)[K]" : "dosan2",
  "高徳線(高松-徳島間)[T]" : "koutoku",
  "徳島線(徳島-阿波池田間)[B]" : "tokushima",
  "鳴門線(池谷-鳴門間)[N]" : "naruto",
  "瀬戸大橋線(児島-宇多津間)[M]" : "seto",
};
export const getStationList2 = async () => {
  return {
    yosan,
    uwajima,
    uwajima2,
    dosan,
    dosan2,
    koutoku,
    tokushima,
    naruto,
    seto,
  };
};
export const stationIDPair = {
  yosan: "Y",
  uwajima: "U",
  uwajima2: "S",
  dosan: "D",
  dosan2: "K",
  koutoku: "T",
  tokushima: "B",
  naruto: "N",
  seto: "M",
};
export const stationNamePair = {
  "予讃線(高松 - 松山)": "yosan",
  "予讃線・内子線(松山 - 内子 - 宇和島)": "uwajima",
  "予讃線・伊予灘線(向井原 - 伊予大洲)": "uwajima2",
  "土讃線(多度津 - 高知)": "dosan",
  "土讃線(高知 - 窪川)": "dosan2",
  "高徳線(高松 - 徳島)": "koutoku",
  "徳島線(徳島 - 阿波池田)": "tokushima",
  "鳴門線(池谷 - 鳴門)": "naruto",
  "瀬戸大橋線(児島 - 宇多津)": "seto",
};

export const getStationList = async () => {
  if (status) return status;
  //駅リストイニシャライズ
  return await Promise.all([
    yosan,
    uwajima,
    uwajima2,
    dosan,
    dosan2,
    koutoku,
    tokushima,
    naruto,
    seto,
    between,
    train_lang,
  ]).then((values) => {
    let stationList = {};
    [
      stationList["予讃線(高松-松山間)[Y]"],
      stationList["予讃線(松山-宇和島間)[U]"],
      stationList["予讃線/愛ある伊予灘線(向井原-伊予大洲間)[S]"],
      stationList["土讃線(多度津-高知間)[D]"],
      stationList["土讃線(高知-窪川間)[K]"],
      stationList["高徳線(高松-徳島間)[T]"],
      stationList["徳島線(徳島-阿波池田間)[B]"],
      stationList["鳴門線(池谷-鳴門間)[N]"],
      stationList["瀬戸大橋線(児島-宇多津間)[M]"],
      stationList["駅間リスト"],
      stationList["日英対応表"],
    ] = values;
    const concatBetweenStations = (eachRouteData) => {
      let additional = [];
      eachRouteData.forEach((routeData, routeIndex) => {
        try {
          const currentStationID = parseInt(
            routeData.StationNumber.replace(/[A-Z]/g, "")
          );
          const nextStationID = parseInt(
            eachRouteData[routeIndex + 1].StationNumber.replace(/[A-Z]/g, "")
          );
          if (nextStationID - currentStationID != 1) {
            stationList["駅間リスト"].forEach((betweenList) => {
              if (
                betweenList.BetweenStation ==
                routeData.Station_JP +
                  "～" +
                  eachRouteData[routeIndex + 1].Station_JP
              ) {
                additional = additional.concat(betweenList.Datas);
              }
            });
          }
        } catch (e) {}
      });
      return eachRouteData
        .concat(additional)
        .sort((a, b) => (a.StationNumber > b.StationNumber ? 1 : -1));
    };
    const addStationPosition = (setDataBase, geoJson, EnJpList) => {
      return setDataBase.map((data) => {
        let stationName;
        if (data.hasOwnProperty("Station_JP")) stationName = data.Station_JP;
        else if (data.hasOwnProperty("StationName")) {
          stationName = data.StationName;
          data.Station_JP = data.StationName;
          data.Station_EN = EnJpList.find(
            (d) => d.Station_JP == data.Station_JP
          ).Station_EN;
        }
        geoJson.features
          .filter((d) => d.geometry.type == "Point")
          .forEach((element) => {
            if (element.properties.name == stationName) {
              data.lat = element.geometry.coordinates[1];
              data.lng = element.geometry.coordinates[0];
              data.jslodApi = element.properties.uri;
            }
          });
        return data;
      });
    };
    stationList["予讃線(高松-松山間)[Y]"] = addStationPosition(
      concatBetweenStations(stationList["予讃線(高松-松山間)[Y]"]),
      予讃線,
      stationList["日英対応表"]
    );
    stationList["予讃線(松山-宇和島間)[U]"] = addStationPosition(
      concatBetweenStations(stationList["予讃線(松山-宇和島間)[U]"]),
      予讃線,
      stationList["日英対応表"]
    );
    stationList["予讃線(松山-宇和島間)[U]"] = addStationPosition(
      concatBetweenStations(stationList["予讃線(松山-宇和島間)[U]"]),
      内子線,
      stationList["日英対応表"]
    );
    stationList["予讃線/愛ある伊予灘線(向井原-伊予大洲間)[S]"] =
      addStationPosition(
        concatBetweenStations(
          stationList["予讃線/愛ある伊予灘線(向井原-伊予大洲間)[S]"]
        ),
        予讃線,
        stationList["日英対応表"]
      );
    stationList["土讃線(多度津-高知間)[D]"] = addStationPosition(
      concatBetweenStations(stationList["土讃線(多度津-高知間)[D]"]),
      土讃線,
      stationList["日英対応表"]
    );
    stationList["土讃線(高知-窪川間)[K]"] = addStationPosition(
      concatBetweenStations(stationList["土讃線(高知-窪川間)[K]"]),
      土讃線,
      stationList["日英対応表"]
    );
    stationList["高徳線(高松-徳島間)[T]"] = addStationPosition(
      concatBetweenStations(stationList["高徳線(高松-徳島間)[T]"]),
      高徳線,
      stationList["日英対応表"]
    );
    stationList["鳴門線(池谷-鳴門間)[N]"] = addStationPosition(
      concatBetweenStations(stationList["鳴門線(池谷-鳴門間)[N]"]),
      鳴門線,
      stationList["日英対応表"]
    );
    stationList["徳島線(徳島-阿波池田間)[B]"] = addStationPosition(
      concatBetweenStations(stationList["徳島線(徳島-阿波池田間)[B]"]),
      徳島線,
      stationList["日英対応表"]
    );
    stationList["徳島線(徳島-阿波池田間)[B]"].pop();
    stationList["瀬戸大橋線(児島-宇多津間)[M]"] = [
      {
        Station_JP: "坂出",
        Station_EN: "Sakaide",
        MyStation: "3",
        StationNumber: null,
        DispNum: "3",
        StationTimeTable:
          "http://www.jr-shikoku.co.jp/01_trainbus/jikoku/pdf/sakaide.pdf",
        StationMap: "https://www.google.co.jp/maps/place/34.313222,133.856325",
        JrHpUrl: "http://www.jr-shikoku.co.jp/01_trainbus/kakueki/sakaide/",
        lat: 34.313222,
        lng: 133.856325,
        jslodApi: "https://uedayou.net/jrslod/四国旅客鉄道/予讃線/坂出",
      },

      {
        Station_JP: "児島",
        Station_EN: "Kojima",
        MyStation: "0",
        StationNumber: "M12",
        DispNum: "3",
        StationTimeTable:
          "http://www.jr-odekake.net/eki/timetable.php?id=0651304",
        StationMap: "https://www.google.co.jp/maps/place/34.462562,133.807809",
        JrHpUrl: "http://www.jr-odekake.net/eki/top.php?id=0651304",
        lat: 34.462562,
        lng: 133.807809,
        jslodApi: "https://uedayou.net/jrslod/四国旅客鉄道/本四備讃線/児島",
      },
      {
        Station_JP: "宇多津",
        Station_EN: "Utazu",
        MyStation: "0",
        StationNumber: null,
        DispNum: "3",
        StationTimeTable:
          "http://www.jr-shikoku.co.jp/01_trainbus/jikoku/pdf/utazu.pdf",
        StationMap: "https://www.google.co.jp/maps/place/34.306379,133.813784",
        JrHpUrl: "http://www.jr-shikoku.co.jp/01_trainbus/kakueki/utazu/",
        lat: 34.306379,
        lng: 133.813784,
        jslodApi: "https://uedayou.net/jrslod/四国旅客鉄道/本四備讃線/宇多津",
      },
    ];
    status = stationList;
    return stationList;
  });
};
