import { _decorator, Button, Label, Node, Sprite } from "cc";
import Scene from "db://assets/Module/Scene";
import { MainGame } from "./MainGame/MainGame";
import { changeScene, showPanel, showToast } from "db://assets/Module/UIFast";
import store from "../store/store";
import { observer, render } from "../store/decorators";
import { _asyncThrottle, strFormat } from "../Utils/Utils";
import { UIMgr } from "../../Module/UIMgr";
import gameStore from "../store/gameStore";
import GameResultPanel from "../Panels/GameResultPanel";
import GamePosterPanel from "../Panels/GamePosterPanel";
import ExchangeSuccess from "db://assets/Scripts/Panels/ExchangeSuccess";
import { RankScence } from "./RankScene";
import { BlessingBagPage } from "db://assets/Scripts/Scenes/BlessingBagPage";
import { PropPage } from "db://assets/Scripts/Scenes/PropPage";
import { AudioMgr } from "../../core_tgx/base/AudioMgr";
import SignPanel from "../Panels/SignPanel";
import { PointDetailScene } from "./PointDetailScene";
import { MyBagPanel } from "../Panels/MyBagPanel";
import RulePanel from "../Panels/RulePanel";
import RankPrizePanel from "../Panels/RankPrizePanel";
import SnakeSkin from "../Panels/SnakeSkin";
import shareStore from "../store/shareStore";
import { sendWebNetWithToken, WebNetName } from "../Utils/WebNet/WebNet";
import { getUrlParams } from "../Utils/WebNet/web/webTools";

const { ccclass, property } = _decorator;

@observer
@ccclass('HomeScene')
export class HomeScene extends Scene {

  static bundle: string = "HomeScene";
  static skin: string = "HomeScene";
  // static group: string[] = ["HomeScene"];


  @property(Node) startBtn: Node = null;
  @property(Node) ruleBtn: Node = null;
  @property(Node) rankBtn: Node = null;
  @property(Node) rankInfo1: Node = null;
  @property(Node) rankInfo2: Node = null;
  @property(Node) rankInfo3: Node = null;
  @property(Node) rankNone: Node = null;
  @property(Node) rankList: Node = null;
  @property(Node) blessBagBtn: Node = null;
  @property(Node) PropBtn: Node = null;
  @property(Node) signBtn: Node = null;
  @property(Node) pointBtn: Node = null;
  @property(Node) bagBtn: Node = null;
  @property(Node) shareBtn: Node = null;

  @property(Label) userName: Label;
  @property(Label) pointNum: Label;
  @property(Label) fuDaiText: Label;
  @property(Label) signTxt: Label;

  @property(Sprite) userImg: Sprite;

  // 默认头像
  avatar = "https://yun.duiba.com.cn/polaris/img.546cbcd3811aa2265e50d495c078108a217450b7.png"

  onLoad() {
    this.startBtn.on(Button.EventType.CLICK, this.clickStart, this);
    this.ruleBtn.on(Button.EventType.CLICK, this.clickRule, this);
    this.rankBtn.on(Button.EventType.CLICK, this.clickRank, this);
    this.rankNone.on(Button.EventType.CLICK, this.clickRank, this);
    this.blessBagBtn.on(Button.EventType.CLICK, this.clickBag, this);
    this.PropBtn.on(Button.EventType.CLICK, this.clickProp, this);
    this.signBtn.on(Button.EventType.CLICK, this.clickSign, this);
    this.pointBtn.on(Button.EventType.CLICK, this.clickDetail, this);
    this.bagBtn.on(Button.EventType.CLICK, this.clickMyBag, this);
    this.shareBtn.on(Button.EventType.CLICK, this.clickShare, this);
  }

  async start() {

    store.doAssist();

    await store.getTaskInfo();
    await store.updateIndex();

    const {
      signPopFlag,
      rankPopList,
      newGetSpId,
      newGetSpImage,
      newGetSpName
    } = store.homeInfo || {};

    if (signPopFlag) { // 签到弹窗
      showPanel(SignPanel);
    }
    if (rankPopList.length > 0) { // 排行榜开奖弹窗
      // showPanel(RankPrizePanel);
      store.setRankPop()
    }

    if (newGetSpId == "sp_skin_snake_year") {
      showPanel(SnakeSkin, { newGetSpImage, newGetSpName })
    }

    UIMgr.ins.preloadScene(MainGame);
  }

  @render
  render() {
    const {
      nickname = "",
      creditsNum = 0,
      rankInfos = [],
      continueSignDays = 0,
      luckBenefitDesc ="",
      avatar
    } = store.homeInfo || {};

    this.userName.string = strFormat(nickname || "鸣鸣很忙用户", 8);
    this.pointNum.string = `${creditsNum > 9999999 ? "9999999+" : creditsNum}`;
    this.userImg.spriteFrame = avatar || this.avatar;
    this.signTxt.string = `连续签到${continueSignDays || 0}天`;
    this.fuDaiText.string = luckBenefitDesc;

    if (rankInfos && rankInfos.length > 0) {
      this.rankList.active = true;
      this.rankNone.active = false;
      rankInfos.map((item, index) => {
        const node = this[`rankInfo${index + 1}`]

        node.active = true
        node.getChildByPath("mask/rank_img").getComponent(Sprite).spriteFrame = item.avatar || this.avatar;
        node.getChildByName("rank_name").getComponent(Label).string = strFormat(item.nickname || "鸣鸣很忙用户", 8);
      })
    } else {
      this.rankList.active = false;
      this.rankNone.active = true;
    }
  }


  checkActTime = (isAllow = false) => {
    const { actStartTime, actEndTime, currentTime } = store.homeInfo || {};
    if (actStartTime > currentTime) {
      showToast("活动未开始")
      return false
    }
    if (actEndTime < currentTime && !isAllow) {
      showToast("活动已结束")
      return false
    }
    return true
  }

  clickStart = _asyncThrottle(async () => {
    if (!this.checkActTime()) return;

    await gameStore.startGame();
    changeScene(MainGame);

  });

  clickRule = _asyncThrottle(async () => {
    if (!this.checkActTime(true)) return;

    showPanel(RulePanel);
  });

  clickRank = _asyncThrottle(async () => {
    if (!this.checkActTime(true)) return;

    await changeScene(RankScence);
  });

  clickBack = _asyncThrottle(() => {
  });

  clickBag = _asyncThrottle(async () => {
    if (!this.checkActTime()) return;

    await changeScene(BlessingBagPage)
  })

  clickSign = _asyncThrottle(async () => {
    if (!this.checkActTime()) return;
    showPanel(SignPanel);
  });

  clickMyBag = _asyncThrottle(async () => {
    if (!this.checkActTime()) return;
    showPanel(MyBagPanel);
  });

  clickProp = _asyncThrottle(async () => {
    if (!this.checkActTime()) return;
    await changeScene(PropPage)
  })

  clickDetail = _asyncThrottle(async () => {
    if (!this.checkActTime()) return;
    await changeScene(PointDetailScene);
  });

  clickShare = _asyncThrottle(async () => {
    if (!this.checkActTime()) return;

    shareStore.doShare(true)
  });
}

