using System;
using System.Collections.Generic;
using System.Linq;
using Net.Eagle0.Eagle.Api;

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

    public class SwearBrotherhoodCommandSelector : CommandSelector {
        public HeroDropdownController heroDropdownController;
        public HeroDetailsController heroDetails;
        public GeneratedTextUpdater backgroundText;

        public override AvailableCommand.SealedValueOneofCase CommandType =>
                AvailableCommand.SealedValueOneofCase.SwearBrotherhoodCommand;
        public override string HeaderString => "Swear Brotherhood with Hero";
        public override string CommitButtonString => "Commit Oath";

        public override List<HeroId> TargetedHeroIds => new List<HeroId> { SelectedHeroId };
        public override bool HeroIsTargetable(HeroId heroId) =>
                SwearBrotherhoodAvailableCommand.AvailableHeroes.Any(h => h.HeroId == heroId);
        public override void AddTargetedHero(HeroId heroId) =>
                heroDropdownController.SelectedHeroId = heroId;

        private HeroId SelectedHeroId => heroDropdownController.SelectedHeroId;

        private SwearBrotherhoodAvailableCommand SwearBrotherhoodAvailableCommand =>
                _availableCommand.SwearBrotherhoodCommand;
        private ProvinceId ActingProvinceId => SwearBrotherhoodAvailableCommand.ActingProvinceId;

        protected override void SetUpUI() {
            heroDropdownController.AvailableHeroes =
                    SwearBrotherhoodAvailableCommand.AvailableHeroes
                            .Select(hab => _model.Heroes[hab.HeroId])
                            .ToList();

            heroDropdownController.SelectedHeroIdUpdated = () => HeroDropdownChanged();

            HeroDropdownChanged();
        }

        void HeroDropdownChanged() {
            heroDetails.SetHero(_model.Heroes[SelectedHeroId], _model);
            backgroundText.TextId = SwearBrotherhoodAvailableCommand
                                            .AvailableHeroes[heroDropdownController.SelectedIndex]
                                            .BackstoryTextId;
        }

        public override SelectedCommand Command => new SelectedCommand {
            SwearBrotherhoodCommand =
                    new SwearBrotherhoodSelectedCommand { NewBrotherHeroId = SelectedHeroId }
        };
    }
}
