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


namespace ActionGame.GameArtifacts.Models.Items
{
    public class ItemSprite : IMoveableObject
    {
        public WorldPosition WorldPosition { get; set; }
        public MovementPhysics MovementPhysics { get; set; }

        private IGameContentManager gameContentManager;
        private IRawContentManager rawContentManager;
        private GameData gameData;

        private SpriteFont font;
        private readonly Int32 size = 32;
        private Item item;
        private Int32? quantity;
        public Boolean IsExpired { get; set; }


        public ItemSprite(IGameContentManager gameContentManager, IRawContentManager rawContentManager, GameData gameData, WorldPosition worldPosition, Int32 itemKey, Int32? quantity)
        {
            this.gameContentManager = gameContentManager;
            this.rawContentManager = rawContentManager;
            this.gameData = gameData;

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

            this.item = new Item(gameContentManager.ItemDefinitionRepository.GetContent(itemKey), rawContentManager);
            this.quantity = quantity;

            this.font = rawContentManager.FontRepository.GetContent(@"fonts\Small");
            this.IsExpired = false;
        }


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

            spriteBatch.Draw(this.item.Texture, rect, Color.White);

            if (quantity != null)
            {
                Int32 dx = 16;
                Int32 dy = -8;
                Vector2 vector = new Vector2(this.WorldPosition.PositionX + dx, this.WorldPosition.PositionY + dy);

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


        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(this.IsExpired == false)
            {
                if(GetCurrentCollisionArea().Intersects(gameData.GameState.Player.GetCurrentCollisionArea()))
                {
                    if (this.item.ItemType != ItemDefinition.ItemType.Instant)
                    {
                        //give player item
                        Boolean result = gameData.GameState.Player.Inventory.AddItem(this.item, this.quantity);

                        if (result) this.IsExpired = true;
                    }
                    else
                    {
                        //immediately execute item
                        InventoryItemUser user = new InventoryItemUser(rawContentManager, gameData, gameData.GameState.Player);

                        if (this.quantity == null)
                        {
                            user.UseItem(this.item);
                        }
                        else
                        {
                            for(Int32 i = 0; i < this.quantity; i++)
                            {
                                user.UseItem(this.item);
                            }
                        }

                        //play pick up sound
                        rawContentManager.SoundEffectRepository.GetContent(@"soundeffects\item-pickup").Play();

                        this.IsExpired = true;
                    }
                }
            }
        }

    }
}
