using System;
using Intrepid.Integration.Persistence.DTOs;
using Intrepid.SaveSystem;
using Intrepid.Utilities;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Intrepid.Integration.Persistence
{
    [Serializable]
    [CreateAssetMenu(fileName = "Persistence Configuration", menuName = "Intrepid/Save System/Persistence Configuration")]
    public class ConfigurationPersistenceService : AbstractPersistenceService<ConfigurationSaveFileDTO>
    {
        private const string ConfigurationSaveFileName = "configuration";

        [GroupDebug, ReadOnly, ShowInInspector, PropertyOrder(1000), InlineProperty, HideLabel, PropertySpace, Title("Current Slot")]
        private ConfigurationSaveFileDTO configurationFile;

        public ConfigurationDTO Configuration => configurationFile?.configuration;

        public bool HasLastGame => Configuration.lastGameSlotType != GameDataSlotType.None;

        public void Load()
        {
            if (Exists(ConfigurationSaveFileName))
                LoadConfiguration();
            else
                CreateNewConfiguration();
        }

        [GroupDebug]
        [Button]
        public void CreateNewConfiguration()
        {
            configurationFile = new ConfigurationSaveFileDTO
            {
                fileId = ConfigurationSaveFileName,
                configuration = ConfigurationDTO.WithInitialValues()
            };
            SaveConfigurationQuietly();
        }

        [GroupDebug]
        [Button]
        public async void SaveConfigurationQuietly()
        {
            await Save(configurationFile);
        }

        [GroupDebug]
        [Button]
        public void LoadConfiguration()
        {
            configurationFile = Load(ConfigurationSaveFileName);
        }
    }
}