import ListModule from "..";
import ItemObject from "../objects/item";
import { Dialog, ShowItem } from "../state";
const artifacts = "./Artifacts.json";

function shuffleSelf(array, size) {
  let index = -1,
    length = array.length,
    lastIndex = length - 1;
  size = size === undefined ? length : size;

  while (++index < size) {
    // var rand = baseRandom(index, lastIndex),
    const rand = index + Math.floor(Math.random() * (lastIndex - index + 1));
    const value = array[rand];
    array[rand] = array[index];
    array[index] = value;
  }
  array.length = size;
  return array;
}

export default (listModule: ListModule) => {
  const getUrl = (url: string) => {
    return "./artifacts" + url.substring(1);
  };

  function loadImg(url: string, id: string, name: string): Promise<ShowItem> {
    return new Promise((resolve, reject) => {
      const m = new Image();
      m.src = url;
      m.onload = () => {
        resolve({
          id: id,
          w: m.width,
          h: m.height,
          url: url,
          offsetX: 0,
          offsetY: 0,
          x: 0,
          y: 0,
          z: 0,
          padding: 0,
          opacity: 0,
          currX: 0,
          currY: 0,
          show: false,
          offsetZ: 0,
          timer: 0,
          name,
          
          img: m,
        });
      };
    });
  }
  // function randomString(len?: number) {
  //   len = len || 32;
  //   const $chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
  //   const maxPos = $chars.length;
  //   let str = "";
  //   for (let i = 0; i < len; i++) {
  //     str += $chars.charAt(Math.floor(Math.random() * maxPos));
  //   }
  //   return str;
  // }
  return {
    shuffleSelf(arr, size) {
        return shuffleSelf(arr, size);
    },

    async initListData(wWidth: number, wHeight: number) {
      const items = await listModule.actions.loadData(wWidth, wHeight);

      // console.log(listModule.list);
      this.initLoadShowItems(items);
    },

    async loadData(wWidth: number, wHeight: number) {
      listModule.state.disperseX = Math.floor(wWidth / 4);
      listModule.state.disperseY = Math.floor(wHeight / 4);
      listModule.state.parentWidth = wWidth;
      listModule.state.parentHeight = wHeight;
      // listModule.state.scrollOffsetX = wWidth;

      // const tempDataArr = new Array(40).fill(1);
      // tempDataArr.map((e, index) => {
      //   const randomIndex = Math.round(Math.random() * (items.length - 1));
      //   tempDataArr[index] = items[randomIndex];
      // });
      //@ts-ignore
      listModule.list = await listModule.http.request(artifacts, {
        method: "GET",
        baseURL: "",
      });

      const ps = listModule.list.map((item, index) =>
        loadImg(item.thumbnail, item._id, item.name)
      );
      const rets = await Promise.all(ps);

      return rets;
    },

    async initLoadShowItems(rets :any[]=[]) {
 
      const totalRow = listModule.state.totalRow;
      // const paddingH = listModule.state.paddingH;
      // const paddingW = listModule.state.paddingW;

      // const ItemWidth = Math.floor(
      //   (wWidth - (totalRow + 1) * paddingH) / totalRow
      // );
      // let MaxContentWidth = 0;

      // rets.forEach((item) => {
      //   item.w = ItemWidth;
      //   item.w += Math.floor(0.5 * Math.random() * item.w);
      //   item.h = Math.floor((item.h * ItemWidth) / item.w);
      //   MaxContentWidth += item.w;
      // });
      // MaxContentWidth += (rets.length + 1) * paddingW;

      for (let i = 0; i < totalRow; i++) {
        // const y = (i + 1) * paddingH + (0.5 + i) * ItemWidth;

        //随机排序下
        shuffleSelf(rets, rets.length); 

        let offset = 0;
        rets.forEach((item, index) => {
          const it = JSON.parse(JSON.stringify(item));
          // it.y = y;
          // it.x = (index + 1) * paddingW + offset + item.w * 0.5;
          // offset += item.w;
          listModule.state.items.push(it);
        });
      }
      // listModule.state.itemTotalWidth = MaxContentWidth;
      // listModule.actions.updateItem();
      return rets;
    },

    async loadPack3d(item: ItemObject) {
      let meshId = item.meshId;
      if (!meshId) return;

      //@ts-ignore
      let ret = await listModule.http.request(meshId, { baseURL: "" });
      console.log(ret);

      return ret;
    },
  };
};
