using System;
using DesertPaintCodex.Models;
using DesertPaintCodex.Util;
using System.Collections.Generic;
using System.IO;
namespace DesertPaintCodex.Services
{
internal static class ProfileManager
{
private static bool _areProfilesLoaded;
private static readonly List<string> _profileList = new();
public static PlayerProfile? CurrentProfile { get; private set; }
public static bool HasProfileLoaded => CurrentProfile != null;
public static List<string> GetProfileList()
{
// If it's already loaded, return the cached list.
if (_areProfilesLoaded)
{
return _profileList;
}
// Otherwise, load the list.
string appDataPath = FileUtils.AppDataPath;
if (!Directory.Exists(appDataPath))
{
Directory.CreateDirectory(appDataPath);
}
DirectoryInfo di = new(appDataPath);
DirectoryInfo[] dirs = di.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
if (dir.Name != "template")
{
_profileList.Add(dir.Name);
}
}
_areProfilesLoaded = true;
return _profileList;
}
public static PlayerProfile LoadProfile(string name)
{
CurrentProfile = new PlayerProfile(name, Path.Combine(FileUtils.AppDataPath, name));
CurrentProfile.Load();
return CurrentProfile;
}
public static void ReloadProfile()
{
if (CurrentProfile == null) return;
LoadProfile(CurrentProfile.Name);
}
public static PlayerProfile CreateNewProfile(string name)
{
CurrentProfile = new PlayerProfile(name, Path.Combine(FileUtils.AppDataPath, name));
CurrentProfile.Initialize();
// Invalidate profile list, so it will reload next time.
_profileList.Clear();
_areProfilesLoaded = false;
return CurrentProfile;
}
public static int GetProfileCount()
{
// This is a function instead of a property, because it may be slow.
List<string> profiles = GetProfileList();
return profiles.Count;
}
public static bool HasProfiles()
{
// This is a function instead of a property, because it may be slow.
List<string> profiles = GetProfileList();
return profiles.Count > 0;
}
public static void UnloadProfile()
{
CurrentProfile = null;
}
}
}