using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace eagle {
    public class ProvinceUnderAttackAlertController : MonoBehaviour {
        private static readonly Color UnderAttackTextColor = new(0.55f, 0.05f, 0.04f);
        private static readonly Color UnderAttackTextPulseColor = new(1.0f, 0.2f, 0.08f);

        [SerializeField]
        private ProvinceInfoPanelController provinceInfoPanelController;
        [SerializeField]
        private CommandPanelController commandPanelController;
        [SerializeField]
        private GameObject alertPanel;
        [SerializeField]
        private TextMeshProUGUI alertText;
        [SerializeField]
        private RectTransform freeHeroesPanel;
        [SerializeField]
        private RectTransform factionsAndMovingArmiesPanel;

        private LayoutElement freeHeroesLayoutElement;
        private LayoutElement factionsAndMovingArmiesLayoutElement;
        private float freeHeroesMinimumHeight;
        private float factionsAndMovingArmiesMinimumHeight;
        private float alertHeightPerFlexiblePanel;

        private void Awake() {
            freeHeroesLayoutElement = freeHeroesPanel.GetComponent<LayoutElement>();
            factionsAndMovingArmiesLayoutElement =
                    factionsAndMovingArmiesPanel.GetComponent<LayoutElement>();
            freeHeroesMinimumHeight = freeHeroesLayoutElement.minHeight;
            factionsAndMovingArmiesMinimumHeight = factionsAndMovingArmiesLayoutElement.minHeight;
            var alertLayoutElement = alertPanel.GetComponent<LayoutElement>();
            var columnLayoutGroup = alertPanel.transform.parent.GetComponent<VerticalLayoutGroup>();
            alertHeightPerFlexiblePanel =
                    (alertLayoutElement.preferredHeight + columnLayoutGroup.spacing) / 2f;

            provinceInfoPanelController.UnderAttackChanged += SetUnderAttack;
            SetUnderAttack(provinceInfoPanelController.UnderAttack);
        }

        private void OnDestroy() {
            provinceInfoPanelController.UnderAttackChanged -= SetUnderAttack;
        }

        private void Update() {
            if (!alertPanel.activeSelf) { return; }

            float pulse = (Mathf.Sin(Time.unscaledTime * Mathf.PI * 2f / 1.2f) + 1f) * 0.5f;
            SetAlertTextColor(Color.Lerp(UnderAttackTextColor, UnderAttackTextPulseColor, pulse));
        }

        private void SetUnderAttack(bool underAttack) {
            alertPanel.SetActive(underAttack);
            commandPanelController.underAttackText.gameObject.SetActive(underAttack);
            freeHeroesLayoutElement.minHeight =
                    underAttack ? freeHeroesMinimumHeight - alertHeightPerFlexiblePanel
                                : freeHeroesMinimumHeight;
            factionsAndMovingArmiesLayoutElement.minHeight =
                    underAttack ? factionsAndMovingArmiesMinimumHeight - alertHeightPerFlexiblePanel
                                : factionsAndMovingArmiesMinimumHeight;
            if (underAttack) { SetAlertTextColor(UnderAttackTextColor); }
        }

        private void SetAlertTextColor(Color color) {
            alertText.color = color;
            commandPanelController.underAttackText.color = color;
        }
    }
}
