using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Cryville.EEW.Unity {
	record Config(
		string SeverityScheme,
		float SeverityColorMappingLuminanceMultiplier,
		bool UseContinuousColor,
		string ColorScheme,

		string OverrideTimeZone,
		bool DoDisplayTimeZone,
		bool DoSwitchBackToHistory,

		IReadOnlyCollection<EventSourceConfig> EventSources
	) {
		public static Config Default => new(
			"Default",
			1f,
			false,
			"Default",

			null,
			true,
			true,

			new List<EventSourceConfig> {
				new JMAAtomEventSourceConfig(Array.Empty<string>()),
				new UpdateCheckerEventSourceConfig(),
				new WolfxEventSourceConfig(Array.Empty<string>()),
			}
		);
	}

	[JsonPolymorphic(TypeDiscriminatorPropertyName = "Type", UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToBaseType)]
	[JsonDerivedType(typeof(CWAOpenDataEventSourceConfig), "CWAOpenData")]
	[JsonDerivedType(typeof(GlobalQuakeServerEventSourceConfig), "GlobalQuakeServer")]
	[JsonDerivedType(typeof(GlobalQuakeServer15EventSourceConfig), "GlobalQuakeServer15")]
	[JsonDerivedType(typeof(JMAAtomEventSourceConfig), "JMAAtom")]
	[JsonDerivedType(typeof(NOAAEventSourceConfig), "NOAA")]
	[JsonDerivedType(typeof(UpdateCheckerEventSourceConfig), "UpdateChecker")]
	[JsonDerivedType(typeof(WolfxEventSourceConfig), "Wolfx")]
	abstract record EventSourceConfig();
	record CWAOpenDataEventSourceConfig([property: JsonRequired] string Subtype, [property: JsonRequired] string Token) : EventSourceConfig;
	record GlobalQuakeServerEventSourceConfig([property: JsonRequired] string Host, int Port = 38000) : EventSourceConfig;
	record GlobalQuakeServer15EventSourceConfig(string Host, int Port = 38000) : GlobalQuakeServerEventSourceConfig(Host, Port);
	record JMAAtomEventSourceConfig(IReadOnlyCollection<string> Filter = null, bool IsFilterWhitelist = false) : EventSourceConfig;
	record NOAAEventSourceConfig([property: JsonRequired] string Subtype) : EventSourceConfig;
	record UpdateCheckerEventSourceConfig : EventSourceConfig;
	record WolfxEventSourceConfig(IReadOnlyCollection<string> Filter = null, bool IsFilterWhitelist = false) : EventSourceConfig;

	[JsonSerializable(typeof(Config))]
	[JsonSourceGenerationOptions(WriteIndented = true)]
	sealed partial class ConfigSerializationContext : JsonSerializerContext { }
}
