import { TFunction } from "i18next";
import { runInAction } from "mobx";
import { sprintf } from "sprintf-js";

import { formatText } from "src/Utility";
import { HOF } from "src/hof/HOF";
import { wwwAXIOS } from "src/Www";
import { GetHOFAPI } from "src/wwwAPI";

export default function setLevelStore() {
  const formatValue = (
    data: GetHOFAPI[],
    onValue: (value: number) => string,
  ): HOF[] =>
    data.map((hof) => ({
      ...hof,
      text: onValue(hof.value),
    }));

  return {
    isTotalTotalLoading: false,
    isTotalHighestLoading: false,
    isTotalStandLoading: false,
    isTotalBandLoading: false,
    isAtTotalLoading: false,
    isAtHighestLoading: false,
    isAtStandLoading: false,
    isAtBandLoading: false,
    isAbility5KLoading: false,
    isAbility7KLoading: false,
    isAbility9KLoading: false,
    isLevelLoading: false,
    totalTotalHOF: [] as HOF[],
    totalHighestHOF: [] as HOF[],
    totalStandHOF: [] as HOF[],
    totalBandHOF: [] as HOF[],
    atTotalHOF: [] as HOF[],
    atHighestHOF: [] as HOF[],
    atStandHOF: [] as HOF[],
    atBandHOF: [] as HOF[],
    ability5KHOF: [] as HOF[],
    ability7KHOF: [] as HOF[],
    ability9KHOF: [] as HOF[],
    levelHOF: [] as HOF[],
    tabPosition: 0,
    totalTabPosition: 0,
    atTabPosition: 0,
    abilityTabPosition: 0,

    setTabPosition(tabPosition: number) {
      this.tabPosition = tabPosition;
    },

    setTotalTabPosition(totalTabPosition: number) {
      this.totalTabPosition = totalTabPosition;
    },

    setAtTabPosition(atTabPosition: number) {
      this.atTabPosition = atTabPosition;
    },

    setAbilityTabPosition(abilityTabPosition: number) {
      this.abilityTabPosition = abilityTabPosition;
    },

    get isLoading() {
      return (
        this.isTotalTotalLoading ||
        this.isTotalHighestLoading ||
        this.isTotalStandLoading ||
        this.isTotalBandLoading ||
        this.isAtTotalLoading ||
        this.isAtHighestLoading ||
        this.isAtStandLoading ||
        this.isAtBandLoading ||
        this.isAbility5KLoading ||
        this.isAbility7KLoading ||
        this.isAbility9KLoading ||
        this.isLevelLoading
      );
    },

    async getTotalTotalHOF(t: TFunction) {
      this.isTotalTotalLoading = true;
      const { status, data } =
        await wwwAXIOS.get<GetHOFAPI[]>("/hof/totalTotal");
      if (status === 200) {
        runInAction(() => {
          this.totalTotalHOF = formatValue(data, (value) =>
            sprintf(t("textHandled"), formatText(value)),
          );
          this.isTotalTotalLoading = false;
        });
      }
    },

    async getTotalHighestHOF(t: TFunction) {
      this.isTotalHighestLoading = true;
      const { status, data } =
        await wwwAXIOS.get<GetHOFAPI[]>("/hof/totalHighest");
      if (status === 200) {
        runInAction(() => {
          this.totalHighestHOF = formatValue(data, (value) =>
            sprintf(t("textCount"), formatText(value)),
          );
          this.isTotalHighestLoading = false;
        });
      }
    },

    async getTotalStandHOF(t: TFunction) {
      this.isTotalStandLoading = true;
      const { status, data } =
        await wwwAXIOS.get<GetHOFAPI[]>("/hof/totalStand");
      if (status === 200) {
        runInAction(() => {
          this.totalStandHOF = formatValue(data, (value) =>
            sprintf(t("textStand"), formatText(value)),
          );
          this.isTotalStandLoading = false;
        });
      }
    },

    async getTotalBandHOF(t: TFunction) {
      this.isTotalBandLoading = true;
      const { status, data } =
        await wwwAXIOS.get<GetHOFAPI[]>("/hof/totalBand");
      if (status === 200) {
        runInAction(() => {
          this.totalBandHOF = formatValue(data, (value) =>
            sprintf(t("textBand"), formatText(value)),
          );
          this.isTotalBandLoading = false;
        });
      }
    },

    async getAtTotalHOF(t: TFunction) {
      this.isAtTotalLoading = true;
      const { status, data } = await wwwAXIOS.get<GetHOFAPI[]>("/hof/atTotal");
      if (status === 200) {
        runInAction(() => {
          this.atTotalHOF = formatValue(data, (value) =>
            sprintf(t("textHandled"), formatText(value)),
          );
          this.isAtTotalLoading = false;
        });
      }
    },

    async getAtHighestHOF(t: TFunction) {
      this.isAtHighestLoading = true;
      const { status, data } =
        await wwwAXIOS.get<GetHOFAPI[]>("/hof/atHighest");
      if (status === 200) {
        runInAction(() => {
          this.atHighestHOF = formatValue(data, (value) =>
            sprintf(t("textCount"), formatText(value)),
          );
          this.isAtHighestLoading = false;
        });
      }
    },

    async getAtStandHOF(t: TFunction) {
      this.isAtStandLoading = true;
      const { status, data } = await wwwAXIOS.get<GetHOFAPI[]>("/hof/atStand");
      if (status === 200) {
        runInAction(() => {
          this.atStandHOF = formatValue(data, (value) =>
            sprintf(t("textStand"), formatText(value)),
          );
          this.isAtStandLoading = false;
        });
      }
    },

    async getAtBandHOF(t: TFunction) {
      this.isAtBandLoading = true;
      const { status, data } = await wwwAXIOS.get<GetHOFAPI[]>("/hof/atBand");
      if (status === 200) {
        runInAction(() => {
          this.atBandHOF = formatValue(data, (value) =>
            sprintf(t("textBand"), formatText(value)),
          );
          this.isAtBandLoading = false;
        });
      }
    },

    async getAbility5KHOF() {
      this.isAbility5KLoading = true;
      const { status, data } =
        await wwwAXIOS.get<GetHOFAPI[]>("/hof/ability/5K");
      if (status === 200) {
        runInAction(() => {
          this.ability5KHOF = formatValue(data, (value) =>
            sprintf("%s Point", formatText(value)),
          );
          this.isAbility5KLoading = false;
        });
      }
    },

    async getAbility7KHOF() {
      this.isAbility7KLoading = true;
      const { status, data } =
        await wwwAXIOS.get<GetHOFAPI[]>("/hof/ability/7K");
      if (status === 200) {
        runInAction(() => {
          this.ability7KHOF = formatValue(data, (value) =>
            sprintf("%s Point", formatText(value)),
          );
          this.isAbility7KLoading = false;
        });
      }
    },

    async getAbility9KHOF() {
      this.isAbility9KLoading = true;
      const { status, data } =
        await wwwAXIOS.get<GetHOFAPI[]>("/hof/ability/9K");
      if (status === 200) {
        runInAction(() => {
          this.ability9KHOF = formatValue(data, (value) =>
            sprintf("%s Point", formatText(value)),
          );
          this.isAbility9KLoading = false;
        });
      }
    },

    async getLevelHOF() {
      this.isLevelLoading = true;
      const { status, data } = await wwwAXIOS.get<GetHOFAPI[]>("/hof/level");
      if (status === 200) {
        runInAction(() => {
          this.levelHOF = formatValue(data, (value) =>
            sprintf("LV. %d", value),
          );
          this.isLevelLoading = false;
        });
      }
    },
  };
}
