/**
 * 主页逻辑  
 */
const BETAREA = {
	PLAYER: 0,
	BANKER: 1,
	TIE: 2,
	PLAYERPAIR: 3,
	BANKERPAIR: 4
}

class Index {
	private static _instance: Index;
	static get instance(): Index {
		if (!this._instance) {
			this._instance = new Index();
		}
		return this._instance;
	}
	public uiIndex: UI_Index;
	public gameInfo: any;
	//0-player 1-banker 2-tie 3-playerpair 4-bankerpair
	private betMountTotal: number[] = [0, 0, 0, 0, 0];
	public constructor() {
		this.openIndexView();
		GameMgr.instance.addEventListener(SocketEvent.PROTO_MSG, this.onRecieved, this);
	}

	onRecieved(ev: SocketEvent) {
		let { protoId, data } = ev;
		if (protoId === Protocol.channelNewGame) {
			Api.getNewGame(data.old, data.new, UserMgr.instance.getPlayer())
			EOS_VUE.$getNewGame()
		}
		else if (protoId === Protocol.channelNewPoker) {
			this.uiIndex.showChannelPoker(data)
		}
		else if (protoId === Protocol.channelNewOffer) {
			this.renderOfferItem(data)
		}
		else if (protoId === Protocol.channelNewPlayer) {
			let onlineNumber = data.total
			DEBUG && console.log('onlineNumber', onlineNumber);
			this.renderOnlineNumber(onlineNumber);
		}
	}

	openIndexView(): void {
		this.uiIndex = UIController.instance.createView<UI_Index>(UI_Index);
	}

	renderGameInfo(gameInfo): void {
		this.uiIndex.renderGameInfo(gameInfo);
	}

	renderOnlineNumber(onlineNumber: number) {
		this.uiIndex.setOnlineNumber(onlineNumber);
	}

	renderLotteryInfo(lotteryInfo, _newGame) {
		this.uiIndex.renderLotteryInfo(lotteryInfo, _newGame);
	}

	renderOfferItem(data) {
		if (Bet.instance.isBetSucc && data.player === (UserMgr.instance.account && UserMgr.instance.account.name)) {
			DEBUG && console.log('自己下的注，无视');
			return;
		}

		DEBUG && console.log('channelNewOffer:', data);
		/**
		 * required uint32 type = 1; // 类型 //0:闲,1:庄,2:和,3:闲对,4:庄对
		 * repeated pbKeyVal counter = 3; // 筹码 : 总数  筹码单位：分 0.1 = 10， 1= 100 
		 * polyfill 统一数据结构
		 */
		let pbBetList: any[] = null
		let types = data.offertype.split('|')
		let amounts = data.offeramount.split('|')
		pbBetList = types.map((trade, index) => {
			let amountLevel = [20, 10, 5, 1, 0.5, 0.1]
			let amount = NP.divide(amounts[index] * 1, 1e4)
			let counter = []
			let amountLevelCouter = amountLevel.map(number => {
				let val = Math.floor(amount / number)
				if (amount >= number) {
					amount = amount % number
				}
				return val
			})
			amountLevelCouter.forEach((number, index) => {
				if (number > 0) {
					counter.push({
						key: amountLevel[index],
						val: number
					})
				}
			})
			return {
				type: trade * 1,
				counter
			}

		})

		this.uiIndex.showOtherBet(pbBetList, data.player === (UserMgr.instance.account && UserMgr.instance.account.name));
	}
	refreshLotteryResult() {
		this.uiIndex.renderLotteryResult(this.betMountTotal)
	}
	addChip(type: number, value: number) {
		this.betMountTotal[type] = NP.plus(this.betMountTotal[type], value)
		this.uiIndex.renderLotteryResult(this.betMountTotal)
	}
	removeChip(type: number, value: number) {
		this.betMountTotal[type] = NP.minus(this.betMountTotal[type], value)
		this.uiIndex.renderLotteryResult(this.betMountTotal)
	}
	clearChip() {
		this.betMountTotal = [0, 0, 0, 0, 0]
		this.uiIndex.renderLotteryResult(this.betMountTotal)
	}

	allowBet(allow: boolean): void {
		this.uiIndex.setBetGroupEnable(allow);
	}

	setMusicToggle(selected: boolean): void {
		this.uiIndex && this.uiIndex.setMusicToggle(selected);
	}

	release(): void {

	}
}