using ActionGame.GameArtifacts.Models.Characters;
using ActionGame.GameArtifacts.Models.Game;
using ActionGame.GameArtifacts.Models.Items;
using ActionGame.GameResources.Manager;
using ActionGame.GameResources.Models;
using ActionGame.RawResources.Manager;
using System;


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

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


        public void GoToNextItem()
        {
            GameState gameState = gameData.GameState;
            IGameContentManager gameContentManager = gameData.GameContentManager;
            IRawContentManager rawContentManager = gameData.RawContentManager;


            if (gameState.ActivatedNonPlayerCharacter != null)
            {
                NonPlayerCharacter npc = gameState.ActivatedNonPlayerCharacter;

                Int32 index = npc.Plot.IndexOf(gameState.ActivatedPlotItem);
                Boolean keepRunning = true;

                while (keepRunning)
                {
                    index++;

                    if (index == npc.Plot.Count)
                    {
                        //end
                        gameState.ActivatedNonPlayerCharacter = null;
                        gameState.ActivatedPlotItem = null;
                        
                        return;
                    }
                    else
                    {
                        gameState.ActivatedPlotItem = npc.Plot[index];
                    }

                    if(gameState.ActivatedPlotItem.Type == PlotItem.Types.Text)
                    {
                        keepRunning = false;
                    }
                    else if (gameState.ActivatedPlotItem.Type == PlotItem.Types.VariableSet)
                    {
                        gameState.VariableState.SetVariable(gameState.ActivatedPlotItem.VariableName, (Boolean)gameState.ActivatedPlotItem.VariableValue);

                        if(gameState.ActivatedPlotItem.VariableValue == true)
                        {
                            WorldPosition position = gameData.GameState.Player.WorldPosition;
                            WorldMapCell cell = gameData.GetWorldMapCell(position);

                            WorldMapCellActivator cellActivator = new WorldMapCellActivator(gameData);
                            cellActivator.ActivateNonPlayerCharactersForVariableSetTrue(gameState.ActivatedPlotItem.VariableName, cell, position);
                        }   

                        keepRunning = true;
                    }
                    else if (gameState.ActivatedPlotItem.Type == PlotItem.Types.GiveItem)
                    {
                        ItemDefinition itemDefinition = gameContentManager.ItemDefinitionRepository.GetContent((Int32)gameState.ActivatedPlotItem.ItemKey);
                        Item item = new Item(itemDefinition, rawContentManager);
                        Int32? quantity = gameState.ActivatedPlotItem.Quantity;

                        gameState.Player.Inventory.AddItem(item, quantity);

                        keepRunning = true;
                    }
                    else if(gameState.ActivatedPlotItem.Type == PlotItem.Types.Heal)
                    {
                        gameState.Player.CharacterStats.Health = gameState.Player.CharacterStats.MaximumHealth;

                        keepRunning = true;
                    }
                    else if (gameState.ActivatedPlotItem.Type == PlotItem.Types.ChangeStats)
                    {
                        if (gameState.ActivatedPlotItem.Health != null)
                        {
                            gameState.Player.CharacterStats.MaximumHealth += (Int32)gameState.ActivatedPlotItem.Health;
                            gameState.Player.CharacterStats.Health += (Int32)gameState.ActivatedPlotItem.Health;
                        }

                        if (gameState.ActivatedPlotItem.Attack != null) gameState.Player.CharacterStats.Attack += (Int32)gameState.ActivatedPlotItem.Attack;
                        if (gameState.ActivatedPlotItem.Defence != null) gameState.Player.CharacterStats.Defence += (Int32)gameState.ActivatedPlotItem.Defence;

                        keepRunning = true;
                    }
                    else if (gameState.ActivatedPlotItem.Type == PlotItem.Types.FillItems)
                    {
                        gameState.Player.Inventory.FillAllItems();

                        keepRunning = true;
                    }

                }
            }
        }


    }
}
