using ActionGame.GameArtifacts.Models.Characters;
using ActionGame.GameArtifacts.Models.Game;
using ActionGame.GameArtifacts.Operators.Characters;
using ActionGame.GameResources.Models;
using ActionGame.RawResources.Manager;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;


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

        private IRawContentManager rawContentManager;
        private IList<Texture2D> textures;
        private Int32 frame;
        private TimeSpan lastFrameUpdateTime;
        private readonly Int32 size = 32;



        public ImpactSprite(IRawContentManager rawContentManager, WorldPosition worldPosition)
        {
            IsExpired = false;
            this.rawContentManager = rawContentManager;
            this.WorldPosition = worldPosition.Copy();
            this.MovementPhysics = new MovementPhysics();

            this.textures = new List<Texture2D>();
            this.textures.Add(rawContentManager.TextureRepository.GetContent(@"textures\items\impact-1"));
            this.textures.Add(rawContentManager.TextureRepository.GetContent(@"textures\items\impact-2"));
            this.textures.Add(rawContentManager.TextureRepository.GetContent(@"textures\items\impact-3"));
            this.textures.Add(rawContentManager.TextureRepository.GetContent(@"textures\items\impact-4"));
            this.textures.Add(rawContentManager.TextureRepository.GetContent(@"textures\items\impact-5"));
            this.textures.Add(rawContentManager.TextureRepository.GetContent(@"textures\items\impact-6"));

            frame = 0;
        }


        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 - size, size, size);

            return rect;
        }


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


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

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

                frame++;
            }

            //expire
            if (frame >= this.textures.Count)
            {
                IsExpired = true;
            }
        }


        public void Draw(SpriteBatch spriteBatch)
        {
            Rectangle rect = this.GetDrawArea();

            spriteBatch.Draw(this.textures[frame], rect, Color.White);
        }

    }
}
