/*
 * @Author: mojunshou 1637302775@qq.com
 * @Date: 2025-03-06 15:56:14
 * @LastEditors: mojunshou 1637302775@qq.com
 * @LastEditTime: 2025-03-06 16:33:21
 * @Description: Cocos向安卓发送消息处理类
 */
// CocosHandler.ts
import { native } from 'cc';
import { _decorator, Component } from 'cc';
import { DeviceUtil } from 'db://oops-framework/core/utils/DeviceUtil';
const { ccclass } = _decorator;

@ccclass('CocosHandler')
export class CocosHandler extends Component {
    // 单例模式
    private static instance: CocosHandler;
    public static getInstance(): CocosHandler {
        if (!CocosHandler.instance) {
            CocosHandler.instance = new CocosHandler();
        }
        return CocosHandler.instance;
    }

    // 初始化通信
    // public initCommunication() {
    //     // 注册 Android 调用 Cocos 的方法
    //     if (window.jsb) {
    //         window.jsb.reflection.setNativeCallback('onAndroidMessage', this.onAndroidMessage.bind(this));
    //     }
    // }

    // 处理 Android 发送的消息
    private onAndroidMessage(jsonStr: string) {
        const data = JSON.parse(jsonStr);
        console.log('Cocos 收到 Android 消息:', data);

        // 处理逻辑
        // 例如：根据 data 的内容执行不同的操作，不是每个回调都要处理
    }

    // 发送消息到 Android
    public async sendMessageToAndroid(json: Object) {
        if (DeviceUtil.isAndroid && DeviceUtil.isNative) {
            let jsonStr = JSON.stringify(json);
            console.log("js传给安卓的数据打印>>>>", jsonStr);
            let result = await native.reflection.callStaticMethod("com/cocos/game/AndroidHandler", "onCocosMessage", "(Ljava/lang/String;)Ljava/lang/String;", jsonStr);
            //等待上边安卓返回数据，再执行下边代码
            if (result) {
                let android_result = JSON.parse(result);
                console.log("安卓返回数据打印>>>>>", android_result);
                return android_result;
            }
        }
    }


    async login(username: string, password: string) {
        //区分普通登录，微信登录，游客登录
        let obj = {
            method: "login",
            callback: "StartViewComp.androidCallJs",            //安卓完成业务的回调JS函数
            data: {
                username: username,
                password: password,
                type: 1   //登录类型
            }
        }
        let result = await this.sendMessageToAndroid(obj);
        return result;
    }



    //用户注册
    async user_register(username: string, password: string) {
        let obj = {
            method: "user_register",
            callback: "",
            data: {
                username: username,
                password: password
            }
        }
        let result = await this.sendMessageToAndroid(obj);
        return result;
    }

    //微信登录
    async wechat_login() {
        let obj = {
            method: "wechat_login",
            callback: "",
            data: {

            }
        }
        let result = await this.sendMessageToAndroid(obj);
        return result;
    }



    //用户提现
    async user_withdrawal(user_id: string) {
        let obj = {
            method: "user_withdrawal",
            callback: "",
            data: {


            }
        }
        let result = await this.sendMessageToAndroid(obj);
        return result;
    }

    //获取提现记录
    async get_withdrawal_record(user_id: string) {
        let obj = {
            method: "get_withdrawal_record",
            callback: "",
            data: {

            }
        }
        let result = await this.sendMessageToAndroid(obj);
        return result;
    }

    //获取用户信息
    async get_user_info(user_id: string) {
        let obj = {
            method: "get_user_info",
            callback: "",
            data: {
                user_id: user_id
            }
        }
        let result = await this.sendMessageToAndroid(obj);
        return result;
    }

    //现金提现
    async cash_withdrawal(user_id: string, amount: string) {
        let obj = {
            method: "cash_withdrawal",
            callback: "",
            data: {
                user_id: user_id,
                amount: amount
            }
        }
        let result = await this.sendMessageToAndroid(obj);
        return result;
    }


    //红包提现
    async red_packet_withdrawal(user_id: string, amount: string) {
        let obj = {
            method: "red_packet_withdrawal",
            callback: "",
            data: {
                user_id: user_id,
                amount: amount
            }
        }
        let result = await this.sendMessageToAndroid(obj);
        return result;
    }


    //复制文字
    async copy_text(text: string) {
        let obj = {
            method: "copy_text",
            callback: "",
            data: {
                text: text
            }
        }
        let result = await this.sendMessageToAndroid(obj);
        return result;
    }


    //获取远程CDN地址，这用和安卓交互

    //获取游戏数据信息--返回后才能进入游戏渲染数据，也可以登录后推送过来
    async get_game_data() {
        let obj = {
            method: "get_game_data",
            callback: "",
            data: {

            }
        }
        let result = await this.sendMessageToAndroid(obj);
        return result;
    }


    //

}
window['CocosHandler'] = CocosHandler.getInstance();