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

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

    public abstract class ResolveDiplomacyCommandSelector : PopupPanelController {
        public Action<SelectedCommand> submissionAction;

        public GameObject acceptButton;
        public GameObject rejectButton;
        public GameObject imprisonButton;

        public IGameModel Model { get; set; }

        public int CommandIndex { get; set; }

        public HeroDetailsController heroDetailsController;
        protected AvailableCommand _availableCommand = null;

        protected abstract string PrefixedText(DiplomacyOffer offer);
        protected abstract string TitleText { get; }
        protected abstract IList<DiplomacyOffer> Offers { get; }

        private DiplomacyOffer Offer => Offers[DisplayedIndex];
        private int OfferCount => Offers.Count;
        protected HeroId MessengerHeroId => Offer.MessengerHeroId;
        protected FactionId OriginatingFactionId => Offer.OriginatingFactionId;

        public AvailableCommand AvailableCommand {
            get => _availableCommand;
            set {
                _availableCommand = value;
                if (value == null) {
                    PopupInfos = new List<PopupInfo>();
                } else {
                    var offeringFaction = Model.ActiveFactions[OriginatingFactionId];
                    var offeringFactionHead = Model.Heroes[offeringFaction.FactionHeadId];

                    PopupInfos =
                            Offers.Select(off => new PopupInfo {
                                      titleText = TitleText,
                                      mainText = PrefixedText(off),
                                      mainTextLlmId = off.OfferTextId,
                                      affectedProvinceIds = new List<ProvinceId>(),
                                      displayedHeroes = new List<HeroView> { offeringFactionHead }
                                  }).ToList();
                }

                SetUpPanel();
            }
        }

        public abstract SelectedCommand selectedCommand(DiplomacyOfferStatus resolution);

        public void AcceptButtonClicked() {
            submissionAction(selectedCommand(DiplomacyOfferStatus.Accepted));
            AvailableCommand = null;
            SetUpPanel();
        }

        public void RejectButtonClicked() {
            submissionAction(selectedCommand(DiplomacyOfferStatus.Rejected));
            AvailableCommand = null;
            SetUpPanel();
        }

        public void ImprisonButtonClicked() {
            submissionAction(selectedCommand(DiplomacyOfferStatus.Imprisoned));
            AvailableCommand = null;
            SetUpPanel();
        }

        protected override void SetUpPanel() {
            base.SetUpPanel();

            mapController.OverrideTargetedProvinces = new List<ProvinceId>();

            if (OfferCount > DisplayedIndex) {
                heroDetailsController.SetHero(Model.Heroes[MessengerHeroId], Model);

                mapController.OverrideTargetedProvinces =
                        Model.Provinces.Values
                                .Where(pv => pv.RulingFactionId == OriginatingFactionId)
                                .Select(pv => pv.Id)
                                .ToList();

                if (acceptButton) {
                    acceptButton.SetActive(
                            Offer.EligibleStatuses.Contains(DiplomacyOfferStatus.Accepted));
                }

                if (rejectButton) {
                    rejectButton.SetActive(
                            Offer.EligibleStatuses.Contains(DiplomacyOfferStatus.Rejected));
                }

                if (imprisonButton) {
                    imprisonButton.SetActive(
                            Offer.EligibleStatuses.Contains(DiplomacyOfferStatus.Imprisoned));
                }
            }
        }
    }
}