/*
 * @Author: mojunshou 1637302775@qq.com
 * @Date: 2025-04-11 10:15:45
 * @LastEditors: mojunshou 1637302775@qq.com
 * @LastEditTime: 2025-04-29 15:08:59
 * @Description: 登录管理
 */
// LoginHandler.ts
import { oops } from 'db://oops-framework/core/Oops';
import { smc } from '../SingletonModuleComp';
import { CocosHandler } from './CocosHandler';
import { ServerHandler } from './ServerHandler';
import { DCHandler } from './DCHandler';

export class LoginHandler {
    private static _inst: LoginHandler;

    public static get inst(): LoginHandler {
        if (!this._inst) {
            this._inst = new LoginHandler();
        }
        return this._inst;
    }


    //获取当前版本状态
    async getVersionStatus() {
        const data = { method: 'system.debug.get' };
        const result = await CocosHandler.inst.sendMessageToAndroid(data, '获取版本状态');
        return JSON.parse(result);
    }

    //关闭安卓启动页
    closeAndroidSplash() {
        const data = { method: 'start.scene.hide' };
        CocosHandler.inst.sendMessageToAndroid(data, '关闭安卓启动页');
    }


    async getPrivacyStatus() {
        //这直接调关闭安卓
        this.closeAndroidSplash();
        const data = { method: 'privacy.grant.get' };
        const result = await CocosHandler.inst.sendMessageToAndroid(data, '获取隐私授权状态');
        return JSON.parse(result);
    }

    async savePrivacyStatus(status: boolean) {
        const data = {
            method: 'privacy.grant.set',
            param: JSON.stringify({ granted: status })
        };
        const result = await CocosHandler.inst.sendMessageToAndroid(data, '保存隐私授权状态');
        return JSON.parse(result);
    }

    openAgreement() {
        const type = smc.game.GameModel.protocolType;
        const url = type === 1 ? oops.config.game.gamePrivacyUrl : oops.config.game.gameProtocolUrl;
        const data = {
            method: 'system.browser.open',
            param: JSON.stringify({ url })
        };
        CocosHandler.inst.sendMessageToAndroid(data, '打开协议链接');
    }

    async wechatLogin() {
        const param = {
            callback: {
                onSuccess: 'LoginHandler.inst.wechatLoginSuccess',
                onFail: 'LoginHandler.inst.wechatLoginFail'
            }
        };
        const data = {
            method: 'auth.wechat',
            param: JSON.stringify(param)
        };
        await CocosHandler.inst.sendMessageToAndroid(data, '微信登录');
    }

    wechatLoginSuccess(str: string) {
        console.log('[微信] 登录成功', str);
        // this.wxLogin(str);
        //向服务器申请登录
        ServerHandler.inst.wxLogin(str);
        DCHandler.inst.reportData(3000004, 1001);
    }

    wechatLoginFail(str: number) {
        console.log('[微信] 登录失败', str);
        switch (str) {
            case -5:
                oops.gui.toast('登录失败，微信未安装~');
                break;
            default:
                oops.gui.toast('登录失败，请稍后再试~');
                break
        }
        DCHandler.inst.reportData(3000004, 1002);
    }

}

window["LoginHandler"] = LoginHandler;
