import Scene from "../views/Scene";
import MyImage from "./MyImage";
import PanelCtrl from "../ctrls/panelCtrl";
import { ModuleTypes } from "../types/sceneTypes";
import { NetManager } from "../../libs/tw/manager/NetManager";
import { DataManager } from "../../libs/tw/manager/DataManager";
import { GTime } from "../../libs/tc/util/GTime";
import { TimeFormat } from "../../libs/tc/enum/TimeFormat";
import Panel from "../views/Panel";
import { GDispatcher } from "../../libs/tc/util/GDispatcher";
import { Buried } from "../../libs/tw/util/Buried";
import ComponentBase from "../components/ComponentBase";
import GuidePanel from "../guidePanel/GuidePanel";
import { GCache } from "../../libs/tc/util/GCache";
import Loading from "../loading/Loading";
import { GetPrizeInfoData } from "../../libs/tw/data/plug/getPrizeInfo/GetPrizeInfoData";
import { ABNetManager } from "../../libs/tc/manager/ABNetManager";
import FriendItem from "./FriendItem";
import { NetName } from "../../libs/tw/enum/NetName";
import DogItem from "./DogItem";

export default class MainScene extends Scene {

	public topGroup: eui.Group;
	public lvText: eui.Label;
	public foodCountText: eui.Label;
	public teamCountText: eui.Label;
	public yieldRateText: eui.Label;
	public team_0: eui.Group;
	public team_1: eui.Group;
	public team_2: eui.Group;
	public team_3: eui.Group;
	public team_4: eui.Group;
	public team_5: eui.Group;
	public bottomGroup: eui.Group;
	public getDogText: eui.Label;
	public doglist_0: eui.Group;
	public doglist_1: eui.Group;
	public doglist_2: eui.Group;
	public doglist_3: eui.Group;
	public doglist_4: eui.Group;
	public doglist_5: eui.Group;
	public doglist_6: eui.Group;
	public doglist_7: eui.Group;
	public doglist_8: eui.Group;
	public doglist_9: eui.Group;
	public doglist_10: eui.Group;
	public doglist_11: eui.Group;




	public get skinKey() { return 'MainScene' }
	public get groupName() { return 'main_scene' }

	private teams = [];
	private doglists = [];
	constructor(data?) {
		super(data);
		if (data) {

		}
		this.percentHeight = 100;
	}

	initEvents() {
		GDispatcher.addEvent(ABNetManager.ERROR, this.onError, this);

		for (let i = 0; i < 6; i++) {
			this.teams[i] = new FriendItem()
			this.teams[i].x = 0;
			this.teams[i].y = 0;
			this["team_" + i].addChild(this.teams[i])
			/* this.teams[i].setLoadVal(50)
			this.teams[i].setState(true) */
		}

		for (let i = 0; i < 12; i++) {
			this.doglists[i] = new DogItem()
			this.doglists[i].x = -8;
			this.doglists[i].y = -60;
			this["doglist_" + i].addChild(this.doglists[i])
		}
	}

	private onError(net: any, code: any): void {
		console.log(code);
		var msg: string;
		var msgArr: any = window['errorMessage'];
		if (!isNaN(code)) {
			msg = msgArr[code] ? msgArr[code] : '额哦，一个未知的错误';
		} else {
			msg = code;
		}

		PanelCtrl.instance.show(ModuleTypes.MSG_PANEL, [msg], false);
	}
	removeEvents() {
		this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
		GDispatcher.removeEvent(ABNetManager.ERROR, this.onError, this);
	}

	protected onSkinComplete(): void {
		super.onSkinComplete();
		NetManager.ins.getHome((succuss) => {
			if (!succuss) {
				return;
			}
			this.updateIndex()
		}, "")
	}

	homeData
	teamData
	dogData

	updateIndex() {
		console.log(DataManager.ins.getData(NetName.GET_HOME))
		this.homeData = DataManager.ins.getData(NetName.GET_HOME).data;
		if (this.homeData) {
			this.teamData = this.homeData.team
			this.dogData = this.homeData.dogs
			this.updateTeam()
			this.updateDogList()
			this.updateLv()
		}
	}

	updateLv(){
		this.lvText.text=`LV.${this.homeData.maxLevel}`
	}

	updateTeam() {
		for (let i = 0; i < 6; i++) {
			if (this.teamData[i]) {
				this.teams[i].setLoadVal((24 * 60 - this.teamData[i].remainingTime) / (24 * 60) * 100)
				this.teams[i].setState(true)
				this.teams[i].setImgUrl(this.teamData[i].avatar)
			} else {
				this.teams[i].setState(false)
			}

		}
	}


	updateDogList() {
		for (let i = 0; i < 12; i++) {
			if (this.dogData[i]) {
				this.doglists[i].setLv(this.dogData[i].level)
			}
		}
	}



	start(data?) {

	}

	protected onLoad(): void {
		this.initUI();

		this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
	}

	private initUI(): void {
		//初始化数据
		this.initData();

	}
	private initData(): void {
	}


	private onEnterFrame(): void {

	}

}