using System.Collections.Generic;
using System.Linq;
using common;
using Net.Eagle0.Common;
using TMPro;
using UnityEngine.UI;

namespace eagle {
    public class CustomUnitRowController : TableRowController {
        public TMP_Dropdown professionDropdown;
        public TMP_InputField heroName;
        public TMP_InputField strength;
        public TMP_InputField agility;
        public TMP_InputField constitution;
        public TMP_InputField charisma;
        public TMP_InputField wisdom;
        public TMP_InputField bravery;
        public TMP_InputField vigor;
        public TMP_Dropdown battalionTypeDropdown;
        public TMP_InputField sizeLabel;
        public TMP_InputField trainingLabel;
        public TMP_InputField armamentLabel;
        public TMP_Dropdown startingPosition;
        public Toggle isVip;
        public Toggle canFlee;
        public Toggle canArchery;
        public Toggle canStartFire;
        public Button deleteButton;

        public CommonProfession SelectedProfession {
            get => professionDropdown.value == 0 ? CommonProfession.NoProfession
                                                 : (CommonProfession)(professionDropdown.value);
            set => professionDropdown.value = (int)value;
        }

        public CommonBattalionTypeId SelectedBattalionTypeId {
            get => (CommonBattalionTypeId)battalionTypeDropdown.value;
            set => battalionTypeDropdown.value = (int)value;
        }

        private void Start() {}

        public void DeleteButtonClicked() {
            GetComponentInParent<EventBasedTable>().RemoveRow(this);
        }

        private List<int> _availableStartingPositions;
        public List<int> AvailableStartingPositions {
            get => _availableStartingPositions;
            set {
                _availableStartingPositions = value;
                if (_availableStartingPositions.Count == 0) {
                    startingPosition.options = new List<TMP_Dropdown.OptionData> {
                        new TMP_Dropdown.OptionData("DEF")
                    };
                } else {
                    startingPosition.options =
                            _availableStartingPositions
                                    .Select(i => new TMP_Dropdown.OptionData(i.ToString()))
                                    .ToList();
                }

                startingPosition.value = 0;
            }
        }

        public CommonUnit Unit {
            get {
                var hero = new CommonHero {
                    NameTextId = heroName.text,
                    Profession = SelectedProfession,
                    Strength = int.Parse(strength.text),
                    Agility = int.Parse(agility.text),
                    Constitution = int.Parse(constitution.text),
                    Charisma = int.Parse(charisma.text),
                    Wisdom = int.Parse(wisdom.text),
                    Bravery = int.Parse(bravery.text),
                    Vigor = double.Parse(vigor.text),
                    IsVip = isVip.isOn
                };

                var battalion = SelectedBattalionTypeId == CommonBattalionTypeId.Undead
                                        ? null
                                        : new CommonBattalion {
                                              Type = SelectedBattalionTypeId,
                                              Size = int.Parse(sizeLabel.text),
                                              Training = double.Parse(trainingLabel.text),
                                              Armament = double.Parse(armamentLabel.text)
                                          };

                return new CommonUnit {
                    Hero = hero,
                    Battalion = battalion,
                    StartingPositionIndex =
                            _availableStartingPositions != null &&
                            _availableStartingPositions.Count >
                                    0?(int?)_availableStartingPositions[startingPosition.value] :
                                            (int?)null,
                    CanFlee = canFlee.isOn,
                    CanArchery = canArchery.isOn,
                    CanStartFire = canStartFire.isOn
                };
            }
            set {
                heroName.text = value.Hero.NameTextId;
                professionDropdown.value = value.Hero.Profession == CommonProfession.NoProfession
                                                   ? 0
                                                   : (int)value.Hero.Profession;
                strength.text = value.Hero.Strength.ToString();
                agility.text = value.Hero.Agility.ToString();
                constitution.text = value.Hero.Constitution.ToString();
                charisma.text = value.Hero.Charisma.ToString();
                wisdom.text = value.Hero.Wisdom.ToString();
                bravery.text = value.Hero.Bravery.ToString();
                vigor.text = value.Hero.Vigor.ToString();

                SelectedBattalionTypeId = value.Battalion.Type;
                sizeLabel.text = value.Battalion.Size.ToString();
                trainingLabel.text = value.Battalion.Training.ToString();
                armamentLabel.text = value.Battalion.Armament.ToString();

                startingPosition.value = (value.StartingPositionIndex.HasValue &&
                                          _availableStartingPositions != null)
                                                 ? _availableStartingPositions.IndexOf(
                                                           value.StartingPositionIndex.Value)
                                                 : 0;
                canFlee.isOn = value.CanFlee;
            }
        }
    }
}