

import { EventTouch, Node, Toggle, Tween, Vec3, _decorator, easing, tween } from "cc";
import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { CCComp } from "../../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
import { UIID } from "../../common/config/GameUIConfig";
import { SingletonModuleComp, smc } from "../../common/SingletonModuleComp";
import { AdManager } from "../../../platform/ad/AdManager";
import { CheckpointCheckFailComp } from "../bll/CheckpointCheck";
import { AddCellOperationComp } from "../bll/AddCellOperation";
import { DataSdk, REPORT_EVENT } from "../../common/utils/datasdk";


const { ccclass, property } = _decorator;

/** 角色信息界面 */
@ccclass('CheckpointRecoveryViewComp')
@ecs.register('CheckpointRecoveryView', false)
export class CheckpointRecoveryViewComp extends CCComp {

    private btnClose
    private btnGiveup
    private btnGet
    private adFinished

    onAdded(args: any) {
        console.log(args);
    }

    onLoad() {
        this.btnClose = this.node.getChildByPath("BG/btn_close")
        this.btnGiveup = this.node.getChildByPath("BG/btn_giveup")
        this.btnGet = this.node.getChildByPath("BG/btn_get")
        this.btnClose.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.btnGiveup.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.btnGet.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.adFinished = false

        const window = this.node.getChildByName("BG")
        window.setScale(new Vec3(0, 0, 1));
        // 使用Tween来实现动画效果
        Tween.stopAllByTarget(window); // 停止该节点上的所有其他Tween
        tween(window)
            // .delay(0.1)
            .to(0.3, { scale: new Vec3(0.5, 0.5, 1) }, { easing: easing.backOut }) // 缩放动画
            .start(); // 开始动画
        DataSdk.reportEvent(REPORT_EVENT.OPEN_RECOVERY, ecs.getSingleton(SingletonModuleComp)?.account?.AccountModel?.lv)
    }

    private onTouchEnd(event: EventTouch) {
        switch (event.target.name) {
            case "btn_close":
                if (!this.adFinished) smc.initialize.account.checkpoint.add(CheckpointCheckFailComp)
                oops.gui.remove(UIID.Recovery);
                break;
            case "btn_giveup":
                smc.initialize.account.checkpoint.add(CheckpointCheckFailComp)
                oops.gui.remove(UIID.Recovery);
                break;
            case "btn_get":
                // ecs.getSingleton(SingletonModuleComp).account.checkpoint.add(AddCellOperationComp)
                // ecs.getSingleton(SingletonModuleComp).account.checkpoint.CheckpointModelBase.vm.addCellCount+=1
                // oops.gui.remove(UIID.Recovery);
                DataSdk.reportEvent(REPORT_EVENT.CLICK_RECOVERY, JSON.stringify({
                    lv: ecs.getSingleton(SingletonModuleComp)?.account?.AccountModel?.lv,
                    peopleCount: ecs.getSingleton(SingletonModuleComp).account?.checkpoint?.CheckpointModel?.peopleCount
                }))
                DataSdk.reportEvent(REPORT_EVENT.START_ADD_CELL_AD, ecs.getSingleton(SingletonModuleComp)?.account?.AccountModel?.lv)
                AdManager.getInstance().showRewardVideoAd(() => {
                    console.log('成功看完广告')
                    ecs.getSingleton(SingletonModuleComp).account.checkpoint.add(AddCellOperationComp)
                    ecs.getSingleton(SingletonModuleComp).account.checkpoint.CheckpointModelBase.vm.addCellCount += 1
                    this.adFinished = true
                    oops.gui.remove(UIID.Recovery);
                    DataSdk.reportEvent(REPORT_EVENT.END_ADD_CELL_AD, ecs.getSingleton(SingletonModuleComp)?.account?.AccountModel?.lv)
                })
                break;
        }

        event.propagationStopped = true;
    }

    reset() {
        this.node.destroy();
    }
}