import { defineStore } from 'pinia';
import { store } from '@/store';
import {
  // JSON_ACTIVITY,
  JSON_CHAPTER,
  // JSON_PLAYER_LEVEL,
  // JSON_TALENTS,
  // JSON_WEAPONS,
  // JSON_WEARS,
  // JSON_ACTIVITY2,
  // JSON_GOODS,
  // JSON_KingSeasonDan,
  // JSON_PRODUCT,
} from '@/store/mutation-types';

import { storage } from '@/utils/Storage';
import { downloadJson } from '@/api/json';

export interface IDownloadJsonState {
  // weapons: any[];
  // wears: any[];
  // talents: any[];
  // playerLevel: any[];
  chapter: any[];
  // kingSeasonDan: any[];
  // activity: any[];
  // activity2: any[];
  // goods: any[];
  // product: any[];
}

export const downloadJsonStore = defineStore('app-download-json', {
  // id: 'app-download-json',
  state: (): IDownloadJsonState => ({
    // weapons: [],
    // wears: [],
    // talents: [],
    // playerLevel: [],
    chapter: [],
    // kingSeasonDan: [],
    // activity: [],
    // activity2: [],
    // goods: [],
    // product: [],
  }),
  getters: {
    // getWeapons(): [any][] {
    //   return this.weapons;
    // },
    // getWears(): [any][] {
    //   return this.wears;
    // },
    // getTalents(): [any][] {
    //   return this.talents;
    // },
    // getPlayerLevel(): [any][] {
    //   return this.playerLevel;
    // },
    getChapter(): [any][] {
      return this.chapter;
    },
    // getKingSeasonDan(): [any][] {
    //   return this.kingSeasonDan;
    // },
    // getActivity(): [any][] {
    //   return this.activity;
    // },
    // getActivity2(): [any][] {
    //   return this.activity2;
    // },
    // getGoods(): [any][] {
    //   return this.goods;
    // },
    // getProduct(): [any][] {
    //   return this.product;
    // },
  },
  actions: {
    // setWeapons(weapons) {
    //   this.weapons = weapons;
    // },
    // setWears(wears) {
    //   this.wears = wears;
    // },
    // setTalents(talents) {
    //   this.talents = talents;
    // },
    // setPlayerLevel(playerLevel) {
    //   this.playerLevel = playerLevel;
    // },
    setChapter(chapter) {
      this.chapter = chapter;
    },
    // setKingSeasonDan(kingSeasonDan) {
    //   this.kingSeasonDan = kingSeasonDan;
    // },
    // setActivity(activity) {
    //   this.activity = activity;
    // },
    // setActivity2(activity2) {
    //   this.activity2 = activity2;
    // },
    // setGoods(goods) {
    //   this.goods = goods;
    // },
    // setProduct(product) {
    //   this.product = product;
    // },
    // downloadALL(jsonVersion: string) {
    //   this.downloadTalent(jsonVersion).then();
    //   this.downloadGoods(jsonVersion).then();
    //   this.downloadProduct(jsonVersion).then();
    //   this.downloadPlayerLevel(jsonVersion).then();
    // },
    // downloadWeapons(jsonVersion: string) {
    //   return new Promise((resolve, reject) => {
    //     downloadJson({ version: jsonVersion, name: 'weapons.json' })
    //       .then((res) => {
    //         const result = res;
    //         storage.set(JSON_WEAPONS, result);
    //         this.setWeapons(result);
    //         resolve(res);
    //       })
    //       .catch((error) => {
    //         reject(error);
    //       });
    //   });
    // },
    // downloadWear(jsonVersion: string) {
    //   return new Promise((resolve, reject) => {
    //     downloadJson({ version: jsonVersion, name: 'wear.json' })
    //       .then((res) => {
    //         const result = res;
    //         storage.set(JSON_WEARS, result);
    //         this.setWears(result);
    //         resolve(res);
    //       })
    //       .catch((error) => {
    //         reject(error);
    //       });
    //   });
    // },
    // downloadTalent(jsonVersion: string) {
    //   return new Promise((resolve, reject) => {
    //     downloadJson({ version: jsonVersion, name: 'talent.json' })
    //       .then((res) => {
    //         const result = res;
    //         storage.set(JSON_TALENTS, result);
    //         this.setTalents(result);
    //         resolve(res);
    //       })
    //       .catch((error) => {
    //         reject(error);
    //       });
    //   });
    // },
    // downloadGoods(jsonVersion: string) {
    //   return new Promise((resolve, reject) => {
    //     downloadJson({ version: jsonVersion, name: 'goods.json' })
    //       .then((res) => {
    //         const result = res;
    //         storage.set(JSON_GOODS, result);
    //         this.setGoods(result);
    //         resolve(res);
    //       })
    //       .catch((error) => {
    //         reject(error);
    //       });
    //   });
    // },
    // downloadProduct(jsonVersion: string) {
    //   return new Promise((resolve, reject) => {
    //     downloadJson({ version: jsonVersion, name: 'product.json' })
    //       .then((res) => {
    //         const result = res;
    //         storage.set(JSON_PRODUCT, result);
    //         this.setProduct(result);
    //         resolve(res);
    //       })
    //       .catch((error) => {
    //         reject(error);
    //       });
    //   });
    // },
    // downloadPlayerLevel(jsonVersion: string) {
    //   return new Promise((resolve, reject) => {
    //     downloadJson({ version: jsonVersion, name: 'playerLevel.json' })
    //       .then((res) => {
    //         const result = res;
    //         storage.set(JSON_PLAYER_LEVEL, result);
    //         this.setPlayerLevel(result);
    //         resolve(res);
    //       })
    //       .catch((error) => {
    //         reject(error);
    //       });
    //   });
    // },
    downloadChapter(jsonVersion: string) {
      return new Promise((resolve, reject) => {
        downloadJson({ version: jsonVersion, name: 'LevelList.json' })
          .then((res) => {
            for (let i = 0; i < res.length; i++) {
              res[i].label = res[i].Uid.toString() + '-' + res[i].Name;
              res[i].value = res[i].Uid.toString();
            }

            res.sort((a, b) => a.ID - b.ID);
            const result = res;
            storage.set(JSON_CHAPTER, result);
            this.setChapter(result);
            resolve(res);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // downloadKingSeasonDan(jsonVersion: string) {
    //   return new Promise((resolve, reject) => {
    //     downloadJson({ version: jsonVersion, name: 'KingSeasonDan.json' })
    //       .then((res) => {
    //         storage.set(JSON_KingSeasonDan, res);
    //         this.setKingSeasonDan(res);
    //         resolve(res);
    //       })
    //       .catch((error) => {
    //         reject(error);
    //       });
    //   });
    // },
    // downloadActivity(jsonVersion: string) {
    //   return new Promise((resolve, reject) => {
    //     downloadJson({ version: jsonVersion, name: 'activity.json' })
    //       .then((res) => {
    //         const result = res;
    //         storage.set(JSON_ACTIVITY, result);
    //         this.setActivity(result);
    //         resolve(res);
    //       })
    //       .catch((error) => {
    //         reject(error);
    //       });
    //   });
    // },
    // downloadActivity2(jsonVersion: string) {
    //   return new Promise((resolve, reject) => {
    //     downloadJson({ version: jsonVersion, name: 'Activity2.json' })
    //       .then((res) => {
    //         const result = res;
    //         storage.set(JSON_ACTIVITY2, result);
    //         this.setActivity2(result);
    //         resolve(res);
    //       })
    //       .catch((error) => {
    //         reject(error);
    //       });
    //   });
    // },
  },
});

// Need to be used outside the setup
export function downloadJsonStoreWidthOut() {
  return downloadJsonStore(store);
}
