import { defineStore } from "pinia";
import { store } from "@/stores";
import { actions } from "@/utils/storage";
import { noticeStore } from "./notice";
import http from "@/https";

export const gameListStore = defineStore(actions.GAME_LIST, {
  persist: true,
  state: () => {
    return {
      //封装后数据
      gameList: <Array<any>>[],
      //原始游戏列表数据
      originalList: <Array<any>>[],
      // 首页厂商
      providerList: <Array<any>>[],
      // 首页 游戏分类
      gameClassData: <Array<any>>[],
      // 首页轮播
      carouselData: <Array<any>>[],

      gameTypeData: <Array<any>>[],
      //公告列表
      announcementData: <Array<any>>[],
    };
  },
  actions: {
    // 游戏状态更新 替换对应游戏 更新所有游戏列表
    setAllGameList(data: any) {
      this.originalList = data;
      this.setPackageData();
    },
    async setGameTypeData() {
      const gameTypeData = await http.getGameTypeList();
      console.log(gameTypeData, "gameTypeData");
      if (gameTypeData.code != 0) return;
      this.gameTypeData = gameTypeData.data;
    },
    // 轮播状态更新 替换数据列表
    async setCarouselData() {
      const carousel = await http.getCarousel();
      if (carousel.code != 0) return;
      this.carouselData = carousel.data.filter(
        (item: any) => !item.isDisable
      ); // 过滤 isDisable=true 隐藏
    },
    // 游戏厂商状态更新 替换数据列表
    async setProviderListData() {
      const providerListData = await http.getGameProviderList();
      if (providerListData.code != 0) return;
      this.providerList = providerListData.data.filter(
        (item: any) => !item.isDisable && !item.isDeleted
      );
    },
    // 游戏分类状态更新 替换数据列表
    async setGameClassDataData(bool: boolean) {
      const classData = await http.getGameClassList();
      if (classData.code != 0) return;
      this.gameClassData = classData.data.filter(
        (item: any) => !item.isWebDisable && !item.isDeleted
      );
      if (bool) {
        this.setPackageData();
      }
    },
    // 获取公告列表
    async getAds() {
      const res = await http.getAds();
      console.log(res, "announcementData");
      this.announcementData = res.data;
    },
    // 火爆热门 轮播
    async setNotice() {
      const notice = await http.getNotice();
      if (notice.code != 0) return;
      let noticeData: Array<any> = [];
      for (let i = 0; i < notice.data.length; i++) {
        if (notice.data[i].gameId) {
          let newItem = this.originalList.find(
            (item: any) => item.gameId === notice.data[i].gameId
          );
          noticeData.push({
            ...newItem,
            ...notice.data[i],
          });
        }
      }
      noticeStore().setAllNotice(noticeData);
    },

    async getGameList() {
      // 请求轮播
      if (this.carouselData.length === 0) {
        await this.setCarouselData();
      }

      // 请求 厂商分类
      if (this.providerList.length === 0) {
        await this.setProviderListData();
      }

      if (this.gameTypeData.length === 0) {
        await this.setGameTypeData();
      }

      // 请求 游戏分类
      if (this.gameList.length === 0) {
        await this.setGameClassDataData(false);
      }

      // 请求 所有游戏
      if (this.originalList.length === 0) {
        console.log("加载游戏+++++++++++++++++++++++++++++++++++");
        const gameListData = await http.gameList();
        if (gameListData.code != 0) return;
        this.originalList = gameListData.data;
      }

      // 请求 火爆中奖信息
      if (noticeStore().notice.length === 0) {
        // await this.setNotice();
      }

      this.setPackageData();
    },
    setOriginalList() {
      this.originalList = [];
    },
    // 处理数据
    setPackageData() {
      // 循环类名
      let packageData = <Array<any>>[]; // 封装过后的数据
      for (let i = 0; i < this.gameClassData.length; i++) {
        let obj = <any>{
          ...this.gameClassData[i],
          gameList: [],
          proIdData: [],
        };
        //循环游戏列表 取类名 存在的游戏数据
        for (let j = 0; j < this.originalList.length; j++) {
          if (
            this.originalList[j].gameClassId.find(
              (item: any) => item === this.gameClassData[i].gameClassId // 过滤 游戏类别 包含的数据
            ) &&
            this.originalList[j].state != -1 // 过滤 游戏状态 -1 隐藏的 游戏
          ) {
            obj.gameList.push(this.originalList[j]);
          }
        }
        //游戏列表根据类classId 排序
        obj.gameList.sort(
          (a: any, b: any) =>
            b.sortInClass[obj.gameClassId] - a.sortInClass[obj.gameClassId]
        );
        for (let j = 0; j < obj.gameList.length; j++) {
          for (let i = 0; i < this.providerList.length; i++) {
            if (
              obj.gameList[j].providerId === this.providerList[i].providerId
            ) {
              obj.proIdData.push(this.providerList[i]);
            }
          }
        }
        obj.proIdData = Array.from(new Set(obj.proIdData));
        // 循环结束 push 数据
        packageData.push(obj);
      }

      // 从新赋值
      this.gameList = packageData;
    },
  },
});

// Need to be used outside the setup
export const useGameListStoreWithOut = () => {
  return gameListStore(store);
};
