using System;
using System.IO;
using Newtonsoft.Json;
namespace AGVMapEditor.Models
{
///
/// AGV 맵 에디터의 환경설정을 관리하는 클래스
///
public class EditorSettings
{
#region Properties
///
/// 마지막으로 열었던 맵 파일의 경로
///
public string LastMapFilePath { get; set; } = string.Empty;
///
/// 프로그램 시작시 마지막 맵 파일을 자동으로 로드할지 여부
///
public bool AutoLoadLastMapFile { get; set; } = true;
///
/// 설정이 마지막으로 저장된 시간
///
public DateTime LastSaved { get; set; } = DateTime.Now;
///
/// 기본 맵 파일 저장 디렉토리
///
public string DefaultMapDirectory { get; set; } = string.Empty;
#endregion
#region Constants
private static readonly string SettingsFileName = "EditorSettings.json";
private static readonly string SettingsDirectory = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"AGVMapEditor");
private static readonly string SettingsFilePath = Path.Combine(SettingsDirectory, SettingsFileName);
#endregion
#region Static Instance
private static EditorSettings _instance;
private static readonly object _lock = new object();
///
/// 싱글톤 인스턴스
///
public static EditorSettings Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
_instance = LoadSettings();
}
}
}
return _instance;
}
}
#endregion
#region Methods
///
/// 설정을 파일에서 로드
///
private static EditorSettings LoadSettings()
{
try
{
if (File.Exists(SettingsFilePath))
{
string jsonContent = File.ReadAllText(SettingsFilePath);
var settings = JsonConvert.DeserializeObject(jsonContent);
return settings ?? new EditorSettings();
}
}
catch (Exception ex)
{
// 설정 로드 실패시 기본 설정 사용
System.Diagnostics.Debug.WriteLine($"설정 로드 실패: {ex.Message}");
}
return new EditorSettings();
}
///
/// 설정을 파일에 저장
///
public void Save()
{
try
{
// 디렉토리가 없으면 생성
if (!Directory.Exists(SettingsDirectory))
{
Directory.CreateDirectory(SettingsDirectory);
}
LastSaved = DateTime.Now;
string jsonContent = JsonConvert.SerializeObject(this, Formatting.Indented);
File.WriteAllText(SettingsFilePath, jsonContent);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"설정 저장 실패: {ex.Message}");
}
}
///
/// 마지막 맵 파일 경로 업데이트
///
/// 맵 파일 경로
public void UpdateLastMapFile(string filePath)
{
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{
LastMapFilePath = filePath;
// 기본 디렉토리도 업데이트
DefaultMapDirectory = Path.GetDirectoryName(filePath);
Save();
}
}
///
/// 마지막 맵 파일이 존재하는지 확인
///
public bool HasValidLastMapFile()
{
return !string.IsNullOrEmpty(LastMapFilePath) && File.Exists(LastMapFilePath);
}
///
/// 설정 초기화
///
public void Reset()
{
LastMapFilePath = string.Empty;
AutoLoadLastMapFile = true;
DefaultMapDirectory = string.Empty;
LastSaved = DateTime.Now;
Save();
}
#endregion
}
}