/*
 * @Author: mojunshou 1637302775@qq.com
 * @Date: 2025-03-20 17:00:12
 * @LastEditors: mojunshou 1637302775@qq.com
 * @LastEditTime: 2025-04-03 11:06:20
 * @Description: 微信提现页面
 */
import { _decorator, Node } 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 "../../common/config/GameUIConfig";
import { RichText } from "cc";
import { tween } from "cc";
import { Vec3 } from "cc";

const { ccclass, property } = _decorator;

/** 视图层对象 */
@ccclass('WechatWithdrawalViewComp')
@ecs.register('WechatWithdrawalView', false)
export class WechatWithdrawalViewComp extends CCComp {
    data: any = {
        nickName: "",
        money: 0,
        channel: "微信零钱"
    }

    @property(RichText)
    private richText: RichText = null!;

    @property(Node)
    private node_notice: Node = null!;



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

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


    private btn_back() {
        oops.gui.remove(UIID.WechatWithdraw);
    }

    private btn_record() {
        oops.gui.open(UIID.WithdrawRecord);
    }


    //更新滚动公告
    updateNotice() {
        this.node_notice.active = true;
        const num = oops.random.getRandomInt(1, 20)
        //随机1-500的数，有小数保留两位
        const randomNum = Math.random() * 500;
        const numStr = randomNum.toFixed(2);
        let name = this.getRandomChineseName();
        let str = "恭喜<color=#bcdc36>$y</color>收集<color=#bcdc36>$n</color>金砖，到账<color=#bcdc36>$m元</color>";
        //正则替换str里边的$y，$n，$m
        let str1 = str.replace(/\$y/g, name);
        str1 = str1.replace(/\$n/g, num + "块");
        str1 = str1.replace(/\$m/g, numStr);
        this.richText.string = str1;
        this.node_notice.setPosition(new Vec3(0, -40, 0));
        tween(this.node_notice)
            .to(4, { position: new Vec3(0, 40, 0) })
            .call(() => {
                this.updateNotice();
            })
            .start();
    }





    getRandomChineseName(): string {
        const surnames = ["王", "李", "张", "刘", "陈", "杨", "黄", "赵", "周", "吴", "徐", "孙", "胡", "朱", "高", "林", "何", "郭", "马", "罗"];
        const characters = "的一是了不在人有我他这中大来上国个到说们时用地为子就那和要出也得于里后自以会着对生能而多小学同见天去好她所然家前开";

        // 随机获取姓
        const surname = surnames[Math.floor(Math.random() * surnames.length)];

        // 生成 1 到 3 个字的名字
        const nameLength = Math.floor(Math.random() * 3) + 1;
        let givenName = "";
        for (let i = 0; i < nameLength; i++) {
            givenName += characters[Math.floor(Math.random() * characters.length)];
        }

        return surname + givenName;
    }


}