using System;
using System.Collections.Generic;
using System.Linq;
using Net.Eagle0.Eagle.Api;
using Net.Eagle0.Eagle.Views;
using TMPro;
using UnityEngine.UI;

namespace eagle {
    using HeroId = Int32;
    using ProvinceId = Int32;

    public class DivineCommandSelector : CommandSelector {
        public HeroDetailsController heroDetails;
        public TMP_Text goldCostLabel;
        public Button nextHeroButton;
        public Toggle divineAllToggle;
        public TMP_Text divineAllText;

        private bool DivineAllSelected => divineAllToggle.isOn;

        private DivineAvailableCommand DivineCommand => _availableCommand.DivineCommand;
        private ProvinceId ActingProvinceId => DivineCommand.ActingProvinceId;

        public override AvailableCommand.SealedValueOneofCase CommandType =>
                AvailableCommand.SealedValueOneofCase.DivineCommand;
        public override string HeaderString => "Divine Heroes";
        public override string CommitButtonString =>
                DivineAllSelected ? "Commit Divine All" : "Commit Divine";

        public override bool HeroIsTargetable(HeroId heroId) =>
                DivineCommand.DivinableHeroes.Any(uh => uh.Hero.Id == heroId);
        public override List<HeroId> TargetedHeroIds => new List<HeroId> { SelectedHeroId };

        private int _selectedHeroIndex;
        private HeroView SelectedHero =>
                DivineCommand.DivinableHeroes.ToArray()[_selectedHeroIndex].Hero;
        private HeroId SelectedHeroId => SelectedHero.Id;
        private int DivinableCount => DivineCommand.DivinableHeroes.Count;

        protected override void SetUpUI() {
            if (_selectedHeroIndex >= DivinableCount) { _selectedHeroIndex = 0; }
            heroDetails.SetHero(SelectedHero, _model);

            nextHeroButton.gameObject.SetActive(DivinableCount > 1);

            bool enableDivineAll =
                    DivinableCount > 1 && _model.Provinces[ActingProvinceId].FullInfo.Gold >=
                                                  DivinableCount * DivineCommand.CostPerHero;
            divineAllToggle.enabled = enableDivineAll;
            divineAllToggle.gameObject.SetActive(enableDivineAll);
            divineAllText.text = $"Divine All ({DivinableCount})";
            divineAllText.enabled = enableDivineAll;
            divineAllText.gameObject.SetActive(enableDivineAll);

            divineAllToggle.isOn = enableDivineAll;
            SetCostLabel();
        }

        public override SelectedCommand Command {
            get {
                var dc = new DivineSelectedCommand();
                if (DivineAllSelected) {
                    dc.HeroIds.Add(DivineCommand.DivinableHeroes.Select(h => h.Hero.Id));
                } else {
                    dc.HeroIds.Add(SelectedHeroId);
                }
                var cmd = new SelectedCommand { DivineCommand = dc };

                return cmd;
            }
        }

        public void NextHero() {
            _selectedHeroIndex++;
            if (_selectedHeroIndex >= DivinableCount) { _selectedHeroIndex = 0; }
            heroDetails.SetHero(SelectedHero, _model);
        }

        private void SetCostLabel() {
            if (divineAllToggle.isOn) {
                goldCostLabel.text = (DivineCommand.CostPerHero * DivinableCount).ToString();
            } else {
                goldCostLabel.text = DivineCommand.CostPerHero.ToString();
            }
        }

        public void ToggleClicked(bool value) { SetCostLabel(); }
    }
}
