using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

namespace eagle {
    public class OrganizeTroopsTableRow : TableRowController {
        public LayoutElement layoutElement;
        public OrganizeTroopsResultRow resultRow;
        public OrganizeTroopsComponentRow componentRowPrefab;
        public Transform componentsLayoutTransform;
        private List<OrganizeTroopsComponentRow> existingComponents =
                new List<OrganizeTroopsComponentRow>();

        public Toggle disclosureTriangle;

        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();

                disclosureTriangle.isOn = value.Open;

                _components = value.Components;
                DrawComponents();
            }
        }

        private List<BattalionComponent> _components = new List<BattalionComponent>();
        public List<BattalionComponent> Components { get => _components; }

        private void DrawComponents() {
            existingComponents.ForEach(t => Destroy(t.gameObject));
            existingComponents.Clear();

            var resultHeight = resultRow.GetComponent<LayoutElement>().minHeight;

            if (disclosureTriangle.isOn) {
                existingComponents = Components
                                             .Select(comp => {
                                                 var cr = Instantiate(
                                                         componentRowPrefab,
                                                         componentsLayoutTransform);
                                                 cr.BattalionComponent = comp;
                                                 return cr;
                                             })
                                             .ToList();

                layoutElement.minHeight =
                        resultHeight +
                        existingComponents
                                .Select(c => c.gameObject.GetComponent<LayoutElement>().minHeight)
                                .Sum();
            } else {
                layoutElement.minHeight = resultHeight;
            }
        }

        // Start is called before the first frame update
        private void Start() {
            disclosureTriangle.isOn = BattalionInfo != null && BattalionInfo.Open;
        }

        public void DisclosureTriangleToggled(bool newValue) {
            BattalionInfo.Open = newValue;

            DrawComponents();
        }

        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(); }
    }
}