using Cryville.Common.Unity.UI;
using Cryville.EEW.Colors;
using Cryville.EEW.Report;
using System;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using UnityEngine.UI;

namespace Cryville.EEW.Unity.UI {
	abstract class EventBaseView : MonoBehaviour {
		[SerializeField] Image[] m_reportView;

		[SerializeField] Image m_keyView;
		[SerializeField] TMPLocalizedText m_keyTitleView;
		[SerializeField] TMPLocalizedText m_keyValueView;
		[SerializeField] TMPLocalizedText m_keyConditionView;

		[SerializeField] TMPLocalizedText m_locationView;
		[SerializeField] TMPLocalizedText m_predicateView;
		[SerializeField] TMPLocalizedText m_timeView;
		[SerializeField] EventPropertyListView m_propertyListView;

		protected virtual bool UseShortTimeFormat => false;
		void SetSeverity(float severity, out IColor color) {
			color = SharedSettings.Instance.SeverityColorMapping.From(severity);
			SetMainColor(color.ToSrgb().ToUnityColor());
		}
		protected void SetMainColor(Color color, Image image) {
			image.color = color;
		}
		protected virtual void SetMainColor(Color color) {
			foreach (var view in m_reportView)
				SetMainColor(color, view);
			m_keyView.color = color;
		}
		protected virtual void SetView(float mainSeverity, Localized<string> location, Localized<string> predicate, Localized<DateTime?> time, TimeZoneInfo timeZone, Localized<ReportViewModelProperty> keyProp, IEnumerable<Localized<ReportViewModelProperty>> props) {
			SetSeverity(mainSeverity, out var mainColor);
			SetText(m_locationView, location.Value, location.Culture);
			SetText(m_predicateView, predicate.Value, predicate.Culture);
			if (time.Value is DateTime ttime) {
				if (SharedSettings.Instance.OverrideTimeZone is TimeZoneInfo tTimeZone)
					ttime = TimeZoneInfo.ConvertTime(ttime, timeZone, tTimeZone);
				else
					tTimeZone = timeZone;
				if (UseShortTimeFormat) {
					SetText(m_timeView, ttime.ToString("G", time.Culture), time.Culture);
				}
				else {
					SetText(m_timeView, SharedSettings.Instance.DoDisplayTimeZone ? string.Format(time.Culture, "{0:G} ({1})", ttime, tTimeZone.ToTimeZoneString()) : ttime.ToString(time.Culture), time.Culture);
				}
			}
			else {
				SetText(m_timeView, null, time.Culture);
			}
			if (keyProp.Value != null) {
				m_keyView.gameObject.SetActive(true);
				Color color = SharedSettings.Instance.TextColorScheme.From("Severity", mainSeverity, mainColor).ToSrgb().ToUnityColor();
				m_keyTitleView.Text.color = color;
				m_keyValueView.Text.color = color;
				m_keyConditionView.Text.color = color;
				SetText(m_keyTitleView, keyProp.Value.Key, keyProp.Culture);
				SetText(m_keyValueView, keyProp.Value.Value, keyProp.Culture);
				SetText(m_keyConditionView, keyProp.Value.Condition, keyProp.Culture);
			}
			else {
				m_keyView.gameObject.SetActive(false);
			}
			m_propertyListView.Set(props);
		}

		protected static void SetText(TMPLocalizedText view, string text, CultureInfo culture) {
			if (string.IsNullOrWhiteSpace(text)) {
				view.gameObject.SetActive(false);
				return;
			}
			view.gameObject.SetActive(true);
			view.SetText(text, culture);
		}
	}
}
