using ActionGame.GameArtifacts.Models.Characters;
using ActionGame.GameArtifacts.Models.Game;
using ActionGame.GameArtifacts.Operators.Items;
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 Bomb : IWeaponSprite
    {
        public WorldPosition WorldPosition { get; set; }
        public MovementPhysics MovementPhysics { get; set; }
        public Boolean IsExpired { get; set; }

        private IRawContentManager rawContentManager;
        private GameData gameData;
        private IBattleObject caster;
        private Int32 attack;

        private IList<Texture2D> textures;
        private Int32 frame;
        private TimeSpan lastFrameUpdateTime;


        private TimeSpan creationTime;
        private readonly Int32 size = 32;



        public Bomb(IRawContentManager rawContentManager, GameData gameData, IBattleObject caster, WorldPosition worldPosition, Int32 attack)
        {
            IsExpired = false;
            this.rawContentManager = rawContentManager;
            this.gameData = gameData;

            this.caster = caster;
            this.WorldPosition = worldPosition.Copy();
            this.attack = attack;
            this.MovementPhysics = new MovementPhysics();

            this.textures = new List<Texture2D>();
            this.textures.Add(rawContentManager.TextureRepository.GetContent(@"textures\items\bomb-sprite-1"));
            this.textures.Add(rawContentManager.TextureRepository.GetContent(@"textures\items\bomb-sprite-2"));
            
            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 (creationTime == default(TimeSpan)) creationTime = gameTime.TotalGameTime;
            if (lastFrameUpdateTime == default(TimeSpan)) lastFrameUpdateTime = gameTime.TotalGameTime;

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

                frame++;
                if (frame >= this.textures.Count) frame = 0;
            }

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

                //spawn explosion sprite
                ExplosionMaker maker = new ExplosionMaker(rawContentManager, gameData, null);
                maker.Do(this.WorldPosition, attack, 160);
            }
        }


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

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

    }
}
