/**
 * 下注逻辑
 */
class Bet {
	private static _instance: Bet;
	static get instance(): Bet {
		if (!this._instance) {
			this._instance = new Bet();
		}
		return this._instance;
	}
	private gaming: boolean = false;
	private betHistory = {};
	public isBetSucc: boolean = false;
	private uiBet: UI_Bet;
	private _chipValue: number = 0;
	private chipPoolNodes = {};
	public rate: number = 0;
	public constructor() {

	}

	get chipValue(): number {
		return this._chipValue;
	}
	set chipValue(chipValue: number) {
		this._chipValue = chipValue;
	}

	setRate(rate: string): void {
		if (rate) {
			this.rate = NP.divide(parseInt(rate), 1e4);
			this.uiBet && this.uiBet.updateRate();
		}
	}

	addBetHistory(id: string, history: any): void {
		this.betHistory[id] = history;
	}
	popBetHistory(id: string): any {
		let history = this.betHistory[id];

		this.betHistory[id] = null;

		return history;
	}

	getBetView(): UI_Bet {
		return this.uiBet = UIController.instance.createView<UI_Bet>(UI_Bet);
	}

	setChipNodes(nodePool: UI_ChipItem[]): void {
		nodePool.forEach(node => {
			let key = `${node.chipValue}${node.size}`;
			if (!this.chipPoolNodes[key]) {
				this.chipPoolNodes[key] = [];
			}
			this.chipPoolNodes[key].push(node);
			if (node.type !== undefined) {
				Index.instance.removeChip(node.type, node.chipValue)
			}
		})
	}

	private getChipNodes(chipValue: number, size: number): UI_ChipItem {
		let key = `${chipValue}${size}`;

		if (!this.chipPoolNodes[key]) {
			this.chipPoolNodes[key] = [];
		}

		if (this.chipPoolNodes[key].length) {
			DEBUG && console.log('节点池读取');
			return this.chipPoolNodes[key].pop();
		} else {
			DEBUG && console.log('新建节点');
			return new UI_ChipItem(chipValue, size);
		}
	}

	largeChipIn(type: number, contaniner: egret.DisplayObjectContainer, stageX: number, stageY: number): void {
		let chipNode = this.getChipNodes(this.chipValue, CHIPSIZE.LARGE);

		chipNode.type = type;

		this.uiBet.largeChipIn(contaniner, stageX, stageY, chipNode);
	}

	largeChipIn2(type: number, chipValue: number, contaniner: egret.DisplayObjectContainer, stageX: number, stageY: number): void {
		let chipNode = this.getChipNodes(chipValue, CHIPSIZE.LARGE);

		chipNode.type = type;

		Index.instance.addChip(type, chipValue)

		this.uiBet.largeChipIn2(contaniner, stageX, stageY, chipNode);
	}


	smallChipIn(chipValue: number, contaniner: egret.DisplayObjectContainer, stageX: number, stageY: number): void {
		let chipNode = this.getChipNodes(chipValue, CHIPSIZE.SMALL);

		this.uiBet.smallChipIn(contaniner, stageX, stageY, chipNode);
	}

	//TODO render gamestart
	renderGameStart(): void {
		this.gaming = true;
		this.uiBet.renderGameStart();
		// this.chipValue = 0;
	}

	//TODO render lotteryInfo
	renderLotteryInfo(): void {
		this.gaming = false;
		this.uiBet.renderLotteryInfo();
	}

	//下注成功
	betSucc(doDeposit: any): void {
		let {Bet} = Lang.instance.lang();

		AlertMgr.instance.showAlert(Bet.betSucc);

		DEBUG && console.log('下注成功', doDeposit);

		Userinfo.instance.updateBalance(-doDeposit.amount);

		Userinfo.instance.updateGt(doDeposit.amount * this.rate);

		//重置本次下注
		this.uiBet.transBetEosToPhase();

		this.isBetSucc = true;

		if (this.gaming) {
			this.uiBet.allowBet(true)
			Index.instance.allowBet(true);
		}
	}

	//下注失败
	betFail(): void {
		if (this.gaming) {
			this.uiBet.allowBet(true)
			Index.instance.allowBet(true);
		}
	}

}