/*
 * @Author: mojunshou 1637302775@qq.com
 * @Date: 2025-03-20 15:40:20
 * @LastEditors: mojunshou 1637302775@qq.com
 * @LastEditTime: 2025-04-29 16:48:45
 * @Description:设置界面 
 */
import { _decorator } from "cc";
import { oops } from "db://oops-framework/core/Oops";
import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { CCComp } from "db://oops-framework/module/common/CCComp";
import { UIID } from "../config/GameUIConfig";
import { CCVMParentComp } from "db://oops-framework/module/common/CCVMParentComp";
import { Toggle } from "cc";
import { Account } from "../../account/Account";
import { ImageAsset, Texture2D, SpriteFrame, Sprite } from "cc";
import { IRemoteOptions, resLoader } from "db://oops-framework/core/common/loader/ResLoader";
import { Button } from "cc";
import { smc } from "../SingletonModuleComp";
import { CocosHandler } from "../manager/CocosHandler";
import { LoginHandler } from "../manager/LoginHandler";
import { UITransform } from "cc";
import { Size } from "cc";
import { GameEvent } from "../config/GameEvent";
import { DCHandler } from "../manager/DCHandler";
const { ccclass, property } = _decorator;

/** 视图层对象 */
@ccclass('SettingViewComp')
@ecs.register('SettingView', false)
export class SettingViewComp extends CCVMParentComp {

    data: any = {
        head_url: "",
        uid: 100000,
    }



    @property(Button)
    private musicBtn: Button = null!;

    @property(Button)
    private effectBtn: Button = null!;

    private _musicState: boolean = true;
    private _effectState: boolean = true;



    /** 视图层逻辑代码分离演示 */
    start() {
        // const entity = this.ent as ecs.Entity;         // ecs.Entity 可转为当前模块的具体实体对象
        this.setButton();
        this.updateBtnState();
        this.updateHead();
        DCHandler.inst.reportData(3001000);

    }

    /** 视图对象通过 ecs.Entity.remove(SettingViewComp) 删除组件是触发组件处理自定义释放逻辑 */
    reset() {
        this.node.destroy();
    }


    private btn_about() {
        oops.gui.open(UIID.AboutUs);
        DCHandler.inst.reportData(3001004);
    }


    private btn_close() {
        oops.gui.remove(UIID.Setting);
        oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
        DCHandler.inst.reportData(3001001);
    }


    //更新按钮状态
    private updateBtnState() {
        const music_state = oops.storage.get("music_state");
        const effect_state = oops.storage.get("effect_state");
        if (!music_state) {
            // 将字符串转换为布尔值
            let music_state_bool = true;
            this._musicState = music_state_bool;
            oops.storage.set("music_state", true);
        } else {
            let music_state_bool = music_state == "true" ? true : false
            this._musicState = music_state_bool;
            oops.storage.set("music_state", music_state_bool);
        }
        if (!effect_state) {
            oops.storage.set("effect_state", true);
            let effect_state_bool = true;
            this._effectState = effect_state_bool;
        } else {
            //存在
            let effect_state_bool = effect_state == "true" ? true : false;
            this._effectState = effect_state_bool;
            oops.storage.set("effect_state", effect_state_bool);
        }
        oops.audio.switchMusic = this._musicState;
        oops.audio.switchEffect = this._effectState;

        const music_on = this.musicBtn.node.getChildByPath("btn_on")!;
        const music_off = this.musicBtn.node.getChildByPath("btn_off")!;
        music_on.active = this._musicState;
        music_off.active = !this._musicState;

        const btn_on = this.effectBtn.node.getChildByPath("btn_on")!;
        const btn_off = this.effectBtn.node.getChildByPath("btn_off")!;
        btn_on.active = this._effectState;
        btn_off.active = !this._effectState;
    }


    //更新头像
    private updateHead() {
        this.data.uid = smc.account.AccountModel.uid;
        let url = smc.account.AccountModel.headUrl;
        let spriteNode = this.node.getChildByPath("Bg/btn_head/Mask/sp_head");
        if (spriteNode) {
            const uiTransform = spriteNode.getComponent(UITransform);
            uiTransform?.setContentSize(new Size(110, 110))
            let sprite = spriteNode.getComponent(Sprite);
            if (sprite) {
                console.log("更新头像啦")
                var opt: IRemoteOptions = { ext: ".png" };
                var onComplete = (err: Error | null, data: ImageAsset) => {
                    const texture = new Texture2D();
                    texture.image = data;
                    const spriteFrame = new SpriteFrame();
                    spriteFrame.texture = texture;
                    sprite.spriteFrame = spriteFrame;
                }
                resLoader.loadRemote<ImageAsset>(url, opt, onComplete);
            }
        }
    }


    //打开音乐
    private btn_music() {
        this._musicState = !this._musicState;
        oops.storage.set("music_state", this._musicState);
        oops.audio.switchMusic = this._musicState;
        if (this._musicState) {
            DCHandler.inst.reportData(3001002);
            oops.audio.playMusicLoop("common/audios/bgm");
        } else {
            DCHandler.inst.reportData(3001003);
        }
        const btn_on = this.musicBtn.node.getChildByPath("btn_on")!;
        const btn_off = this.musicBtn.node.getChildByPath("btn_off")!;
        btn_on.active = this._musicState;
        btn_off.active = !this._musicState;
    }

    //打开音效
    private btn_effect() {
        this._effectState = !this._effectState;
        oops.storage.set("effect_state", this._effectState);
        oops.audio.switchEffect = this._effectState;

        const btn_on = this.effectBtn.node.getChildByPath("btn_on")!;
        const btn_off = this.effectBtn.node.getChildByPath("btn_off")!;
        btn_on.active = this._effectState;
        btn_off.active = !this._effectState;
    }


    //隐私协议
    private btn_privacy() {
        smc.game.GameModel.protocolType = 1;
        LoginHandler.inst.openAgreement();
    }

    //用户协议
    private btn_protocol() {
        smc.game.GameModel.protocolType = 2;
        LoginHandler.inst.openAgreement();
    }

}