using System;
using UnityEngine;
using UnityEngine.UI;

namespace eagle {
    public class OrganizeTroopsTableRow : TableRowController {
        public LayoutElement layoutElement;
        public OrganizeTroopsResultRow resultRow;

        public Button plusButton;
        public Button minusButton;
        public Button maxButton;
        public Button dismissButton;

        public Action PlusButtonClickedCallback;
        public Action MinusButtonClickedCallback;
        public Action MaxButtonClickedCallback;
        public Action DismissButtonClickedCallback;

        private BattalionDisplayInfo _displayInfo;

        private bool _canAugment = true;
        public Boolean CanAugment {
            get => _canAugment;
            set {
                _canAugment = value;
                SetButtons();
            }
        }

        private void SetButtons() {
            AllowPlus = BattalionInfo.Count < BattalionInfo.Capacity && CanAugment;
            AllowMax = BattalionInfo.Count < BattalionInfo.Capacity && CanAugment;
            AllowMinus = BattalionInfo.Count > 0;
        }

        public BattalionDisplayInfo BattalionInfo {
            get => _displayInfo;
            set {
                _displayInfo = value;

                resultRow.BattalionImage = value.Image;
                resultRow.Size = value.Count;
                resultRow.Armament = value.Armament;
                resultRow.Training = value.Training;
                resultRow.Price = value.Cost;
                resultRow.Capacity = value.Capacity;
                resultRow.Name = value.Name;

                SetButtons();

                DrawComponents();
            }
        }

        private void DrawComponents() {
            var resultHeight = resultRow.GetComponent<LayoutElement>().minHeight;
            layoutElement.minHeight = resultHeight;
        }

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

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

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

        public void PlusButtonClicked() { PlusButtonClickedCallback(); }

        public void MinusButtonClicked() { MinusButtonClickedCallback(); }

        public void MaxButtonClicked() { MaxButtonClickedCallback(); }

        public void DismissButtonClicked() { DismissButtonClickedCallback(); }
    }
}
