var Light = cc.Class({
	extends: cc.Component,

	properties: {
		photo2: {
			default: [],
			type: cc.SpriteFrame,
			displayName: "图片资源"
		}
	},

	ctor: function () {
		Light.inst = this;
		g.Light = Light;
	},

	addLightNum(Num) {
		let length = Num;
		const starLayout = cc.find('Canvas/bg/connent/nodCenter/bg_trafficlight');
		starLayout.removeAllChildren();

		this.refreshLayoutHeight(starLayout);
		const baseY = 370;
		//最大长度
		this.MaxLength = length

		for (let i = 0; i < length; i++) {
			const starBase = cc.instantiate(cc.find('itemLigth'));
			starBase.getChildByName("sprite").getComponent(cc.Sprite).spriteFrame = this.photo2[i % 3];
			starBase.name = `itemLigth_${i}`;
			starBase.parent = starLayout;
			
			starBase.x = 0;
			starBase.y = baseY - i * 88;
		}
		this.currentStarIdx = 0;

		if (length <= 3) {
			for (let j = 0; j < 3 - length; j++) {
				var starBase2 = cc.instantiate(cc.find('itemLigth2'));
				starBase2.parent = starLayout;
			}
		}
	},

	refreshLayoutHeight(starLayout) {
		const canvas = cc.find("Canvas");
		starLayout.height = canvas.height;

		// const targetH = canvas.height / 2 - starLayout.y;
		// console.log("targetH: ", targetH);
		// console.log("starLayout.height: ", starLayout.height);

		// const y = targetH - (starLayout.y + starLayout.height);
		// console.log("y is ", y);
		// starLayout.y = y;
	},

	showLight() {
		if (this.currentStarIdx >= this.MaxLength) {
			return -1
		}
		if (!this.currentStarIdx) {
			this.currentStarIdx = 0;
		}
		const starBase = cc.find(`Canvas/bg/connent/nodCenter/bg_trafficlight/itemLigth_${this.currentStarIdx}`);
		starBase.getChildByName("sprite").active = true;
		this.currentStarIdx++;
		if (this.currentStarIdx >= this.MaxLength) {
			return -1
		}
	}
});
