/*
 * @Author: mojunshou 1637302775@qq.com
 * @Date: 2025-04-15 20:16:16
 * @LastEditors: mojunshou 1637302775@qq.com
 * @LastEditTime: 2025-04-28 20:44:31
 * @Description: 继续二倍速
 */
import { _decorator, Component, Node } from 'cc';
import { oops } from 'db://oops-framework/core/Oops';
import { GameComponent } from 'db://oops-framework/module/common/GameComponent';
import { AD_TYPE } from '../common/config/GameDefine';
import { UIID } from '../common/config/GameUIConfig';
import { ADHandler } from '../common/manager/ADHandler';
import { smc } from '../common/SingletonModuleComp';
import { Label } from 'cc';
import { DCHandler } from '../common/manager/DCHandler';
import { GameEvent } from '../common/config/GameEvent';
const { ccclass, property } = _decorator;

@ccclass('keepDoubleSpeedView')
export class keepDoubleSpeedView extends GameComponent {
    private isAuto: boolean = false;

    @property(Label)
    private lab_time: Label = null!;

    private time: number = 3;
    private autoFunction: Function | null = null;
    start() {
        this.setButton();
        //设置3秒自启动
        this.isAuto = true;
        this.time = 3;
        this.lab_time.string = `${this.time}秒后自启动`;
        this.autoFunction = () => {
            this.time--
            this.lab_time.string = `${this.time}秒后自启动`;
            if (this.isAuto && this.time <= 0) {
                if (this.autoFunction) {
                    this.unschedule(this.autoFunction);
                    this.isAuto = false;
                    this.openAd();
                    oops.gui.remove(UIID.KeepSpeed);
                    DCHandler.inst.reportData(3000304);

                }
            }
        }
        this.schedule(this.autoFunction, 1);
    }


    private btn_open() {
        this.isAuto = false;
        this.openAd();
        oops.gui.remove(UIID.KeepSpeed);
        DCHandler.inst.reportData(3000303);
        oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
    }

    //打开广告
    private openAd() {
        smc.game.GameModel.viewType = "speed_reward";
        ADHandler.inst.showAd(AD_TYPE.Double_Speed_Receive);
        smc.game.GameModel.doubleSpeedAdCount++;
    }

    private btn_no() {
        this.isAuto = false;
        oops.gui.remove(UIID.KeepSpeed);
        ADHandler.inst.showAd("107");
        oops.message.dispatchEvent(GameEvent.updateGameState, "paused");
    }

    private btn_close() {
        this.isAuto = false;
        oops.gui.remove(UIID.KeepSpeed);
        //播放插屏广告
        ADHandler.inst.showAd("107");
        DCHandler.inst.reportData(3000305);
        oops.message.dispatchEvent(GameEvent.updateGameState, "paused");
    }

    protected onDestroy(): void {
        this.isAuto = false;
        if (this.autoFunction) {
            this.unschedule(this.autoFunction);
        }
    }

}


