using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace LocalDataStorageWithSQLite.StorageUtils
{
    internal class FileSystemStorage
    {
        private string _dir = $"{FileSystem.AppDataDirectory}/Temp";

        public FileSystemStorage()
        {
            if (!Directory.Exists(_dir))
                Directory.CreateDirectory(_dir);
        }

        public T Get<T>(string key)
        {
            string path = Path.Combine(_dir, key);
            if (!File.Exists(path))
                return default(T);

            string raw = File.ReadAllText(path);
            T result = JsonSerializer.Deserialize<T>(raw);
            return result;
        }

        public void Set<T>(string key, T value)
        {
            var serialized = JsonSerializer.Serialize(value);
            string path = Path.Combine(_dir, key);
            File.WriteAllText(path, serialized);
        }
    }
}
