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

namespace eagle {
    using ProvinceId = Int32;

    public class ResolveTributeCommandSelector : CommandSelector {
        public TMP_Text factionDescription;
        public TMP_Text goldAmountLabel;
        public TMP_Text foodAmountLabel;
        public TMP_Text incomingHeroCount;
        public TMP_Text incomingTroopsCount;
        public Toggle payToggle;
        public Button nextFactionButton;

        public override AvailableCommand.SealedValueOneofCase CommandType =>
                AvailableCommand.SealedValueOneofCase.ResolveTributeCommand;
        public override string HeaderString => "Tribute Demanded";
        public override string CommitButtonString =>
                payToggle.isOn ? "Commit Pay Tribute" : "Commit Refuse to Pay";
        public override bool EnableCommitButton => !payToggle.isOn || SufficientResources;
        public override string DisabledCommitButtonReason =>
                "You don't have enough resources to pay";

        private List<TributeAndFaction> _demands;
        private TributeAndFaction SelectedDemand => _demands[_demandIndex];
        private bool SufficientResources =>
                ResolveTributeAvailableCommand.AvailableGold >=
                        SelectedDemand.TributeDemanded.Gold &&
                ResolveTributeAvailableCommand.AvailableFood >= SelectedDemand.TributeDemanded.Food;

        private int _demandIndex;

        private ResolveTributeAvailableCommand ResolveTributeAvailableCommand =>
                _availableCommand.ResolveTributeCommand;
        private ProvinceId ActingProvinceId => ResolveTributeAvailableCommand.ActingProvinceId;

        public override SelectedCommand Command => new SelectedCommand {
            ResolveTributeCommand =
                    new ResolveTributeSelectedCommand {
                        DemandingFactionId = SelectedDemand.DemandingFactionId,
                        Paid = payToggle.isOn
                    }
        };

        public void NextButtonClicked() {
            _demandIndex += 1;
            if (_demandIndex >= _demands.Count) { _demandIndex = 0; }
            DisplayDemand();
        }

        protected override void SetUpUI() {
            _demands = new List<TributeAndFaction>(ResolveTributeAvailableCommand.Demands);
            _demandIndex = 0;
            DisplayDemand();
        }

        private void DisplayDemand() {
            nextFactionButton.gameObject.SetActive(_demands.Count > 1);

            var demandingFaction = _model.MaybeDestroyedFaction(SelectedDemand.DemandingFactionId);

            factionDescription.text =
                    $"{DisplayNames.ResolvedFactionString(demandingFaction, _model)} demands tribute!";

            goldAmountLabel.text = SelectedDemand.TributeDemanded.Gold.ToString();
            foodAmountLabel.text = SelectedDemand.TributeDemanded.Food.ToString();

            var armiesForFaction = _model.Provinces[ActingProvinceId].KnownIncomingArmies.Where(
                    ai => ai.FactionId == SelectedDemand.DemandingFactionId);
            incomingHeroCount.text = SelectedDemand.HeroCount.ToString();
            incomingTroopsCount.text = SelectedDemand.TroopCount.ToString();

            payToggle.isOn = false;
            payToggle.enabled = SufficientResources;
        }
    }
}