﻿﻿﻿﻿﻿﻿using CamBooth.App.Core.Logging;

namespace CamBooth.App.Core.AppSettings;

using Microsoft.Extensions.Configuration;

public class AppSettingsService
{
	private readonly Logger _logger;

	IConfigurationRoot configuration;
	
	private string loadedConfigFile = string.Empty;
	
	public AppSettingsService(Logger logger)
	{
		this._logger = logger;
		this.Initialize();
	}


	private void Initialize()
	{
		var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
		
		loadedConfigFile = "Core/AppSettings/app.settings.json";
		var configBuilder = new ConfigurationBuilder()
			.SetBasePath(AppContext.BaseDirectory)
			.AddJsonFile(loadedConfigFile, optional: false, reloadOnChange: true);

		// Lade umgebungsspezifische Konfigurationsdatei
		if (environment == "Development")
		{
			configBuilder.AddJsonFile("Core/AppSettings/app.settings.dev.json", optional: true, reloadOnChange: true);
			loadedConfigFile = "Core/AppSettings/app.settings.dev.json";
		}

		configuration = configBuilder.Build();

		_logger.Info($"Konfiguration geladen aus: {loadedConfigFile} (Environment: {environment})");
	}

	public bool IsDebugMode => bool.Parse(configuration["AppSettings:IsDebugMode"] ?? "false");

	public string? AppName => configuration["AppSettings:AppName"];

	public bool IsDebugConsoleVisible => bool.Parse(configuration["AppSettings:DebugConsoleVisible"] ?? string.Empty);
	
	public string? PictureLocation => configuration["AppSettings:PictureLocation"];

	public int PhotoCountdownSeconds => int.Parse(configuration["AppSettings:PhotoCountdownSeconds"] ?? "5");

	public int FocusDelaySeconds => int.Parse(configuration["AppSettings:FocusDelaySeconds"] ?? "2");

	public int FocusTimeoutMs => int.Parse(configuration["AppSettings:FocusTimeoutMs"] ?? "3000");

	public bool IsShutdownEnabled => bool.Parse(configuration["AppSettings:IsShutdownEnabled"] ?? "false");

	public bool UseMockCamera => bool.Parse(configuration["AppSettings:UseMockCamera"] ?? "false");
	
	public string? ConnectionString => configuration.GetConnectionString("DefaultConnection");

	public string  ConfigFileName => loadedConfigFile;

	// Logging Settings
	public string LogLevel => configuration["LoggingSettings:LogLevel"] ?? "Information";

	public string LogDirectory => configuration["LoggingSettings:LogDirectory"] ?? "Logs";

	public string? RemoteServerUrl => configuration["LoggingSettings:RemoteServerUrl"];

	public string? RemoteServerApiKey => configuration["LoggingSettings:RemoteServerApiKey"];

	// Lychee Upload Settings (deprecated - wird durch PhotoPrism ersetzt)
	public string? LycheeApiUrl => configuration["LycheeSettings:ApiUrl"];

	public string? LycheeUsername => configuration["LycheeSettings:Username"];

	public string? LycheePassword => configuration["LycheeSettings:Password"];

	public string? LycheeDefaultAlbumId => configuration["LycheeSettings:DefaultAlbumId"];

	public bool LycheeAutoUploadEnabled => bool.Parse(configuration["LycheeSettings:AutoUploadEnabled"] ?? "false");

	// PhotoPrism Upload Settings
	public string? PhotoPrismApiUrl => configuration["PhotoPrismSettings:ApiUrl"];

	public string? PhotoPrismApiKey => configuration["PhotoPrismSettings:ApiKey"];

	public string? PhotoPrismUsername => configuration["PhotoPrismSettings:Username"];

	public string? PhotoPrismPassword => configuration["PhotoPrismSettings:Password"];

	public string? PhotoPrismAlbumShareUrl => configuration["PhotoPrismSettings:AlbumShareUrl"];

	public bool PhotoPrismAutoUploadEnabled => bool.Parse(configuration["PhotoPrismSettings:AutoUploadEnabled"] ?? "false");
}