using ActionGame.GameArtifacts.Models.Characters;
using ActionGame.GameResources.Models;
using ActionGame.RawResources.Manager;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;

namespace ActionGame.GameArtifacts.Models.WeaponSprites
{
    public class DamageSprite : IWeaponSprite
    {
        public WorldPosition WorldPosition { get; set; }
        public MovementPhysics MovementPhysics { get; set; }
        public Boolean IsExpired { get; set; }

        private IBattleObject target;
        private Int32 amount;
        private SpriteFont font;

        private Int32 frame;
        private TimeSpan lastFrameUpdateTime;
        private TimeSpan creationTime;


        public DamageSprite(IRawContentManager rawContentManager, IBattleObject target, Int32 amount, WorldPosition worldPosition)
        {
            this.IsExpired = false;
            this.target = target;
            this.amount = amount;
            this.font = rawContentManager.FontRepository.GetContent(@"fonts\Small");
            this.frame = 0;
            this.WorldPosition = worldPosition.Copy();
        }


        public void Draw(SpriteBatch spriteBatch)
        {
            Color fontColor = Color.White;

            if(target.GetType() == typeof(Player))
            {
                if (amount > 0) fontColor = Color.Red;
                else fontColor = Color.LightGreen;
            }
            else
            {
                if (amount > 0) fontColor = Color.White;
                else fontColor = Color.Blue;
            }

            Int32 dx = TileSet.DRAW_WIDTH / 4;
            Int32 dy = amount > 0 ? (frame * 3) : (frame * -3);
            String text = amount < 0 ? (amount * -1).ToString() : amount.ToString();
            Vector2 vector = new Vector2(target.WorldPosition.PositionX + dx, target.WorldPosition.PositionY + dy);

            spriteBatch.DrawString(this.font, text, vector + new Vector2(1,1), Color.Black, 0, Vector2.Zero, 1f, SpriteEffects.None, 1f);
            spriteBatch.DrawString(this.font, text, vector + new Vector2(-1,1), Color.Black, 0, Vector2.Zero, 1f, SpriteEffects.None, 1f);
            spriteBatch.DrawString(this.font, text, vector, fontColor, 0, Vector2.Zero, 1f, SpriteEffects.None, 0f);
        }


        public void Update(GameTime gameTime)
        {
            if (creationTime == default(TimeSpan)) creationTime = gameTime.TotalGameTime;
            if (lastFrameUpdateTime == default(TimeSpan)) lastFrameUpdateTime = gameTime.TotalGameTime;

            //frame update
            if ((gameTime.TotalGameTime - lastFrameUpdateTime).TotalSeconds > 0.125)
            {
                lastFrameUpdateTime = gameTime.TotalGameTime;

                frame++;
            }

            //expire
            if ((gameTime.TotalGameTime - creationTime).TotalSeconds > 0.5)
            {
                IsExpired = true;
            }
        }


        public Rectangle GetCurrentCollisionArea()
        {
            Rectangle rect = GetCollisionAreaAtPosition(this.WorldPosition.PositionX, this.WorldPosition.PositionY);

            return rect;
        }


        public Rectangle GetCollisionAreaAtPosition(int x, int y)
        {
            Rectangle rect = new Rectangle(x, y, 1, 1);

            return rect;
        }


        public Rectangle GetDrawArea()
        {
            return GetCurrentCollisionArea();
        }

    }
}
