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 StartEpidemicCommandSelector : CommandSelector {
        public HeroDropdownController heroDropdownController;
        public TMP_Dropdown targetDropdown;

        public override AvailableCommand.SealedValueOneofCase CommandType =>
                AvailableCommand.SealedValueOneofCase.StartEpidemicCommand;
        public override string HeaderString => "Start Plague";
        public override string CommitButtonString => $"Commit Plague";

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

        private HeroId SelectedHeroId => heroDropdownController.SelectedHeroId;

        private StartEpidemicAvailableCommand StartEpidemicAvailableCommand =>
                _availableCommand.StartEpidemicCommand;
        private ProvinceId ActingProvinceId => StartEpidemicAvailableCommand.ActingProvinceId;

        private ProvinceId SelectedTargetProvinceId =>
                StartEpidemicAvailableCommand.Options[targetDropdown.value].TargetProvinceId;

        protected override ProvinceId? TargetedProvince {
            get => SelectedTargetProvinceId;
            set {
                if (value.HasValue) {
                    int index = StartEpidemicAvailableCommand.Options.ToList().FindIndex(
                            seo => seo.TargetProvinceId == value.Value);
                    if (index != -1) targetDropdown.value = index;
                }
            }
        }

        public override SelectedCommand Command => new SelectedCommand {
            StartEpidemicSelectedCommand =
                    new StartEpidemicSelectedCommand {
                        SelectedProvinceId = SelectedTargetProvinceId,
                        ActingHeroId = SelectedHeroId
                    }
        };

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

            targetDropdown.ClearOptions();
            var destinationProvinceNames =
                    StartEpidemicAvailableCommand.Options.Select(opt => opt.TargetProvinceId)
                            .Select(id => GUIUtils.DarkColoredProvinceName(
                                            _model.Provinces[id],
                                            _model.PlayerId.Value))
                            .ToList();

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

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