using EagleGUIUtils;
using Net.Eagle0.Eagle.Views;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace eagle {
    public class HeroRowController : TableRowController, System.IDisposable {
        public RawImage professionImage;
        public TMP_Text heroNameLabel;
        public TMP_Text strengthLabel;
        public TMP_Text strengthXpLabel;
        public TMP_Text agilityLabel;
        public TMP_Text agilityXpLabel;
        public TMP_Text wisdomLabel;
        public TMP_Text wisdomXpLabel;
        public TMP_Text charismaLabel;
        public TMP_Text charismaXpLabel;
        public TMP_Text vigorLabel;
        public TMP_Text constitutionXpLabel;
        public TMP_Text loyaltyLabel;

        public RawImage archeryImage;
        public RawImage fireImage;

        private DynamicHeroTextUpdater textUpdater = new DynamicHeroTextUpdater();

        public HeroView Hero {
            set {
                professionImage.texture =
                        gameObject.GetComponentInParent<EagleCommonTextures>().Profession(
                                value.Profession);
                if (!professionImage.texture) {
                    professionImage.color = Color.clear;
                } else {
                    professionImage.color = Color.white;
                }
                textUpdater.SetHeroText(heroNameLabel, value.NameTextId, "Hero");

                // not ideal -- this is just so we don't show 0s for scouted heroes
                if (value.Strength != 0) {
                    strengthLabel.text = value.Strength.ToString();
                    strengthXpLabel.text = $"({value.StrengthXp})";
                    agilityLabel.text = value.Agility.ToString();
                    agilityXpLabel.text = $"({value.AgilityXp})";
                    wisdomLabel.text = value.Wisdom.ToString();
                    wisdomXpLabel.text = $"({value.WisdomXp})";
                    charismaLabel.text = value.Charisma.ToString();
                    charismaXpLabel.text = $"({value.CharismaXp})";
                    vigorLabel.text = value.Vigor == null
                                              ? null
                                              : GUIUtils.ConditionString(value.Vigor) + "/" +
                                                        (value.Constitution.ToString());
                    constitutionXpLabel.text = $"({value.ConstitutionXp})";
                    loyaltyLabel.text =
                            value.Loyalty == null ? "—" : GUIUtils.ConditionString(value.Loyalty);

                    archeryImage.gameObject.SetActive(value.ArcheryCapable);
                    fireImage.gameObject.SetActive(value.StartFireCapable);
                } else {
                    strengthLabel.text = "?";
                    strengthXpLabel.text = "?";
                    agilityLabel.text = "?";
                    agilityXpLabel.text = "?";
                    wisdomLabel.text = "?";
                    wisdomXpLabel.text = "?";
                    charismaLabel.text = "?";
                    charismaXpLabel.text = "?";
                    vigorLabel.text = "?";
                    constitutionXpLabel.text = "?";
                    loyaltyLabel.text = "?";
                }
            }
        }

        public void Dispose() { textUpdater?.Dispose(); }

        private void OnDestroy() { Dispose(); }
    }
}