using ActionGame.GameArtifacts.Models.Characters;
using ActionGame.GameArtifacts.Models.Game;
using ActionGame.GameArtifacts.Models.Maps;
using ActionGame.GameResources.Models;
using System;


namespace ActionGame.GameArtifacts.Operators
{
    public class WorldMapCellActivator
    {
        private GameData gameData;


        public WorldMapCellActivator(GameData gameData)
        {
            this.gameData = gameData;
        }


        public void ActivateWorldMapCell(WorldMapCell worldMapCell, WorldPosition cellPosition)
        {
            GameState state = gameData.GameState;

            //activate NPC's
            ActivateNonPlayerCharacters(worldMapCell, cellPosition);

            //activate Door's
            state.ActiveDoors.Clear();
            foreach(DoorDefinition def in worldMapCell.DoorDefinitions)
            {
                Door door = new Door(def, gameData.GameContentManager);
                state.ActiveDoors.Add(door);
            }

            //activate enemies
            state.ActiveEnemies.Clear();
            foreach(EnemyDefinitionPlacement definitionPlacement in worldMapCell.EnemyDefinitionPlacements)
            {
                EnemyDefinition definition = gameData.GameContentManager.EnemyDefinitionRepository.GetContent(definitionPlacement.Key);

                Enemy enemy = new Enemy(definition, gameData.GameContentManager, cellPosition, gameData);
                enemy.WorldPosition.PositionX = TileSet.DRAW_WIDTH * definitionPlacement.TileX;
                enemy.WorldPosition.PositionY = (TileSet.DRAW_HEIGHT * definitionPlacement.TileY) + TileSet.DRAW_HEIGHT;

                state.ActiveEnemies.Add(enemy);
            }

            //clear active weapon sprites
            state.ActiveWeaponSprites.Clear();
            state.ActiveDisplaySprites.Clear();
            state.ActiveItemSprites.Clear();
        }


        public void ActivateNonPlayerCharacters(WorldMapCell worldMapCell, WorldPosition cellPosition)
        {
            GameState state = gameData.GameState;

            state.ActiveNonPlayerCharacters.Clear();
            foreach (NonPlayerCharacterDefinition def in worldMapCell.NonPlayerCharacterDefinitions)
            {
                Boolean activate = false;

                if (def.ShowIfVariableTrue.Equals(""))
                {
                    activate = true;
                }
                else if (this.gameData.GameState.VariableState.GetVariable(def.ShowIfVariableTrue) == true)
                {
                    activate = true;
                }


                if (activate)
                {
                    NonPlayerCharacter npc = new NonPlayerCharacter(def, gameData.GameContentManager, cellPosition, gameData);

                    state.ActiveNonPlayerCharacters.Add(npc);
                }
            }
        }


        public void ActivateNonPlayerCharactersForVariableSetTrue(String variable, WorldMapCell worldMapCell, WorldPosition cellPosition)
        {
            GameState state = gameData.GameState;

            foreach (NonPlayerCharacterDefinition def in worldMapCell.NonPlayerCharacterDefinitions)
            {
                if(def.ShowIfVariableTrue.Equals(variable))
                {
                    NonPlayerCharacter npc = new NonPlayerCharacter(def, gameData.GameContentManager, cellPosition, gameData);

                    state.ActiveNonPlayerCharacters.Add(npc);
                }
            }
        }


    }
}
