import { Sprite, Vec3, _decorator } from 'cc';
import { AudioID } from '../../../framework/config/AudioConf';
import { ViewID } from '../../../framework/config/LayerConf';
import { Framework } from '../../../framework/Framework';
import { BaseView } from '../../../framework/layer/BaseView';

import { UserData } from '../../data/UserData';
import { LoginMgr } from '../../common/LoginManager';
const { ccclass, property } = _decorator;

@ccclass('MainUI')
export class MainUI extends BaseView {
	private GameUI = {}
	private openGameUI = 'Unpacking';
	private _timer_uuid = 0;
	onLoad() {
		super.onLoad();
	}


	// 所有监听事件
	EventAll() {

	}

	onDestroy() {
		Framework.event.removeEvent(this);
		super.onDestroy();
	}

	onOpen() {
		// this.mainRedDot();
		Framework.layer.open(ViewID.TowerUI, null, (the) => {
			this.GameUI[this.openGameUI] = the
		});
	}

		onMiningRedDotChanged(redNum: number) {
		this.Node.Mining_red.active = redNum > 0;
	}

	onArena() {
		Framework.audio.playEffect(AudioID.Click);
		LoginMgr.sendPost('arena', 'get', (data) => {
			if (data) {
				//Framework.layer.open(ViewID.Arena, null, data);
			}
		})
	}
	onOpenWin(event: Event, openUI) {
		Framework.audio.playEffect(AudioID.Click);
		let target: any = event.target;
		if (openUI) {
			if (openUI == 'Legion') {
				if (UserData['legion'].id != 0) {
					//Framework.layer.open(ViewID.Legion);
				} else {
					//Framework.layer.open(ViewID.NoLegion);
				}
			} else {
				let open_url = Number(ViewID[openUI])
				Framework.layer.open(open_url);
			}

		}
	}
	onClose() {

	}

	onShow() {
		this._ui_opacity.opacity = 255;
		this.node.setPosition(0, 0);
	}

	onHide() {
		this._ui_opacity.opacity = 0;
		this.node.setPosition(0, -5000);
	}
	private onTouchButton(event: Event, openUI) {
		Framework.audio.playEffect(AudioID.Click);
		let target: any = event.target;
		// UserData.getNodeFullPath(target)
		if (openUI) {


			let open_url = Number(ViewID[openUI])
			Framework.layer.open(open_url, () => {
				if (openUI == 'Territory') {
					if (!UserData['RoomId']) {
						UserData['RoomId'] = UserData['_id']
					}
				}
			});
		}
	}
	private BottonBtn(e, str) {
		Framework.audio.playEffect(AudioID.Click);
		let target = e.target
		// UserData.getNodeFullPath(target)
		if (this.openGameUI != target.name) {
			if (str == 'Mining') {
				// let openState = GameUtil.checkSystemOpen(Number(ViewID.Mining));
				// if (!openState.state) {
				// 	Framework.tips.setTips(openState.tips);
				// 	return;
				// }
			}
			let openGameUI = this.openGameUI
			let onBgFrame = target.parent.getComponent(Sprite).spriteFrame
			let unBgFrame = this.Node[this.openGameUI].parent.getComponent(Sprite).spriteFrame
			this.Node[this.openGameUI].children[0].scale = new Vec3(0.75, 0.75)
			target.parent.getComponent(Sprite).spriteFrame = unBgFrame
			this.Node[this.openGameUI].parent.getComponent(Sprite).spriteFrame = onBgFrame
			target.children[0].scale = new Vec3(1, 1)
			if (this.GameUI[this.openGameUI]) {
				this.GameUI[this.openGameUI].onHide()
			}
			this.openGameUI = str;

			if (!this.GameUI[str]) {
				Framework.layer.open(Number(ViewID[str]), () => {
					console.log(str);
					// let onBgFrame = this.Node[this.openGameUI].parent.getComponent(Sprite).spriteFrame;
					// let unBgFrame = this.Node[openGameUI].parent.getComponent(Sprite).spriteFrame;
					// console.log(this.openGameUI);
					// this.Node[this.openGameUI].children[0].scale = new Vec3(0.75, 0.75);
					// this.Node[this.openGameUI].parent.getComponent(Sprite).spriteFrame = unBgFrame;
					// this.Node[openGameUI].parent.getComponent(Sprite).spriteFrame = onBgFrame;
					// this.Node[openGameUI].children[0].scale = new Vec3(1, 1);
					// if (this.GameUI[openGameUI]) {
					// 	this.GameUI[openGameUI].onHide();
					// }
					// this.openGameUI = openGameUI;
				}, (the) => {
					// this.GameUI[str] = the
					// UserData['GameUIPage'] = GameConf.GamePageType[str];
				});
			} else {
				this.GameUI[str].onShow()
			}

		}

	}

	private _startTimer() {
		(this._timer_uuid != 0) && this._unschedule(this._timer_uuid);
		// this._timer_uuid = this._schedule(() => {
		// 	// LoginMgr.saveData();
		// }, GameConf.Macro.default_update_time);
	}

}