class UI_Rate extends UIBaseComponent {
	private bgOpen: eui.Image;
	private bgClose: eui.Image;
	private rateTitle: eui.Label;
	private typeGroup: eui.DataGroup;
	private rateScroller: eui.Scroller;
	private rateList: eui.DataGroup;
	private btnClose: eui.Button;
	private btnOpen: eui.Button;
	private sbtnClose: eui.Button;
	private scrolling: boolean = false;
	private isOpen: boolean = true;//视图状态-默认打开

	public constructor() {
		super();
	}

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

		mouse.setButtonMode(this.btnClose, true);
		mouse.setButtonMode(this.btnOpen, true);

		this.btnClose.addEventListener(egret.TouchEvent.TOUCH_TAP, this.changeViewStatus, this);
		this.btnOpen.addEventListener(egret.TouchEvent.TOUCH_TAP, this.changeViewStatus, this);
		this.sbtnClose.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onSbtnClose, this);
		this.rateScroller.addEventListener(eui.UIEvent.CHANGE_START, this.onScroll, this)
		this.rateScroller.addEventListener(eui.UIEvent.CHANGE_END, this.onScrollEnd, this)
	}

	/**
	 * 创建完成时调用
	 */
	protected onCreate(): void {
		super.onCreate();
		this.render();
		this.onLangChange();
	}
	private onScroll(ev: egret.Event): void {
		this.scrolling = true
	}
	private onScrollEnd(ev: egret.Event): void {
		this.scrolling = false
	}

	//切换视图状态
	private changeViewStatus() {
		AudiosMgr.instance.playButtonClick();

		this.isOpen = !this.isOpen;

		this.bgOpen.visible = this.isOpen;
		this.rateScroller.visible = this.isOpen;
		this.btnClose.visible = this.isOpen;

		this.bgClose.visible = !this.isOpen;
		this.btnOpen.visible = !this.isOpen;
	}

	private onSbtnClose() {
		AudiosMgr.instance.playButtonClick();

		Rate.instance.closeRateView();
	}


	public renderHistory(): void {
		DEBUG && console.log("Rate.instance.rateHistory", Rate.instance.rateHistory)

		if (!Rate.instance.rateHistory) {
			(this.typeGroup.dataProvider as eui.ArrayCollection).source = [0, 0, 0, 0, 0];
			return;
		}

		let {type, result} = Rate.instance.rateHistory;
		(this.typeGroup.dataProvider as eui.ArrayCollection).source = type;
		(this.rateList.dataProvider as eui.ArrayCollection).source = result;

		this.validateNow()

		if (this.scrolling === false) {
			this.rateScroller.viewport.scrollH = this.rateScroller.viewport.contentWidth - this.rateScroller.width;
		}
	}

	/**
	 * 渲染UI时调用
	 */
	protected render(): void {
		this.typeGroup.itemRenderer = UI_RateTypeItem;
		this.typeGroup.dataProvider = new eui.ArrayCollection();
		(this.rateScroller.horizontalScrollBar.thumb as eui.Image).source = "pc_json.pc_board10";
		this.rateList.itemRenderer = UI_RateItem;
		this.rateList.dataProvider = new eui.ArrayCollection();
	}


	/**
	 * 改变语言时调用
	 */
	protected onLangChange(ev?: egret.Event): void {
		let {pcTitle, h5Title} = Lang.instance.lang().Rate;

		this.rateTitle.text = GAME_ISMOBILE ? h5Title : pcTitle;

		this.renderHistory();
	}

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