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

namespace ActionGame.GameArtifacts.Factories
{
    public class GameDataFactory
    {
        private IGameContentManager gameContentManager;
        private IRawContentManager rawContentManager;


        public GameDataFactory(IGameContentManager gameContentManager, IRawContentManager rawContentManager)
        {
            this.gameContentManager = gameContentManager;
            this.rawContentManager = rawContentManager;
        }


        public GameData CreateNewGame(Boolean noLoadAfter = true)
        {
            GameData gameData = new GameData()
            {
                WorldMaps = gameContentManager.WorldMapRepository,
                GameContentManager = gameContentManager,
                RawContentManager = rawContentManager,
                GameState = new GameState()
            };

            PlayerDefinition playerDefinition = this.gameContentManager.PlayerDefinition.GetContent(0);
            gameData.GameState.Player = new Player(gameData)
            {
                CharacterSet = gameContentManager.CharacterSetRepository.GetContent(0).Copy(),
                WorldPosition = new WorldPosition()
                {
                    WorldMapKey = playerDefinition.StartPosition.WorldMapKey,
                    WorldCellX = playerDefinition.StartPosition.WorldCellX,
                    WorldCellY = playerDefinition.StartPosition.WorldCellY,
                    PositionX = playerDefinition.StartPosition.PositionX * TileSet.DRAW_WIDTH,
                    PositionY = (playerDefinition.StartPosition.PositionY * TileSet.DRAW_HEIGHT) + TileSet.DRAW_HEIGHT
                },
                MovementPhysics = new MovementPhysics(),
                CharacterStats = new CharacterStats()
                {
                    Health = playerDefinition.Health,
                    MaximumHealth = playerDefinition.Health,
                    Attack = playerDefinition.Attack,
                    Defence = playerDefinition.Defence
                }
            };

            //temp
            Item item = null;
            item = new Item(gameContentManager.ItemDefinitionRepository.GetContent(2), rawContentManager);
            gameData.GameState.Player.Inventory.AddItem(item, null);

            if (noLoadAfter)
            {
                WorldMapCellActivator cellActivator = new WorldMapCellActivator(gameData);
                cellActivator.ActivateWorldMapCell(gameData.GetWorldMapCell(gameData.GameState.Player.WorldPosition), gameData.GameState.Player.WorldPosition);
            }

            return gameData;
        }

    }
}
