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


namespace ActionGame.GameArtifacts.Models.Game
{
    public class GameData
    {
        public IGameContentRepository<WorldMap> WorldMaps { get; set; }
        public GameState GameState { get; set; }     
        public IGameContentManager GameContentManager { get; set; }
        public IRawContentManager RawContentManager { get; set; }


        public void Update(GameTime gameTime)
        {
            GameState.Player.Update(gameTime);

            if (GameState.IsPlotActive() == false)
            {
                //NPC's - delete if needed
                for (int i = 0; i < GameState.ActiveNonPlayerCharacters.Count; i++)
                {
                    NonPlayerCharacter npc = GameState.ActiveNonPlayerCharacters[i];

                    if (npc.HideIfVariableTrue?.Equals("") == false)
                    {
                        if (GameState.VariableState.GetVariable(npc.HideIfVariableTrue))
                        {
                            GameState.ActiveNonPlayerCharacters.Remove(npc);
                            i--;
                        }
                    }
                }

                //Update NPC's
                foreach (NonPlayerCharacter npc in GameState.ActiveNonPlayerCharacters)
                {
                    npc.Update(gameTime);
                }

                //Update Enemies
                for(Int32 i = 0; i < GameState.ActiveEnemies.Count; i++)
                {
                    Enemy enemy = GameState.ActiveEnemies[i];

                    if(enemy.HideIfVariableTrue?.Equals("") == false)
                    {
                        if(GameState.VariableState.GetVariable(enemy.HideIfVariableTrue))
                        {
                            GameState.ActiveEnemies.Remove(enemy);
                            i--;
                        }
                    }

                    enemy.Update(gameTime);

                    if(enemy.CharacterStats.Health <= 0)
                    {
                        GameState.ActiveEnemies.Remove(enemy);
                        i--;

                        DeathBringer death = new DeathBringer(this.GameContentManager, this.RawContentManager, this);
                        death.Do(enemy);

                        if(enemy.VariableToSetTrueUponDeath?.Equals("") == false)
                        {
                            GameState.VariableState.SetVariable(enemy.VariableToSetTrueUponDeath, true);
                        }
                    }
                }

                //Update Weapon Sprites
                for (int i = 0; i < GameState.ActiveWeaponSprites.Count; i++)
                {
                    IWeaponSprite weaponSprite = GameState.ActiveWeaponSprites[i];

                    weaponSprite.Update(gameTime);

                    if (weaponSprite.IsExpired)
                    {
                        GameState.ActiveWeaponSprites.Remove(weaponSprite);
                        weaponSprite = null;
                        i--;
                    }
                }

                //Update Display Sprites
                for (int i = 0; i < GameState.ActiveDisplaySprites.Count; i++)
                {
                    IWeaponSprite weaponSprite = GameState.ActiveDisplaySprites[i];

                    weaponSprite.Update(gameTime);

                    if (weaponSprite.IsExpired)
                    {
                        GameState.ActiveDisplaySprites.Remove(weaponSprite);
                        weaponSprite = null;
                        i--;
                    }
                }

                for(int i = 0; i < GameState.ActiveItemSprites.Count; i++)
                {
                    ItemSprite itemSprite = GameState.ActiveItemSprites[i];

                    itemSprite.Update(gameTime);

                    if(itemSprite.IsExpired)
                    {
                        GameState.ActiveItemSprites.Remove(itemSprite);
                        itemSprite = null;
                        i--;
                    }
                }

                //Update Tiles on Active Map Cell
                WorldMapCell worldMapCell = GetWorldMapCell(this.GameState.Player.WorldPosition);
                worldMapCell.UpdateTiles(gameTime);
            }
        }


        public WorldMapCell GetWorldMapCell(WorldPosition position)
        {
            WorldMapCell cell = null;

            if (DoesWorldMapCellExist(position))
            {
                cell = WorldMaps.GetContent(position.WorldMapKey).Cells[position.WorldCellX][position.WorldCellY];
            }

            return cell;
        }


        public Boolean DoesWorldMapCellExist(WorldPosition position)
        {
            if(WorldMaps.GetContent(position.WorldMapKey) != null)
            {
                if(WorldMaps.GetContent(position.WorldMapKey).Cells.ContainsKey(position.WorldCellX))
                {
                    if (WorldMaps.GetContent(position.WorldMapKey).Cells[position.WorldCellX].ContainsKey(position.WorldCellY))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

    }
}
