using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class SceneChestDataLoader : SceneBaseDataLoader<ChestPrefabAsset>
{
    public SceneChestDataLoader(string _objectListName, string _objectFolderName)
    {
        SaveModelSystem = new SaveChestManager();
        SaveModelSystem.ObjectFolderName = _objectFolderName;
        SaveModelSystem.ObjectListName = _objectListName;
    }

    /*        
        public void LoadInventoryItemsList()
        {
            string path = SaveSystem.GetSavePath();

            if (!Directory.Exists(path))    // if not exists thats mean there was nothing saved yet - nothing to load
                return;

            FileInfo[] fileInfo = new DirectoryInfo(path).GetFiles();

            foreach(FileInfo file in fileInfo)
            {
                if(file.Name != this.ItemsListName + ".fun")
                    continue;

                Dictionary<int, EquippableItemPrefabAssetData> equippableItemsList = SaveInventorySystem.LoadInventoryItemsList(this.ItemsListName);

                // itemEntry.Value.MapDataToObject() - map data format to object 

                // save object
                foreach(KeyValuePair<int, EquippableItemPrefabAssetData> itemEntry  in equippableItemsList)
                {
                    InventoryManager.Instance.SetupItemInInventory(itemEntry.Key, itemEntry.Value.MapDataToEquippableItemPrefarbAsset().equippableItem);
                } 
            }
        }

        public void SaveInventoryItems()
        {
            // Case I - if we remember all list
            //      1) if after removed item form DynamicList is empty - remove all file
            //      2) if after removed item form DynamciList there are another one - save updated list again
            if(InventoryManager.Instance._items.Count > 0) {

                // 1. Map From Dictionary<int, Item>' to 'Dictionary<int, EquippableItemPrefabAsset>'
                Dictionary<int, EquippableItemPrefabAsset> mappedList = new Dictionary<int, EquippableItemPrefabAsset>();

                foreach(KeyValuePair<int, Item> itemElement in InventoryManager.Instance._items)
                {
                    mappedList[itemElement.Key] = new EquippableItemPrefabAsset(
                        itemElement.Value.Name,
                        itemElement.Value.ItemModel.name,
                        new Vector3(0,0,0),
                        (EquippableItem) itemElement.Value
                    );
                }

                // 2. Save Items
                SaveInventorySystem.SaveInventoryItemsList(mappedList, this.ItemsListName);
            } else {
                string _path = SaveSystem.GetSavePath() + "/" + this.ItemsListName + ".fun";

                try    
                {    
                    Debug.Log("File to remove: " + _path);

                    if(File.Exists(_path))
                    {
                        File.Delete(_path);
                    }    
                }    
                catch (IOException ioExp)    
                {    
                    Debug.LogError(ioExp.Message);    
                }
            }

            // Case II - if we rememenber object per file 
            //      1) remove specyfic file
            //
            // Unfortunatelly we don't use this way of saving items yet :D
        }
    */


    protected override List<ChestPrefabAsset> LoadDynamicData() 
    {
        SaveModelSystem.Path = PathBuilder.BuildSavePath().WithMap(CurrentMap).WithDataType(SceneElementTypeEnum.Dynamic).GetString();

        return SaveModelSystem.LoadModelList();
    }

    protected override bool SaveDynamicData(List<ChestPrefabAsset> _elements) 
    {
        SaveModelSystem.Path = PathBuilder.BuildSavePath().WithMap(CurrentMap).WithDataType(SceneElementTypeEnum.Dynamic).GetString();

        return base.SaveDynamicData(_elements);
    }

    protected override List<ChestPrefabAsset> LoadGenericData() { throw new NotImplementedException(); }

    protected override bool SaveGenericData(List<ChestPrefabAsset> _elements) { throw new NotImplementedException(); }
}