﻿using BomberBro.Engine.Players;
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BomberBro.Engine.Entities.Bombs
{
    public enum BOMB_TYPES
    {
        NORMAL,
        SPIKED,
        RUBBER,
        HEAT_SEEKING,
        MINE,
        POWER,
        REMOTE,
    }

    public enum BOMB_BLOW_SPEED
    {
        FASTER = 2,
        NORMAL = 4,
        SLOW = 8,
    }

	class IBomb : AnimatiableEntity
	{
		#region MEMBROS
		int _fireIntensity;
		bool _alive;
		IPlayer _owner;
		BOMB_TYPES _type;
		TimeSpan _timer;
		BOMB_BLOW_SPEED _secondsToBlow;
		#endregion

		#region CONSTRUTOR
		public IBomb(IPlayer owner, int intensity, Tile tile, Texture2D texture, Map map)
		{
			//Salvando os valores:
			_owner = owner;
			_fireIntensity = intensity;
			_alive = true;
			CurrentTile = tile;
			CurrentMap = map;

			//Salvando a animação:
			Sprite = texture;
			AddFrame(new Rectangle(Tile.TILE_SIZE_DEFAULT_W * 0, 0, Tile.TILE_SIZE_DEFAULT_W, Tile.TILE_SIZE_DEFAULT_H));
			AddFrame(new Rectangle(Tile.TILE_SIZE_DEFAULT_W * 1, 0, Tile.TILE_SIZE_DEFAULT_W, Tile.TILE_SIZE_DEFAULT_H));
			AddFrame(new Rectangle(Tile.TILE_SIZE_DEFAULT_W * 2, 0, Tile.TILE_SIZE_DEFAULT_W, Tile.TILE_SIZE_DEFAULT_H));

			//Velocidade da animação:
			switch (_secondsToBlow)
			{
			case BOMB_BLOW_SPEED.FASTER:
				TimeBetweenFrames = 500;
				break;
			case BOMB_BLOW_SPEED.NORMAL:
				TimeBetweenFrames = 100;
				break;
			case BOMB_BLOW_SPEED.SLOW:
				TimeBetweenFrames = 2000;
				break;
			}

			//Ligando a animação:
			Play = true;

			//Salvando as informações da bomba:
			MapPosition = CurrentTile.MapPosition;
			WorldPosition = CurrentTile.WorldPosition;
			CollisionBox = new Rectangle((int)WorldPosition.X, (int)WorldPosition.Y, Tile.TILE_SIZE_DEFAULT_W, Tile.TILE_SIZE_DEFAULT_H);

			//Zerando o timer:
			_timer = TimeSpan.Zero;

			CurrentMap.SetTopologicalMapPosition (MapPosition, MapTile.BOMB);
		}
		#endregion

		#region MANIPULAÇÃO
		public override void Update(GameTime time)
		{
			//Atualizando o timer:
			_timer += time.ElapsedGameTime;

			//É hora de explodir?
			if (_timer.TotalSeconds >= (int)_secondsToBlow && IsAlive())
			{
				//Estourando:
				BlowUp();
			}

			//Atualizando a animação:
			base.Update(time);
		}

		public virtual void BlowUp()
		{
			//Criando o FOGO no Fire Manager:
			Alive = false;
			FireManager.Instance.CreateFires(CurrentTile, _fireIntensity, false);

			//Removendo a bomba da lista do Player:

			//Tocando um som:

			CurrentMap.UnSetTopologicalMapPosition (MapPosition, MapTile.BOMB);

		}
		#endregion

		#region GETS E SETS
		public IPlayer Owner
		{
			get { return _owner; }
		}

		public bool Alive
		{
			set { _alive = value; }
		}

		public int FireIntensity
		{
			get { return _fireIntensity; }
		}

		public BOMB_TYPES Type
		{
			get { return _type; }
			set{ _type = value;}
		}

		public TimeSpan Timer
		{
			get { return _timer; }
			set { _timer = value; }
		}

		public BOMB_BLOW_SPEED SecondsToBlow
		{
			get { return _secondsToBlow; }
			set { _secondsToBlow = value; }
		}

		public virtual bool IsAlive()
		{
			return _alive;
		}

		public virtual Vector2 GetMapPosition()
		{
			return MapPosition;
		}

		public virtual void SetCurrentMap(Map map)
		{
			CurrentMap = map;
		}
		#endregion
	}

}
