/*
 * @Author: mojunshou 1637302775@qq.com
 * @Date: 2025-03-20 17:53:50
 * @LastEditors: mojunshou 1637302775@qq.com
 * @LastEditTime: 2025-04-30 10:32:38
 * @Description: 红包提现
 */
import { _decorator, Label, Node } from "cc";
import { oops } from "db://oops-framework/core/Oops";
import { DeviceUtil } from "db://oops-framework/core/utils/DeviceUtil";
import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { CCVMParentComp } from "db://oops-framework/module/common/CCVMParentComp";
import { GameEvent } from "../common/config/GameEvent";
import { UIID } from "../common/config/GameUIConfig";
import { ServerHandler } from "../common/manager/ServerHandler";
import List from "../ui/List";
import { RedPackeItemView } from "./RedPackeItemView";
import { smc } from "../common/SingletonModuleComp";
import { Format } from "../utils/Format";
import { DCHandler } from "../common/manager/DCHandler";

const { ccclass, property } = _decorator;

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

    @property(List)
    recordList: List = null!;

    @property(Label)
    lab_tips: Label = null!;

    @property(Label)
    lab_hbCoin: Label = null!;

    @property(Label)
    lab_wxCash: Label = null!;

    data: any = {
        tasklist: [],
        goldNum: 0,
        wxCash: 0,
        hbCoin: 0
    };
    start() {
        this.setButton();
        this.addEvent();
        DCHandler.inst.reportData(3000900);
        this.data.taskList = smc.game.GameModel.taskList;
        let wxCash = smc.game.GameModel.wxCash;
        let hbCoin = Format.formatRedPacketCoin(smc.account.AccountModel.hbCoin);
        this.data.wxCash = wxCash;
        this.data.hbCoin = hbCoin;
        this.data.goldNum = smc.account.AccountModel.goldCoin;
        if (this.data.taskList.length > 0) {
            this.recordList.numItems = this.data.taskList.length;
        }
        // this.lab_tips.string = `当前收集金砖${this.data.goldNum}块，红包10000元=人民币1元`;
    }

    addEvent() {
        oops.message.on(GameEvent.updateRedPackeTaskList, this.updateList, this);
        oops.message.on(GameEvent.updateHbAndWxCoin, this.updateCoin, this);
    }

    private onListRender(item: Node, idx: number) {
        const itemView: RedPackeItemView | null = item.getComponent(RedPackeItemView);
        if (itemView) {
            itemView.setItem(this.data.taskList[idx], idx);
        }
    }


    updateList() {
        this.data.taskList = smc.game.GameModel.taskList;
        let wxCash = smc.game.GameModel.wxCash;
        let hbCoin = Format.formatRedPacketCoin(smc.account.AccountModel.hbCoin);
        // this.lab_wxCash.string = `${wxCash}`;
        // this.lab_hbCoin.string = `${hbCoin}`;
        this.data.wxCash = wxCash;
        this.data.hbCoin = hbCoin;
        this.data.goldNum = smc.account.AccountModel.goldCoin;
        this.recordList.numItems = 0;
        if (this.data.taskList.length > 0) {
            this.recordList.numItems = this.data.taskList.length;
        }
        // this.lab_tips.string = `当前收集金砖${this.data.goldNum}块，红包10000元=人民币1元`;
        oops.gui.toast("领取成功");
    }

    updateCoin() {
        //console.log("更新红包币和微信币")
        let wxCash = smc.game.GameModel.wxCash;
        //console.log("wxCash", wxCash);
        let hbCoin = Format.formatRedPacketCoin(smc.account.AccountModel.hbCoin);
        // console.log("hbCoin", hbCoin);
        this.data.wxCash = wxCash;
        this.data.hbCoin = hbCoin;
    }

    private btn_back() {
        oops.gui.remove(UIID.RedPacketWithdraw);
        oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
        DCHandler.inst.reportData(3000901);
    }


    private btn_record() {
        if (DeviceUtil.isNative && DeviceUtil.isAndroid) {
            ServerHandler.inst.getRecordList();
            DCHandler.inst.reportData(3000902);
        } else {
            oops.gui.open(UIID.WithdrawRecord);
        }
    }

    private async btn_withdrawal() {
        //全部提现
        if (smc.game.GameModel.wxCash >= 0.1) {
            ServerHandler.inst.HbReward();
            DCHandler.inst.reportData(3000903, 1001);
        } else {
            oops.gui.toast("微信官方限制大于0.1元，才能提现~");
            DCHandler.inst.reportData(3000903, 1002);
        }
    }

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


    protected onDestroy(): void {
        oops.message.off(GameEvent.updateRedPackeTaskList, this.updateList, this);
        oops.message.off(GameEvent.updateHbAndWxCoin, this.updateCoin, this);
    }


    btn_text() {
        const info = { "ssid": "0cee6baebcb1413180e266d733510e38", "props": { "1007": 60, "9001": 5, "1005": 1889260, "1004": 5137068, "8001": 550, "1006": 9, "2001": 11, "1008": 30 }, "changes": { "8001": 500, "1004": -5000000 }, "money": 5.13, "transferStatus": 1, "outTransferNo": "2504281114186442061262848" }
        ServerHandler.inst.onHbReward(JSON.stringify(info));
    }
}