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

namespace eagle {
    using static ControlWeatherAvailableCommand.Types;
    using HeroId = Int32;
    using ProvinceId = Int32;

    public class ControlWeatherCommandSelector : CommandSelector {
        public HeroDropdownController heroDropdownController;
        public TMP_Dropdown targetDropdown;
        public TMP_Dropdown actionDropdown;

        public override AvailableCommand.SealedValueOneofCase CommandType =>
                AvailableCommand.SealedValueOneofCase.ControlWeatherAvailableCommand;
        public override string HeaderString => "Control Weather";
        public override string CommitButtonString =>
                $"Commit {DisplayNames.ControlWeatherTypeName(SelectedOption.ControlWeatherTypes[actionDropdown.value])}";

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

        private HeroId SelectedHeroId => heroDropdownController.SelectedHeroId;

        private ControlWeatherAvailableCommand ControlWeatherAvailableCommand =>
                _availableCommand.ControlWeatherAvailableCommand;

        private TargetProvinceOptions SelectedOption =>
                ControlWeatherAvailableCommand.Options[targetDropdown.value];
        private ControlWeatherType SelectedType =>
                SelectedOption.ControlWeatherTypes[actionDropdown.value];

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

        public override SelectedCommand Command => new SelectedCommand {
            ControlWeatherSelectedCommand =
                    new ControlWeatherSelectedCommand {
                        SelectedProvinceId = SelectedOption.ProvinceId,
                        SelectedType = SelectedType,
                        ActingHeroId = SelectedHeroId
                    }
        };

        public void SetOptions() {
            var availableTypes = SelectedOption.ControlWeatherTypes;
            actionDropdown.ClearOptions();
            actionDropdown.AddOptions(
                    availableTypes.Select(tp => DisplayNames.ControlWeatherTypeName(tp)).ToList());
        }

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

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

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

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