/*
 * @Author: mojunshou 1637302775@qq.com
 * @Date: 2025-03-19 16:23:51
 * @LastEditors: mojunshou 1637302775@qq.com
 * @LastEditTime: 2025-04-30 10:27:13
 * @Description: loading界面
 */
import { _decorator, Toggle } from "cc";
import { DeviceUtil } from "db://oops-framework/core/utils/DeviceUtil";
import { ModuleUtil } from "db://oops-framework/module/common/ModuleUtil";
import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { CCVMParentComp } from "../../../../../extensions/oops-plugin-framework/assets/module/common/CCVMParentComp";
import { AndroidEvent } from "../../common/config/AndroidEvent";
import { AD_TYPE } from "../../common/config/GameDefine";
import { GameEvent } from "../../common/config/GameEvent";
import { UIID } from "../../common/config/GameUIConfig";
import { ADHandler } from "../../common/manager/ADHandler";
import { LoginHandler } from "../../common/manager/LoginHandler";
import { smc } from "../../common/SingletonModuleComp";
import { EliminateViewComp } from "../../view/EliminateViewComp";
// import { EliminateView } from "../../view/eliminate/EliminateView";
const { ccclass, property } = _decorator;

/** 游戏资源加载 */
@ccclass('LoadingViewComp')
@ecs.register('LoadingView', false)
export class LoadingViewComp extends CCVMParentComp {
    /** VM 组件绑定数据 */
    data: any = {
        /** 加载资源当前进度 */
        finished: 0,
        /** 加载资源最大进度 */
        total: 0,
        /** 加载资源进度比例值 */
        progress: "0",
        /** 加载流程中提示文本 */
        prompt: "",
    };

    private progress: number = 0;

    start() {
        // oops.log.setTags(8)
        this.enter();
        this.addEvent();
        this.setButton();
        this.updateToggleState();
    }

    private addEvent() {
        this.on(AndroidEvent.AgreePrivacy, this.onAgreePrivacy, this);
        this.on(GameEvent.WechatLoginSuss, this.loginSuss, this)
        this.on(GameEvent.UserLogin, this.userLogin, this);
    }

    async enter() {
        if (DeviceUtil.isNative && DeviceUtil.isAndroid) {
            const state = await LoginHandler.inst.getPrivacyStatus();
            if (state.data.result) {
                //同意之后都加载一次启屏广告
                // CocosHandler.inst.ad_interstitial_start();
                ADHandler.inst.showAd(AD_TYPE.Start);
            } else {
                oops.gui.open(UIID.KindTips);
            }
        } else {
            //非原生，网页的
            this.setWxLoginBtnState(false);
            this.loadRes();
        }
    }



    private userLogin() {
        const isLogin = smc.account.AccountModel.isLogined;
        if (isLogin) {
            //登录过
            this.setWxLoginBtnState(false);
            this.loadRes();
        } else {
            this.setWxLoginBtnState(true);
        }
    }


    private loginSuss() {
        this.setWxLoginBtnState(false);
        this.loadRes();
    }

    private onAgreePrivacy() {
        oops.storage.set("agree", true);
        oops.gui.remove(UIID.KindTips);
        //如果是客户端就显示微信登录按钮
        if (DeviceUtil.isNative && DeviceUtil.isAndroid) {
            //const show
            this.setWxLoginBtnState(true);
            //
        } else {
            this.loadRes();
        }
    }

    /** 加载资源 */
    private async loadRes() {
        this.data.progress = 0;
        await this.loadCustom();
        this.loadGameRes();
    }

    /** 加载游戏本地JSON数据（自定义内容） */
    private loadCustom() {
        // 加载游戏本地JSON数据的多语言提示文本
        this.data.prompt = oops.language.getLangByID("loading_load_json");
    }

    /** 加载初始游戏内容资源 */
    private loadGameRes() {
        // 加载初始游戏内容资源的多语言提示文本
        this.data.prompt = oops.language.getLangByID("loading_load_game");
        oops.res.loadDir("game", this.onProgressCallback.bind(this), this.onCompleteCallback.bind(this));
    }

    /** 加载进度事件 */
    private onProgressCallback(finished: number, total: number, item: any) {
        this.data.finished = finished;
        this.data.total = total;

        var progress = finished / total;
        if (progress > this.progress) {
            this.progress = progress;
            this.data.progress = (progress * 100).toFixed(2);
        }
    }

    /** 加载完成事件 */
    private async onCompleteCallback() {
        // 获取用户信息的多语言提示文本
        this.data.prompt = oops.language.getLangByID("loading_load_player");
        await ModuleUtil.addViewUiAsync(smc.account, EliminateViewComp, UIID.Eliminate);
        // await ModuleUtil.addViewUiAsync(smc.account, EliminateView, UIID.Eliminate);
        ModuleUtil.removeViewUi(this.ent, LoadingViewComp, UIID.Loading);
    }


    /**
     * @description: 微信登录
     * @return {*}
     */
    private btn_wxlogin() {
        //是否同意了我们的隐私政策
        const agree = oops.storage.getBoolean("agree");
        if (!agree) {
            oops.gui.toast("请同意隐私政策")
            return;
        }
        LoginHandler.inst.wechatLogin();
    }

    /**
     * @description: 显示微信登录
     * @return {*}
     */
    setWxLoginBtnState(state: boolean) {
        const wxNode = this.node.getChildByName("login_node");
        if (wxNode) {
            wxNode.active = state;
        }

        //load条相反
        const loadNode = this.node.getChildByName("pro_progress");
        if (loadNode) {
            loadNode.active = !state;
        }
    }


    /**
     * @description:隐私政策toggle
     * @return {*}
     */
    onToggleChick(toggle: Toggle) {
        if (toggle.isChecked) {
            oops.storage.set("agree", true);
        } else {
            oops.storage.set("agree", false);
        }
    }


    //更新toggle状态
    updateToggleState() {
        const agree = oops.storage.getBoolean("agree");
        const toggle = this.node.getChildByPath("login_node/login_bg/tog_agree")!.uiToggle;
        if (agree) {
            toggle.isChecked = true;
        } else {
            toggle.isChecked = false;
        }
    }

    reset(): void { }


    /**
     * 保存关卡，分数，目标分数
     * 
     * 
    */
}