import { runInAction } from "mobx";
import { getMillis } from "taehui-ts/date";
import { getLanguage } from "taehui-ts/language";

import { wwwAXIOS } from "src/Www";
import {
  EtcAPIEdge,
  EtcAPIMode,
  EtcAPINote,
  EtcAPITitle,
  GetEtcAPI,
} from "src/wwwAPI";

export default function setEtcStore() {
  let lastMillis = getMillis();

  return {
    totalDateSet: [] as string[],
    totalDateValues: [] as number[],
    signUpDateSet: [] as string[],
    signUpDateValues: [] as number[],
    avatarDateSet: [] as string[],
    avatarDateValues: [] as number[],
    totalNoteFiles: [] as EtcAPINote[],
    totalTitles: [] as EtcAPITitle[],
    totalEdges: [] as EtcAPIEdge[],
    favorites: [] as EtcAPINote[],
    favoritesAt: [] as EtcAPINote[],
    inputModes: [] as EtcAPIMode[],
    autoModes: [] as EtcAPIMode[],
    noteSaltModes: [] as EtcAPIMode[],
    faintNoteModes: [] as EtcAPIMode[],
    judgmentModes: [] as EtcAPIMode[],
    hitPointsModes: [] as EtcAPIMode[],
    noteMobilityModes: [] as EtcAPIMode[],
    longNoteModes: [] as EtcAPIMode[],
    inputFavorModes: [] as EtcAPIMode[],
    noteModifyModes: [] as EtcAPIMode[],
    lowestJudgmentConditionModes: [] as EtcAPIMode[],
    isLoading: false,

    async getEtc() {
      const millis = getMillis();
      lastMillis = millis;
      this.isLoading = true;
      const { status, data } = await wwwAXIOS.get<GetEtcAPI>("/etc", {
        params: { language: getLanguage() },
      });
      if (millis >= lastMillis) {
        if (status === 200) {
          runInAction(() => {
            this.totalDateSet = data.totalDateSet;
            this.totalDateValues = data.totalDateValues;
            this.signUpDateSet = data.signUpDateSet;
            this.signUpDateValues = data.signUpDateValues;
            this.avatarDateSet = data.avatarDateSet;
            this.avatarDateValues = data.avatarDateValues;
            this.totalNoteFiles = data.totalNoteFiles;
            this.totalTitles = data.totalTitles;
            this.totalEdges = data.totalEdges;
            this.favorites = data.favorites;
            this.favoritesAt = data.favoritesAt;
            this.inputModes = data.inputModes;
            this.autoModes = data.autoModes;
            this.noteSaltModes = data.noteSaltModes;
            this.faintNoteModes = data.faintNoteModes;
            this.judgmentModes = data.judgmentModes;
            this.hitPointsModes = data.hitPointsModes;
            this.noteMobilityModes = data.noteMobilityModes;
            this.longNoteModes = data.longNoteModes;
            this.inputFavorModes = data.inputFavorModes;
            this.noteModifyModes = data.noteModifyModes;
            this.lowestJudgmentConditionModes =
              data.lowestJudgmentConditionModes;
            this.isLoading = false;
          });
        }
      }
    },
  };
}
