class UI_RateDetailItem extends UIBaseItemRenderer {
	private tabTypeTitle: eui.Label;
	private tabBlockNumTitle: eui.Label;
	private tabBlockIdTitle: eui.Group;
	private tx: egret.TextField;

	public constructor() {
		super();
	}
	/**
	 * 创建完成时调用
	 */
	protected onCreate(): void {
		super.onCreate();
		mouse.setButtonMode(this.tabBlockIdTitle, true);
		mouse.setButtonMode(this.tabBlockNumTitle, true);
		this.tabBlockIdTitle.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBlockTap, this);
		this.tabBlockNumTitle.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBlockTap, this);
	}

	private onBlockTap(ev: egret.TouchEvent): void {
		let data = this.data;
		if (data) {
			window.open(`https://eospark.com/block/${data.blockNum}`);
		}
	}
	protected dataChanged(): void {
		let lang = Lang.instance.lang();
		let {player, banker} = lang.Bet;

		let data = this.data;
		if (data === undefined) {
			this.visible = false;
			return;
		}

		if (!this.tx) {
			this.tx = new egret.TextField;
			this.validateNow()
			this.tx.y = (this.tabBlockIdTitle.height - 24) / 2;
			this.tabBlockIdTitle.addChild(this.tx)
		}

		let {index, state, blockId, blockNum, poker} = data

		this.tabTypeTitle.text = `${state === 0 ? player : banker} ${index}`;
		this.tabBlockNumTitle.text = blockNum;

		//TODO parse blockId to textFlow
		let bigNum = ['a', 'b', 'c', 'd', 'e']
		let val: string = `${((poker - 1) % 13) + 1}`;
		val = parseInt(val) >= 10 ? bigNum[parseInt(val) - 10] : val
		let color = `${Math.floor(poker / 13)}`
		let len = blockId.length
		let step = 0
		let result = []
		while (len >= 0) {
			let char = blockId.charAt(len)

			if (/[0-9a-e]/.test(char) === false) {
				len--
				continue
			}

			if (step === 0 && char === val) {
				result.push(len)
				step = 1
			} else if (step === 1 && char === color) {
				result.push(len)
				step = 2
				len = -1
			}
			len--
		}

		let resultSeatStr = blockId;//占位文本
		let resultStr = result.map(pos => blockId.charAt(pos))//高亮文本
		let resultSeat = ['!', '@']//占位符

		result.forEach((pos, index) => {
			resultSeatStr = resultSeatStr.slice(0, pos) + resultSeat[index] + resultSeatStr.slice(pos + 1)
		})

		let targetLength = GAME_ISMOBILE ? 26 : 36;
		let length = resultSeatStr.length;
		let endStr = resultSeatStr.slice(length - targetLength);
		let targetStr = `<font size=24>...${endStr}</font>`

		resultSeat.forEach((seat, index) => {
			targetStr = targetStr.replace(seat, `</font><font color=0xffa531 size=24>${resultStr[index]}</font><font size=24>`)
		})

		this.tx.textFlow = (new egret.HtmlTextParser).parser(targetStr)
	}
}