import { _decorator, Component, Label, Node, Sprite, tween, UITransform, Vec3, view } from 'cc';
import { GameDataManager } from 'db://assets/Loading/scripts/manager/GameDataManager';
import { SLOT_BAR_EVENT, SYS_GIFT } from './Define';
import { LocalizedSprite } from 'db://assets/Loading/scripts/i18n/LocalizedSprite';
const { ccclass, property } = _decorator;

@ccclass('SysGift')
export class SysGift extends Component {
    /**赠送确认*/
    @property(Node)
    confirm: Node = null;
    /**结算确认*/
    @property(Node)
    settle: Node = null;
    /**顶部展示*/
    @property(Node)
    info: Node = null;

    /**免费旋转期间，投注固定*/
    fixedBet: -1;

    totalWin: -1;
    /**info 中数字最大显示长度*/
    fixWidth_info: number = 250;
    /**settle 中数字最大显示长度*/
    fixWidth_settle: number = 250;


    onLoad(): void {
        this.hideAll();

        let sp_win = this.info.getChildByName("sysgift_win");
        let scale = 116 / sp_win.getComponent(Sprite).spriteFrame.width;
        sp_win.setScale(scale, scale, scale);
    }

    hideAll() {
        this.confirm.active = false
        this.settle.active = false
        this.info.active = false
        this.node.active = true
    }

    /**确认领取free spin, 下注额固定其他控件半透明且不可交互*/
    onClickContinue() {

        this.hide(this.confirm)
        let frb = GameDataManager.instance.frb
        if (!frb) return;

        let txt_spin_num = this.info.getChildByName("count")
        txt_spin_num.getComponent(Label).string = frb.Ongoing.Frn.toLocaleString()

        // let sp_win = this.info.getChildByName("sysgift_win")
        let txt_win = this.info.getChildByName("win")

        let num = Math.round(frb.Ongoing.Fra * 100 + 1e-6) / 100;
        txt_win.getComponent(Label).string = num.toLocaleString('en-US', {
            minimumFractionDigits: 2,
            maximumFractionDigits: 2
        })

        this.info.active = true;

        this.node.emit(SYS_GIFT.CLICK_CONTINUE, this.fixedBet);
    }

    onClickConfirm() {
        GameDataManager.instance.frb.Finished = null;
        this.hide(this.settle);
        this.node.emit(SYS_GIFT.SETTLE_CONTINUE);
    }

    /** 打开confirm的弹窗 */
    showOngingPopup() {
        if (this.confirm.active) return;
        let frb = GameDataManager.instance.frb;
        if (!frb) return;
        this.fixedBet = frb.Ongoing.Bet
        if (frb.Ongoing.Popup) {
            this.confirm.getChildByName("count").getComponent(Label).string = frb.Ongoing.Frn.toLocaleString()
            this.show(this.confirm);
        }
    }


    /** 打开settle的win */
    showSysFreeWinSettle() {
        let frb = GameDataManager.instance.frb;
        if (!frb) return;
        if (frb.Finished?.Popup) {
            let win = this.settle.getChildByName('win');
            let num = Math.round(frb.Finished.Fra * 100 + 1e-6) / 100;
            win.getComponent(Label).string = num.toLocaleString('en-US', {
                minimumFractionDigits: 2,
                maximumFractionDigits: 2
            })
            this.show(this.settle);
            this.info.active = false;
        }
    }

    /** 设置info的count */
    handleSysInfoFreeCount(countNum: number) {
        let frb = GameDataManager.instance.frb;
        if (!frb) return;
        let count = this.info.getChildByName('count');

        // if (frb.Finished == null) {
        count.getComponent(Label).string = (countNum).toLocaleString();
        this.show(count);
        // } else {
        //     count.getComponent(Label).string = (countNum).toLocaleString();
        //     this.show(count);
        // }
    }


    /** 设置info的win */
    handleSysInfoFreeWin() {
        let frb = GameDataManager.instance.frb;
        if (!frb) return;
        if (frb.Finished == null && frb.Ongoing?.Popup == false) {
            let win = this.info.getChildByName("win")
            let num = Math.round(frb.Ongoing.Fra * 100 + 1e-6) / 100
            win.getComponent(Label).string = num.toLocaleString('en-US', {
                minimumFractionDigits: 2,
                maximumFractionDigits: 2
            })
            this.show(win);
            this.info.active = true;
        }
    }

    show(node: Node, scale: number = 1) {
        node.active = true
        tween(node).to(0.1, { scale: new Vec3(0.8, 0.8, 1) })
            .to(0.2, { scale: new Vec3(1.1, 1.1, 1) })
            .to(0.1, { scale: new Vec3(scale, scale, scale) })
            .start()
    }

    hide(node) {
        tween(node).to(0.1, { scale: new Vec3(1.1, 1.1, 1) })
            .to(0.2, { scale: new Vec3(0.8, 0.8, 1) })
            .call(() => {
                node.active = false
                node.scale = new Vec3(1, 1, 1)
            })
            .start()
    }

}

