class Weather {
	private static _instance: Weather;
	static get instance(): Weather {
		if (!this._instance) {
			this._instance = new Weather();
		}
		return this._instance;
	}

	private contaniner: egret.DisplayObjectContainer;

	//雨，雪，风，云(1,2,3)
	private rainArmatureDisplay: dragonBones.EgretArmatureDisplay;
	private rainArmature: dragonBones.Armature = null;

	private snowArmatureDisplay: dragonBones.EgretArmatureDisplay;
	private snowArmature: dragonBones.Armature = null;

	private windArmatureDisplay: dragonBones.EgretArmatureDisplay;
	private windArmature: dragonBones.Armature = null;

	private cloudArmatureDisplays: dragonBones.EgretArmatureDisplay[] = [];
	private cloudArmatures: dragonBones.Armature[] = [];

	private cloud1ArmatureDisplay: dragonBones.EgretArmatureDisplay;
	private cloue1Armature: dragonBones.Armature = null;

	private cloud2ArmatureDisplay: dragonBones.EgretArmatureDisplay;
	private cloue2Armature: dragonBones.Armature = null;

	private cloud3ArmatureDisplay: dragonBones.EgretArmatureDisplay;
	private cloue3Armature: dragonBones.Armature = null;

	private typeMap = {
		rain: this.startRain,
		snow: this.startSnow,
		wind: this.startWind
	}

	public constructor() {
		GameMgr.instance.addEventListener(Channel.RUN_MSG, this.onReceived, this);
	}

	private onReceived(ev: egret.Event): void {
		let {data} = ev;

		if (data.weather) {
			let {weather, duration, fail} = data;

			let fn = this.typeMap[weather];

			fn.call(this, duration);
		}
	}

	public setWeatherContainer(contaniner: egret.DisplayObjectContainer): void {
		this.contaniner = contaniner;
	}

	public startRain(duration?: number): void {
		if (this.rainArmature == null) {
			this.rainArmatureDisplay = Util.createDragonBones('rain');
			this.rainArmature = this.rainArmatureDisplay.armature;
			this.contaniner.addChild(this.rainArmatureDisplay);
		}
		this.rainArmatureDisplay.visible = true;
		this.rainArmature.animation.play('rain', 0);

		Horse.instance.startRain();

		if (duration) {
			setTimeout(() => {
				this.stopRain();
			}, duration);
		}
	}

	public stopRain(): void {
		this.rainArmatureDisplay.visible = false;
		this.rainArmature.animation.stop('rain');

		Horse.instance.stopRain();
	}

	public startSnow(duration?: number): void {
		if (this.snowArmature == null) {
			this.snowArmatureDisplay = Util.createDragonBones('snow');
			this.snowArmature = this.snowArmatureDisplay.armature;
			this.contaniner.addChild(this.snowArmatureDisplay);
		}
		this.snowArmatureDisplay.visible = true;
		this.snowArmature.animation.play('snow', 0);

		if (duration) {
			setTimeout(() => {
				this.stopSnow();
			}, duration);
		}

		Horse.instance.startSnow();
	}

	public stopSnow(): void {
		this.snowArmatureDisplay.visible = false;
		this.snowArmature.animation.stop('snow');

		Horse.instance.stopSnow();
	}

	public startWind(duration?: number): void {
		if (this.windArmature == null) {
			this.windArmatureDisplay = Util.createDragonBones('wind');
			this.windArmature = this.windArmatureDisplay.armature;
			this.contaniner.addChild(this.windArmatureDisplay);
		}
		this.windArmatureDisplay.visible = true;
		this.windArmature.animation.play('wind', 0);

		if (duration) {
			setTimeout(() => {
				this.stopWind();
			}, duration);
		}

		Horse.instance.startWind();
	}

	public stopWind(): void {
		this.windArmatureDisplay.visible = false;
		this.windArmature.animation.stop('wind');

		Horse.instance.stopWind();
	}

	public startCloud(duration?: number): void {
		if (this.cloudArmatureDisplays.length === 0) {
			let displays = [0, 0, 0];
			displays.forEach((item, index) => {
				let cloudIndex = index + 1;
				let display = Util.createDragonBones(`yunceng${cloudIndex}`)
				let armature = display.armature;
				this[`cloud${cloudIndex}ArmatureDisplay`] = display;
				this[`cloue${cloudIndex}Armature`] = armature;
				this.cloudArmatureDisplays.push(display);
				this.cloudArmatures.push(armature);
				this.contaniner.addChild(display);
			})
		}

		if (duration) {
			setTimeout(() => {
				this.stopCloud();
			}, duration);
		}

		this.cloudArmatureDisplays.forEach((item, index) => {
			item.visible = true;
			this.cloudArmatures[index].animation.play(`yunceng${index + 1}`, 0)
		})
	}

	public stopCloud(): void {
		this.cloudArmatureDisplays.forEach((item, index) => {
			item.visible = false;
			this.cloudArmatures[index].animation.stop(`yunceng${index + 1}`)
		})
	}
}