using System;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using static UnityEngine.EventSystems.PointerEventData;

namespace eagle {
    public class ArmTroopsRowController : TableRowController, IPointerClickHandler {
        private static readonly Color SelectedBackgroundColor = new Color(0.68f, 0.84f, 0.96f, 1f);

        public RawImage battalionImage;
        public TMP_Text oldArmamentLabel;
        public TMP_Text newArmamentLabel;
        public TMP_Text sizeLabel;
        public TMP_Text costPerPointLabel;
        public TMP_Text nameLabel;

        public Button plusButton;
        public Button minusButton;
        public Button resetButton;
        public Button maxButton;

        public Action PlusButtonClickedCallback;
        public Action MinusButtonClickedCallback;
        public Action ResetButtonClickedCallback;
        public Action MaxButtonClickedCallback;
        public Action RowClickedCallback;

        public Texture BattalionImage { set => battalionImage.texture = value; }

        public double CostPerPoint { set => costPerPointLabel.text = value.ToString("0.##"); }

        public double OldArmament { set => oldArmamentLabel.text = Math.Floor(value).ToString(); }

        public double NewArmament { set => newArmamentLabel.text = Math.Floor(value).ToString(); }

        public int Size { set => sizeLabel.text = value.ToString(); }

        public string Name { set => nameLabel.text = value; }

        public bool AllowPlus { set => plusButton.interactable = value; }

        public bool AllowMinus { set => minusButton.interactable = value; }

        public bool AllowReset { set => resetButton.interactable = value; }

        public bool AllowMax { set => maxButton.interactable = value; }

        public void SetActionButtonsVisible(bool visible) {
            plusButton.gameObject.SetActive(visible);
            minusButton.gameObject.SetActive(visible);
            resetButton.gameObject.SetActive(visible);
            maxButton.gameObject.SetActive(visible);
        }

        public void SetNameVisible(bool visible) { nameLabel.gameObject.SetActive(visible); }

        public void SetContentRaycastTargets(bool enabled) {
            foreach (var graphic in GetComponentsInChildren<Graphic>()) {
                if (graphic.gameObject == gameObject) continue;
                graphic.raycastTarget = enabled;
            }
        }

        public override void ColorRow() {
            base.ColorRow();
            if (Selected) { gameObject.GetComponent<Image>().color = SelectedBackgroundColor; }
        }

        public void OnPointerClick(PointerEventData eventData) {
            if (eventData.button == InputButton.Left) { RowClickedCallback?.Invoke(); }
        }

        public void PlusButtonClicked() { PlusButtonClickedCallback(); }

        public void MinusButtonClicked() { MinusButtonClickedCallback(); }

        public void ResetButtonClicked() { ResetButtonClickedCallback(); }

        public void MaxButtonClicked() { MaxButtonClickedCallback(); }
    }
}
