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

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

    public class HandleRiotCrackDownCommandSelector : CommandSelector {
        public HeroDropdownController heroDropdownController;
        public BattalionDropdownController battalionDropdownController;

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

        public override string HeaderString => "Crack Down";
        public override string CommitButtonString => "Commit Crack Down";

        public override bool WarnOnCommitButton => !SelectedBattalionId.HasValue;
        public override string CommitWarningText => "You aren't sending a battalion. Are you sure?";

        public override List<HeroId> TargetedHeroIds => new List<HeroId> { SelectedHeroId };
        public override bool HeroIsTargetable(HeroId heroId) =>
                HandleRiotCrackDownAvailableCommand.AvailableHeroIds.Contains(heroId);
        public override void AddTargetedHero(HeroId heroId) =>
                heroDropdownController.SelectedHeroId = heroId;

        public override List<BattalionId> TargetedBattalionIds =>
                SelectedBattalionId.HasValue ? new List<BattalionId> { SelectedBattalionId.Value }
                                             : new List<BattalionId>();
        public override bool BattalionIsTargetable(BattalionId bid) =>
                HandleRiotCrackDownAvailableCommand.BattalionIdsAvailable.Contains(bid);
        public override void AddTargetedBattalion(BattalionId bid) =>
                battalionDropdownController.SelectedBattalionId = bid;

        private HeroId SelectedHeroId => heroDropdownController.SelectedHeroId;
        private BattalionId? SelectedBattalionId => battalionDropdownController.SelectedBattalionId;

        private HandleRiotCrackDownAvailableCommand HandleRiotCrackDownAvailableCommand =>
                _availableCommand.HandleRiotCrackDownAvailableCommand;
        private ProvinceId ActingProvinceId => HandleRiotCrackDownAvailableCommand.ActingProvinceId;

        private ProvinceView Province => _model.Provinces[ActingProvinceId];

        public override SelectedCommand Command => new SelectedCommand {
            HandleRiotCrackDownSelectedCommand =
                    new HandleRiotCrackDownSelectedCommand {
                        HeroId = SelectedHeroId,
                        BattalionId = SelectedBattalionId
                    }
        };

        protected override void SetUpUI() {
            heroDropdownController.AvailableHeroes =
                    HandleRiotCrackDownAvailableCommand.AvailableHeroIds
                            .Select(id => _model.Heroes[id])
                            .ToList();
            battalionDropdownController.AvailableBattalions =
                    HandleRiotCrackDownAvailableCommand.BattalionIdsAvailable
                            .Select(id => Province.FullInfo.Battalions.First(b => b.Id == id))
                            .OrderBy(batt => batt.Type)
                            .ToList();
        }

        // Start is called before the first frame update
        void Start() {}
    }
}