using ActionGame.GameArtifacts.Models.Game;
using ActionGame.GameArtifacts.Models.Items;
using Newtonsoft.Json;
using System;
using System.IO;
using System.IO.IsolatedStorage;


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

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


        public Boolean Load()
        {
            IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream isolatedFileStream = null;
            String filename = "actiongame.sav";

            try
            {
                isolatedFileStream = savegameStorage.OpenFile(filename, System.IO.FileMode.Open);
            }
            catch (IsolatedStorageException)
            {
                return false;
            }

            if (isolatedFileStream != null)
            {
                StreamReader reader = new StreamReader(isolatedFileStream);

                String json = reader.ReadToEnd();
                SaveContainer container = JsonConvert.DeserializeObject<SaveContainer>(json);

                gameData.GameState.Player.CharacterStats = container.CharacterStats;
                gameData.GameState.Player.WorldPosition = container.WorldPosition;
                gameData.GameState.VariableState = container.VariableState;
                LoadInventory(container.Inventory);

                WorldMapCellActivator cellActivator = new WorldMapCellActivator(gameData);
                cellActivator.ActivateWorldMapCell(gameData.GetWorldMapCell(gameData.GameState.Player.WorldPosition), gameData.GameState.Player.WorldPosition);

                reader.Close();
                isolatedFileStream.Close();
            }

            return true;
        }


        private void LoadInventory(SaveInventoryContainer saveInventory) 
        {
            Inventory inventory = gameData.GameState.Player.Inventory;

            //clear
            inventory.Slot1 = null;
            inventory.Slot2 = null;
            inventory.InventorySlots.Clear();

            //load
            if (saveInventory.Slot1 != null)
            {
                Item item = new Item(this.gameData.GameContentManager.ItemDefinitionRepository.GetContent(saveInventory.Slot1.ItemKey), this.gameData.RawContentManager);
                inventory.AddItem(item, saveInventory.Slot1.Quantity);
            }

            if (saveInventory.Slot2 != null)
            {
                Item item = new Item(this.gameData.GameContentManager.ItemDefinitionRepository.GetContent(saveInventory.Slot2.ItemKey), this.gameData.RawContentManager);
                inventory.AddItem(item, saveInventory.Slot2.Quantity);
            }

            foreach(SaveInventorySlotContainer slot in saveInventory.InventorySlots)
            {
                Item item = new Item(this.gameData.GameContentManager.ItemDefinitionRepository.GetContent(slot.ItemKey), this.gameData.RawContentManager);
                inventory.AddItem(item, slot.Quantity);
            }
        }


    }
}
