using System;
using System.Collections.Generic;
using common;
using Net.Eagle0.Eagle.Views;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace eagle {
    using ProvinceId = Int32;

    public class PopupPanelController : MonoBehaviour {
        public PopupCanvasController canvas;
        public MapController mapController;
        public TextMeshProUGUI textField;
        public TextMeshProUGUI title;

        public GameObject imagesAndNamesContainer;
        public RawImage firstImage;
        public GeneratedTextUpdater firstName;
        public RawImage secondImage;
        public GeneratedTextUpdater secondName;
        public RawImage thirdImage;
        public GeneratedTextUpdater thirdName;

        public Button nextButton;

        protected struct PopupInfo {
            public string titleText;
            public string mainText;
            public string mainTextLlmId;
            public List<HeroView> displayedHeroes;
            public List<ProvinceId> affectedProvinceIds;
        }

        List<PopupInfo> _popupInfos = new();
        protected List<PopupInfo> PopupInfos {
            get => _popupInfos;
            set {
                _popupInfos = value;
                SetUpPanel();
            }
        }

        public bool HasInfo => PopupInfos.Count > 0;

        protected int DisplayedIndex { get; private set; }

        void Start() { SetUpPanel(); }

        void OnDisable() {
            if (_popupInfos.Count == 0) { canvas.SetChildActive(false); }
        }

        void PopulateMainText(PopupInfo popupInfo) {
            var updater = textField.gameObject.GetComponent<GeneratedTextUpdater>();
            if (popupInfo.mainTextLlmId is string llmId) {
                updater.TextId = llmId;
                updater.PrefixedText = popupInfo.mainText ?? "";
            } else {
                if (updater != null) updater.TextId = null;
                textField.text = popupInfo.mainText;
            }
        }

        protected virtual void SetUpPanel() {
            if (_popupInfos.Count == 0) {
                mapController.OverrideTargetedProvinces = new List<ProvinceId>();
                gameObject.SetActive(false);
            } else {
                if (DisplayedIndex >= _popupInfos.Count) { DisplayedIndex = 0; }
                if (nextButton) nextButton.gameObject.SetActive(PopupInfos.Count > 1);

                title.text = PopupInfos[DisplayedIndex].titleText;
                PopulateMainText(PopupInfos[DisplayedIndex]);
                mapController.OverrideTargetedProvinces =
                        PopupInfos[DisplayedIndex].affectedProvinceIds;

                if (imagesAndNamesContainer) imagesAndNamesContainer.gameObject.SetActive(false);
                if (firstImage) firstImage.transform.parent.gameObject.SetActive(false);
                if (secondImage) secondImage.transform.parent.gameObject.SetActive(false);
                if (thirdImage) thirdImage.transform.parent.gameObject.SetActive(false);

                if (firstName) firstName.TextId = "";
                if (secondName) secondName.TextId = "";
                if (thirdName) thirdName.TextId = "";

                var imageCount = PopupInfos[DisplayedIndex].displayedHeroes == null
                                         ? 0
                                         : PopupInfos[DisplayedIndex].displayedHeroes.Count;
                if (imageCount > 0) {
                    if (imagesAndNamesContainer) imagesAndNamesContainer.gameObject.SetActive(true);

                    var firstHero = PopupInfos[DisplayedIndex].displayedHeroes[0];
                    ResourceFetcher.headshotFetcher.LoadIntoRawImage(
                            firstImage,
                            firstHero.ImagePath);
                    firstImage.transform.parent.gameObject.SetActive(true);
                    if (firstName) firstName.TextId = firstHero.NameTextId;

                    if (imageCount > 1) {
                        var secondHero = PopupInfos[DisplayedIndex].displayedHeroes[1];
                        ResourceFetcher.headshotFetcher.LoadIntoRawImage(
                                secondImage,
                                secondHero.ImagePath);
                        secondImage.transform.parent.gameObject.SetActive(true);
                        if (secondName) secondName.TextId = secondHero.NameTextId;

                        if (imageCount > 2) {
                            var thirdHero = PopupInfos[DisplayedIndex].displayedHeroes[2];
                            ResourceFetcher.headshotFetcher.LoadIntoRawImage(
                                    thirdImage,
                                    thirdHero.ImagePath);
                            thirdImage.transform.parent.gameObject.SetActive(true);
                            if (thirdName) thirdName.TextId = thirdHero.NameTextId;
                        }
                    }
                }

                canvas.SetChildActive(true);
                gameObject.SetActive(true);

                gameObject.transform.SetAsLastSibling();
            }
        }

        public void NextButtonClicked() {
            DisplayedIndex++;
            if (DisplayedIndex >= PopupInfos.Count) { DisplayedIndex = 0; }
            SetUpPanel();
        }
    }

}