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

namespace eagle {
    public class SelectedUnitRowController : TableRowController, System.IDisposable {
        public RawImage professionImage;
        public RawImage archeryImage;
        public RawImage fireImage;
        public TMP_Text heroNameLabel;

        public RawImage battalionImage;
        public TMP_Text sizeLabel;
        public TMP_Text trainingLabel;
        public RawImage trainImage;
        public TMP_Text armamentLabel;
        public RawImage armamentImage;
        public TMP_Text battalionNameLabel;

        private DynamicHeroTextUpdater textUpdater = new DynamicHeroTextUpdater();

        public HeroView Hero {
            set {
                archeryImage.gameObject.SetActive(false);
                fireImage.gameObject.SetActive(false);

                if (value == null) {
                    heroNameLabel.text = "None";
                    heroNameLabel.color = Color.red;
                } else {
                    professionImage.texture =
                            gameObject.GetComponentInParent<EagleCommonTextures>().Profession(
                                    value.Profession);
                    textUpdater.SetHeroText(heroNameLabel, value.NameTextId, "Hero");
                    archeryImage.gameObject.SetActive(value.ArcheryCapable);
                    fireImage.gameObject.SetActive(value.StartFireCapable);
                }
                professionImage.gameObject.SetActive(professionImage.texture != null);
            }
        }

        public UnityAction HeroClickDetector {
            set {
                heroNameLabel.GetComponent<TableRowClickDetector>().onClick.AddListener(value);
                heroNameLabel.GetComponent<TableRowClickDetector>().selectable = true;
                professionImage.GetComponent<TableRowClickDetector>().onClick.AddListener(value);
                professionImage.GetComponent<TableRowClickDetector>().selectable = true;
                archeryImage.GetComponent<TableRowClickDetector>().onClick.AddListener(value);
                archeryImage.GetComponent<TableRowClickDetector>().selectable = true;
                fireImage.GetComponent<TableRowClickDetector>().onClick.AddListener(value);
                fireImage.GetComponent<TableRowClickDetector>().selectable = true;
            }
        }

        public bool Appropriate {
            set {
                if (value) {
                    heroNameLabel.color = Color.black;
                } else {
                    heroNameLabel.color = Color.red;
                }
            }
        }

        public BattalionView Battalion {
            set {
                if (value == null) {
                    battalionImage.color = Color.clear;
                    trainImage.color = Color.clear;
                    armamentImage.color = Color.clear;
                    sizeLabel.text = "";
                    trainingLabel.text = "";
                    armamentLabel.text = "";
                    battalionNameLabel.text = "";
                } else {
                    battalionImage.texture =
                            gameObject.GetComponentInParent<EagleCommonTextures>().BattalionType(
                                    value.Type);
                    if (battalionImage.texture == null) { battalionImage.color = Color.clear; }
                    sizeLabel.text = value.Size.ToString();
                    trainingLabel.text = ((int)value.Training).ToString();
                    armamentLabel.text = ((int)value.Armament).ToString();
                    battalionNameLabel.text = value.Name;
                }
            }
        }

        public UnityAction BattalionClickDetector {
            set {
                battalionImage.GetComponentInChildren<TableRowClickDetector>().onClick.AddListener(
                        value);
                battalionImage.GetComponentInChildren<TableRowClickDetector>().selectable = true;
                sizeLabel.GetComponentInChildren<TableRowClickDetector>().onClick.AddListener(
                        value);
                sizeLabel.GetComponentInChildren<TableRowClickDetector>().selectable = true;
                trainingLabel.GetComponentInChildren<TableRowClickDetector>().onClick.AddListener(
                        value);
                trainingLabel.GetComponentInChildren<TableRowClickDetector>().selectable = true;
                armamentLabel.GetComponentInChildren<TableRowClickDetector>().onClick.AddListener(
                        value);
                armamentLabel.GetComponentInChildren<TableRowClickDetector>().selectable = true;
                trainImage.GetComponentInChildren<TableRowClickDetector>().onClick.AddListener(
                        value);
                trainImage.GetComponentInChildren<TableRowClickDetector>().selectable = true;
                armamentImage.GetComponentInChildren<TableRowClickDetector>().onClick.AddListener(
                        value);
                armamentImage.GetComponentInChildren<TableRowClickDetector>().selectable = true;
            }
        }

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

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