
import { _decorator, Component, EditBox, EventHandle, EventHandler, js, Label, Node, utils } from 'cc';
import { UtilsPanel } from '../../scene/script/UtilsPanel';
import { Pay } from './Pay';
import { UtilsFormat } from '../../scene/script/UtilsFormat';

const { ccclass } = _decorator;

@ccclass('PayCustom')
export class PayCustom extends Component {
    
    private lb_gold: Label = null;
	private edit_money: EditBox = null;

    private money = 0;
	protected onLoad(){
		UtilsPanel.getAllNeedCom(this, this.node, false);
        let handler = new EventHandler();
        handler.target = this.node;
        handler.component = js.getClassName(this);
        handler.handler = "onInputEnd";
        this.edit_money.editingDidEnded.push(handler);
        // this.edit_money.textChanged.push(handler)
	}
    protected start(): void {
        this.money = Pay.data_shop.MinMoney;
        this.edit_money.string = this.money + "";
        this.refreshGold();
    }
    // private checkNum(text: string){
    //     let index = text.indexOf(".");
    //     let revise = false;
    //     if(index > -1){
    //         revise = true;
    //         text = text.replace(".", "");
    //     }
    //     let num = parseInt(text);
    //     if(num <  Pay.data_shop.MinMoney){
    //         revise = true;
    //         num = Pay.data_shop.MinMoney
    //         text = "" +  num;
    //     }
    //     if(revise){
    //         //@ts-ignore
    //         this.edit_money._impl._edTxt.value = text;
    //         this.edit_money.string = text;
    //     }
    //     if(num != this.money){
    //         this.money = num;
    //         this.refreshGold();
    //     }
    // }
    private onInputEnd(){
        let text = this.edit_money.string;
        let index = text.indexOf(".");
        let revise = false;
        if(index > -1){
            revise = true;
            text = text.replace(".", "");
        }
        let num = parseInt(text);
        if(num <  Pay.data_shop.MinMoney){
            revise = true;
            num = Pay.data_shop.MinMoney
            text = "" +  num;
        }
        if(revise){
            this.edit_money.string = text;
        }
        if(num != this.money){
            this.money = num;
            this.refreshGold();
        }
    }
    private refreshGold(){
        this.lb_gold.string = "" + (Pay.clacGold(this.money));
    }
	private onCloseClick(){
		this.node.destroy();
	}
    private onBuyClick(){
		Pay.buy(this.money);
	}

}
