using Minesweeper.Core;
using UnityEngine;

namespace Minesweeper.Infrastructure
{
    public sealed class PlayerPrefsGameSettingsStorage : IGameSettingsStorage
    {
        private const string SizeXKey = "Minesweeper.Settings.SizeX";
        private const string SizeYKey = "Minesweeper.Settings.SizeY";
        private const string MinesCountKey = "Minesweeper.Settings.MinesCount";

        public bool TryLoad(out GameSettingsValue value)
        {
            if (!PlayerPrefs.HasKey(SizeXKey) || !PlayerPrefs.HasKey(SizeYKey) || !PlayerPrefs.HasKey(MinesCountKey))
            {
                value = default;
                return false;
            }

            value = new GameSettingsValue(PlayerPrefs.GetInt(SizeXKey), PlayerPrefs.GetInt(SizeYKey), PlayerPrefs.GetInt(MinesCountKey));
            return true;
        }

        public void Save(GameSettingsValue value)
        {
            PlayerPrefs.SetInt(SizeXKey, value.SizeX);
            PlayerPrefs.SetInt(SizeYKey, value.SizeY);
            PlayerPrefs.SetInt(MinesCountKey, value.MinesCount);
            PlayerPrefs.Save();
        }
    }
}
