const HorseState = {
	stand1: 'stand1',
	stand2: 'stand2',
	run: 'run',
	jump: 'jump',
	down: 'down'
}
class UI_Horse extends UIBaseComponent {
	private horseArmatureDisplay: dragonBones.EgretArmatureDisplay;
	private horseArmature: dragonBones.Armature = null;  //骨架
	private horseState: string = HorseState.stand1;//马匹状态

	private snowArmatureDisplay: dragonBones.EgretArmatureDisplay;
	private snowArmature: dragonBones.Armature = null;  //骨架
	private snowStep: number = 1;//雪动画进度

	private rainArmatureDisplay: dragonBones.EgretArmatureDisplay;
	private rainArmature: dragonBones.Armature = null;  //骨架

	private buffArmatureDisplay: dragonBones.EgretArmatureDisplay;
	private buffArmature: dragonBones.Armature = null;  //骨架

	public constructor() {
		super();
	}

	protected bindEvents(): void {
		super.bindEvents();
	}

	protected onCreate(): void {
		super.onCreate();

		this.scaleX = 0.5;
		this.scaleY = 0.5;

		this.horseArmatureDisplay = Util.createDragonBones('horse');
		this.horseArmature = this.horseArmatureDisplay.armature;

		this.validateNow();
		this.width = this.horseArmatureDisplay.width;
		this.height = this.horseArmatureDisplay.height;

		this.snowArmatureDisplay = Util.createDragonBones('xueceng')
		this.snowArmature = this.snowArmatureDisplay.armature;
		this.snowArmatureDisplay.y = -110;
		this.snowArmatureDisplay.x = 20;
		this.snowArmatureDisplay.visible = false;

		this.rainArmatureDisplay = Util.createDragonBones('yudi')
		this.rainArmature = this.rainArmatureDisplay.armature;
		this.rainArmatureDisplay.y = -10;
		this.rainArmatureDisplay.x = 40;
		this.rainArmatureDisplay.visible = false;

		this.buffArmatureDisplay = Util.createDragonBones('buff')
		this.buffArmature = this.buffArmatureDisplay.armature;
		this.buffArmatureDisplay.visible = false;

		this.addChild(this.horseArmatureDisplay);
		this.addChild(this.snowArmatureDisplay);
		this.addChild(this.rainArmatureDisplay);
		this.addChild(this.buffArmatureDisplay);
	}

	public play(animation: string, times: number = 0): void {
		if (this.horseState === animation) return;
		this.horseState = animation;
		this.horseArmature.animation.play(animation, times);
	}

	public jump(): void {
		this.play(HorseState.jump, 1);

		let y = this.horseArmatureDisplay.y;

		egret.Tween.get(this.horseArmatureDisplay)
			.to({ y: y - 60 }, 300)
			.call(() => {
				this.down();
			})
	}

	public down() {
		this.play(HorseState.down, 1);

		let y = this.horseArmatureDisplay.y;

		egret.Tween.get(this.horseArmatureDisplay)
			.to({ y: y + 60 }, 300)
			.call(() => {
				this.play(HorseState.run)
			})
	}

	public startSnow(): void {
		this.snowStep = 1;
		this.snowArmatureDisplay.visible = true;
		this.snowArmatureDisplay.addEventListener(dragonBones.EventObject.LOOP_COMPLETE, this.runSnow, this);
		this.runSnow();
	}

	private runSnow(ev?: dragonBones.EgretEvent): void {
		this.snowArmature.animation.play(`xue${this.snowStep}`, 1);
		this.snowStep = this.snowStep % 3 + 1;
	}

	public stopSnow(): void {
		this.snowStep = 1;
		this.snowArmatureDisplay.visible = false;
		this.snowArmatureDisplay.removeEventListener(dragonBones.EventObject.LOOP_COMPLETE, this.runSnow, this);
	}

	public startRain(): void {
		this.rainArmatureDisplay.visible = true;
		this.rainArmature.animation.play('yudi', 0);
	}

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

	public startWind(): void {

	}

	public stopWind(): void {

	}

	public startSpeedUp(distance: number, duration: number = 600) {
		let x = this.x;

		this.buffArmature.animation.play('js', 0);

		egret.Tween.get(this)
			.to({ x: x + distance }, duration)
	}

	public startSlowDown(distance: number, duration: number = 600) {
		let x = this.x;

		this.buffArmature.animation.stop('js');

		egret.Tween.get(this)
			.to({ x: x - distance }, duration)
	}


	private onLoop(ev: dragonBones.EgretEvent): void {
		if (this.horseState === HorseState.jump) {
			this.play(HorseState.down);
		} else if (this.horseState === HorseState.down) {
			this.play(HorseState.run)
		}
	}

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