/*
 * @Author: mojunshou 1637302775@qq.com
 * @Date: 2025-03-21 14:43:24
 * @LastEditors: mojunshou 1637302775@qq.com
 * @LastEditTime: 2025-04-28 20:41:55
 * @Description: 游戏通关弹窗
 */
import { Label, Node } from 'cc';
import { _decorator } from 'cc';
import { oops } from 'db://oops-framework/core/Oops';
import { DeviceUtil } from 'db://oops-framework/core/utils/DeviceUtil';
import { GameComponent } from "db://oops-framework/module/common/GameComponent";
import { AD_TYPE } from '../common/config/GameDefine';
import { UIID } from '../common/config/GameUIConfig';
import { CocosHandler } from '../common/manager/CocosHandler';
import { smc } from '../common/SingletonModuleComp';
import { ADHandler } from '../common/manager/ADHandler';
import { GameEvent } from '../common/config/GameEvent';
import { ServerHandler } from '../common/manager/ServerHandler';
import { DCHandler } from '../common/manager/DCHandler';


const { ccclass, property } = _decorator;

/** 显示对象控制 */
@ccclass('GamePassView')
export class GamePassView extends GameComponent {
    //提示Node
    @property(Label)
    private lab_tips: Label = null!;
    @property(Node)
    private showBtn1: Node = null!;

    @property(Node)
    private showBtn2: Node = null!;


    @property([Label])
    private lab_list: Label[] = [];

    private time: number = 3;  //倒数时间

    private isAuto: boolean = true;  //服务器传是否展示按钮
    callback: Function = null!;

    protected start() {
        DCHandler.inst.reportData(3000500);
        this.setButton();
        this.isAuto = true;
        this.setData();
        this.addEventList();
    }

    addEventList() {
        oops.message.on(GameEvent.showCoinAnimation, this.showCoinAnimation, this);
    }


    showCoinAnimation() {
        if (oops.gui.has(UIID.GamePass)) {
            oops.gui.remove(UIID.GamePass);
        }
    }


    private setData() {
        if (smc.game.GameModel.eventType) {
            const curLevel = smc.account.AccountModel.curLevel;
            DCHandler.inst.reportData(3000100, curLevel);
        }
        this.isAuto = smc.game.GameModel.passViewInfo.doubleReward;
        this.showBtn1.active = this.isAuto;
        this.showBtn2.active = !this.isAuto;

        if (this.isAuto) {
            this.setAutoReceive();
        }
        const list = smc.game.GameModel.passViewInfo.showReward;
        list.forEach((item, index) => {
            if (this.lab_list[index]) {
                this.lab_list[index].string = item.propNum + "";
            }
        })
    }


    //开心收下正常领取
    private btn_none() {
        if (DeviceUtil.isNative && DeviceUtil.isAndroid) {
            ServerHandler.inst.getPassRewards();
            DCHandler.inst.reportData(3000501);
        }
        oops.gui.remove(UIID.GamePass);
        DCHandler.inst.reportData(3000503);
    }

    //十倍领取
    private btn_more() {
        this.isAuto = false;
        if (DeviceUtil.isNative && DeviceUtil.isAndroid) {
            smc.game.GameModel.viewType = "pass_reward";
            ADHandler.inst.showAd(AD_TYPE.Pass_Receive);
            oops.gui.remove(UIID.GamePass);
            DCHandler.inst.reportData(3000503);
        } else {
            oops.gui.remove(UIID.GamePass);
        }
    }


    //设置自动领取
    setAutoReceive() {
        let time = this.time;
        this.lab_tips.string = `${time}秒后自动领取`;
        this.callback = function () {
            this.lab_tips.string = `${time}秒后自动领取`;
            time--;
            if (time <= 0 && this.isAuto) {
                this.btn_more();
                if (this.callback) {
                    this.unschedule(this.callback);
                }
                DCHandler.inst.reportData(3000502);
            }
        }
        this.schedule(this.callback, 1);

    }




} 