class UI_FairAlert extends UIBaseComponent {
	private openText: eui.Label;
	private content: eui.Group;
	private tx: egret.TextField;
	private poker: eui.Image;
	public constructor() {
		super();
	}

	/**
	 * 绑定事件
	 */
	protected bindEvents(): void {
		super.bindEvents();
	}

	/**
	 * 创建完成时调用
	 */
	protected onCreate(): void {
		super.onCreate();
		this.x = (GameMgr.instance.main.stage.stageWidth - this.width) / 2;
		this.y = (GameMgr.instance.main.stage.stageHeight - this.height) / 3;
		this.render();
		this.onLangChange();
	}

	/**
	 * 渲染UI时调用
	 */
	protected render(): void {
	}

	/**
	 * 改变语言时调用
	 */
	protected onLangChange(ev?: egret.Event): void {
		let {Alert} = Lang.instance.lang();
		this.openText.text = Alert.openText
	}

	public showAlert(block, imgUrl: string): void {
		this.visible = true;
		this.poker.source = imgUrl;

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

		let blockId: string = block.blockId
		let poker: number = block.poker
		//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 ? 20 : 26;
		let length = resultSeatStr.length;
		let endStr = resultSeatStr.slice(length - targetLength);
		let targetStr = `<font size=36>...${endStr}</font>`

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

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

	public hideAlert(): void {
		this.visible = false;
	}

	/**
	 * 界面关闭时调用
	 */
	public onClose(): void {
	}
}