using System;
using System.Collections.Generic;
using System.Linq;
using Net.Eagle0.Eagle.Api;
using Net.Eagle0.Eagle.Api.Command.Util;
using Net.Eagle0.Eagle.Views;

namespace eagle {
    using HeroId = Int32;
    public class DeclineQuestCommandSelector : CommandSelector {
        public HeroDropdownController targetHeroDropdown;
        public HeroDetailsController heroDetailsController;
        public GeneratedTextUpdater declineQuestText;

        public override AvailableCommand.SealedValueOneofCase CommandType =>
                AvailableCommand.SealedValueOneofCase.DeclineQuestCommand;

        public override string HeaderString => "Decline Quest";
        public override string CommitButtonString => "Commit Decline Quest";

        public override SelectedCommand Command => new() {
            DeclineQuestCommand = new DeclineQuestSelectedCommand { HeroId = TargetedHeroId }
        };

        public override bool HeroIsTargetable(HeroId heroId) =>
                DeclineQuestCommand.DeclinableHeroes.Any(dh => dh.Hero.Id == heroId);
        public override void AddTargetedHero(HeroId heroId) => targetHeroDropdown.SelectedHeroId =
                heroId;

        private DeclineQuestAvailableCommand DeclineQuestCommand =>
                _availableCommand.DeclineQuestCommand;

        private HeroId TargetedHeroId => targetHeroDropdown.SelectedHeroId;
        private HeroView TargetedHero => _model.Heroes[TargetedHeroId];
        public override List<HeroId> TargetedHeroIds => new() { TargetedHeroId };
        public ExpandedUnaffiliatedHero TargetedExpandedHero =>
                DeclineQuestCommand.DeclinableHeroes.First(dh => dh.Hero.Id == TargetedHeroId);

        protected override void SetUpUI() {
            targetHeroDropdown.SelectedHeroIdUpdated = () => DisplayTargetedHero();

            targetHeroDropdown.AvailableHeroes =
                    DeclineQuestCommand.DeclinableHeroes.Select(dh => dh.Hero).ToList();

            DisplayTargetedHero();
        }

        public void DropdownSelected(int value) { DisplayTargetedHero(); }

        private void DisplayTargetedHero() {
            heroDetailsController.SetHero(TargetedHero, _model);
            declineQuestText.TextId = TargetedExpandedHero.DivinationTextId;
        }
    }
}