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

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

    public class ReconCommandSelector : CommandSelector {
        public HeroDropdownController heroDropdownController;
        public TMP_Dropdown destinationDropdown;

        public override AvailableCommand.SealedValueOneofCase CommandType =>
                AvailableCommand.SealedValueOneofCase.ReconCommand;
        public override string HeaderString => "Recon";
        public override string CommitButtonString => "Commit Recon";

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

        private HeroId SelectedHeroId => heroDropdownController.SelectedHeroId;

        private ReconAvailableCommand ReconAvailableCommand => _availableCommand.ReconCommand;
        private ProvinceId ActingProvinceId => ReconAvailableCommand.ActingProvinceId;

        protected override ProvinceId? TargetedProvince {
            get => ReconAvailableCommand.AvailableTargetProvinces[destinationDropdown.value];
            set {
                if (value.HasValue) {
                    int index = ReconAvailableCommand.AvailableTargetProvinces.IndexOf(value.Value);
                    if (index != -1) destinationDropdown.value = index;
                }
            }
        }

        public override SelectedCommand Command => new SelectedCommand {
            ReconSelectedCommand =
                    new ReconSelectedCommand {
                        ActingHeroId = SelectedHeroId,
                        TargetProvinceId = TargetedProvince.Value
                    }
        };

        protected override void SetUpUI() {
            heroDropdownController.AvailableHeroes =
                    ReconAvailableCommand.AvailableHeroIds.Select(hid => _model.Heroes[hid])
                            .ToList();

            destinationDropdown.ClearOptions();
            var destinationProvinceNames = ReconAvailableCommand.AvailableTargetProvinces
                                                   .Select(id => GUIUtils.DarkColoredProvinceName(
                                                                   _model.Provinces[id],
                                                                   _model.PlayerId.Value))
                                                   .ToList();

            destinationDropdown.AddOptions(destinationProvinceNames);
            destinationDropdown.value = 0;
        }

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