

/*
 * @Author: dgflash
 * @Date: 2022-06-02 09:38:48
 * @LastEditors: dgflash
 * @LastEditTime: 2022-09-20 17:18:29
 */
import { EventTouch, Node, Toggle, Tween, UIOpacity, 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 } from "../../common/SingletonModuleComp";
import { InitCheckpointComp } from "../bll/InitCheckpoint";


const { ccclass, property } = _decorator;

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

    private btnClose
    private btnRestart
    private btnEffect
    private btnVibrate
    private btnSound

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

    onLoad() {
        this.btnClose = this.node.getChildByPath("BG/btn_close")
        this.btnRestart = this.node.getChildByPath("BG/btn_restart")
        this.btnEffect = this.node.getChildByPath("BG/btn_effect")
        this.btnVibrate = this.node.getChildByPath("BG/btn_vibrate")
        this.btnSound = this.node.getChildByPath("BG/btn_sound")
        this.btnClose.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.btnRestart.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.btnEffect.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.btnVibrate.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.btnSound.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);

        const setting = ecs.getSingleton(SingletonModuleComp).account.AccountModelSetting
        this.btnEffect.getComponent(Toggle)!.isChecked = setting.effectOn
        this.btnVibrate.getComponent(Toggle)!.isChecked = setting.vibrationOn
        this.btnSound.getComponent(Toggle)!.isChecked = setting.musicOn



        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(); // 开始动画

    }

    private onTouchEnd(event: EventTouch) {
        switch (event.target.name) {
            case "btn_restart":
                const checkpoint = ecs.getSingleton(SingletonModuleComp).account.checkpoint;
                checkpoint.add(InitCheckpointComp)
                break;
            case "btn_close":
                oops.gui.remove(UIID.Setting);
                break;
            case "btn_effect":
                ecs.getSingleton(SingletonModuleComp).account.AccountModelSetting.effectOn = !ecs.getSingleton(SingletonModuleComp).account.AccountModelSetting.effectOn
                break;
            case "btn_vibrate":
                ecs.getSingleton(SingletonModuleComp).account.AccountModelSetting.vibrationOn = !ecs.getSingleton(SingletonModuleComp).account.AccountModelSetting.vibrationOn
                break;
            case "btn_sound":
                ecs.getSingleton(SingletonModuleComp).account.AccountModelSetting.musicOn = !ecs.getSingleton(SingletonModuleComp).account.AccountModelSetting.musicOn
        }

        event.propagationStopped = true;
    }

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