import Phaser from "phaser";
import Graphics from "../../assets/Graphics";
import Item from "../../entities/Item";

const hp = 10;
const damage = 5;
const speed = 50;
const attackSpeed = 500;
const attackDuration = 165;
const attackCooldown = attackDuration * 2;

export default class slime {
  public sprite: Phaser.Physics.Arcade.Sprite;

  private attackUntil: number;
  private attackLockedUntil: number;
  private body: Phaser.Physics.Arcade.Body;

  constructor(x: number, y: number, scene: Phaser.Scene, player: any) {
	this.scene = scene;
	this.alive = true;
	this.hp = hp;
	this.canAttack = false;
	this.slimeId = window.slimeId++;
	var me = this;
	function hitSlime () {
		me.canAttack = true;
	}
    this.line = new Phaser.Geom.Line();
	this.player = player;
    this.sprite = scene.physics.add.sprite(x, y, Graphics.slime.name, 0);
    this.sprite.setSize(16, 8);
	this.sprite.setOffset(8, 16);
    this.sprite.anims.play(Graphics.slime.animations.idle.name);

    this.attackUntil = 0;
    this.attackLockedUntil = 0;

    this.body = <Phaser.Physics.Arcade.Body>this.sprite.body;
	//this.body.immovable = true;
	scene.physics.add.collider(player.sprite, this.sprite, hitSlime, null, this);
	if (window.julina) scene.physics.add.collider(window.julina.sprite, this.sprite);
	if (window.professor) scene.physics.add.collider(window.professor.sprite, this.sprite);
  }

  update(fov: any, map: any, time: number) {
	if (this.hp <= 0) {
		if (this.alive) {
			this.alive = false;
			this.sprite.anims.play('slimeDeath', true);
			this.body.setVelocity(0);
			this.body.immovable = true;
			this.sprite.depth = 2;
			this.body.checkCollision.none = true;
			
			var blood = new Item(
				this.sprite.x,
				this.sprite.y,
				this.scene,
				this.player,
				'blood',
				'Blood'
			);
			window.blood = blood;
		}
		return;
	}
	if (time < this.attackUntil) {
      return;
    }
	var canAttack = this.canAttack;
	this.canAttack = false;
	
	this.line.setTo(
		this.player.sprite.x, this.player.sprite.y+10, 
		this.sprite.x, this.sprite.y+4
	);
	var tiles = map.wallLayer.getTilesWithinShape(this.line);
	var canSee = true;
	if (Phaser.Geom.Line.Length(this.line) > 150) {
		canSee = false;
	} else {
		for (var i = 0; i < tiles.length; ++i) {
			if (map.tiles[tiles[i].y][tiles[i].x].collides) {
				canSee = false;
				break;
			}
		}
	}
	
	this.body.setVelocity(0);
	var player = this.player;
	if (canSee && player.alive) {
		if (player.sprite.x > this.sprite.x && !this.body.blocked.right) {
			this.body.setVelocityX(speed);
		} else if (player.sprite.x < this.sprite.x && !this.body.blocked.left) {
			this.body.setVelocityX(-speed);
		} 
		if (player.sprite.y > this.sprite.y && !this.body.blocked.down) {
			this.body.setVelocityY(speed);
		} else if (player.sprite.y < this.sprite.y && !this.body.blocked.up) {
			this.body.setVelocityY(-speed);
		} 
		this.body.velocity.normalize().scale(speed);
	}
	
	this.sprite.depth = this.sprite.y + 0.01 * this.slimeId;
	var player = this.player;
	
	if (player.alive && canAttack && time > this.attackLockedUntil) {
		window.hit.play();
		this.attackUntil = time + attackDuration;
		this.attackLockedUntil = time + attackDuration + attackCooldown;
		this.sprite.anims.play('slimeHit', true);
		
		this.body.setVelocity(0);
		player.sprite.setTint(0xff0000);
		player.hp = player.hp - damage;
		clearTimeout(player.timeoutRed);
		player.timeoutRed = setTimeout(function() {
			player.sprite.setTint(0xffffff);
		}, 100);
	} else {
		this.sprite.anims.play('slimeIdle', true);
	}
  }
}
