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

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

    public class AlmsCommandSelector : CommandSelector {
        public HeroDropdownController heroDropdownController;
        public Slider foodSlider;
        public TMP_Text foodGivenLabel;

        private AlmsAvailableCommand AlmsCommand => _availableCommand.AlmsCommand;
        private ProvinceId ActingProvinceId => AlmsCommand.ActingProvinceId;
        private ProvinceView ActingProvince => _model.Provinces[ActingProvinceId];

        private HeroId SelectedHeroId => heroDropdownController.SelectedHeroId;
        private int AvailableFoodAmount => AlmsCommand.FoodAvailable;

        private int SelectedFoodAmount => (int)foodSlider.value;

        public override AvailableCommand.SealedValueOneofCase CommandType =>
                AvailableCommand.SealedValueOneofCase.AlmsCommand;
        public override string HeaderString => "Give Alms";
        public override string CommitButtonString => "Commit Alms";

        public override bool EnableCommitButton => SelectedFoodAmount > 0;
        public override string DisabledCommitButtonReason => "Must include some food";

        public override bool WarnOnCommitButton => ActingProvince.FullInfo.Support.Stat >= 100;
        public override string CommitWarningText =>
                $"{ActingProvince.Name} already has maximum support!";

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

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

            foodSlider.maxValue = AvailableFoodAmount;
            foodSlider.value = 0;
            foodGivenLabel.text = "0";
        }

        public void SelectedProvinceChanged(int newValue) {
            foodSlider.value = Math.Min(foodSlider.value, AvailableFoodAmount);
            foodSlider.maxValue = AvailableFoodAmount;
        }

        public void PlusClicked() {
            foodSlider.value =
                    Math.Min(foodSlider.value + KeyModifiedAmount.Amount(), AvailableFoodAmount);
        }

        public void MinusClicked() {
            foodSlider.value = Math.Max(foodSlider.value - KeyModifiedAmount.Amount(), 0);
        }

        public void FoodSliderChanged(float newValue) {
            foodGivenLabel.text = ((int)newValue).ToString();
        }

        public override SelectedCommand Command => new SelectedCommand {
            AlmsCommand =
                    new AlmsSelectedCommand {
                        Amount = SelectedFoodAmount,
                        ActingHeroId = SelectedHeroId
                    }
        };
    }
}
