using System;
using System.Collections.Generic;
using System.Linq;
using Net.Eagle0.Eagle.Views;

namespace eagle.Notifications {
    using ProvinceId = Int32;

    public class Notification {
        public string Title { get; }
        public string Text { get; private set; }

        public string LlmId { get; private set; }

        public bool ShouldAppend { get; }
        public List<ProvinceId> ProvinceIds { get; }
        public List<HeroView> DisplayedHeroes { get; }

        private static List<HeroView> ListFromHeroViews(
                HeroView firstHero = null,
                HeroView secondHero = null,
                HeroView thirdHero = null) {
            List<HeroView> paths = new List<HeroView> { firstHero, secondHero, thirdHero };
            paths.RemoveAll(s => s == null);
            return paths.Distinct().ToList();
        }

        public Notification(
                string title,
                string text,
                List<ProvinceId> provinceIds,
                List<HeroView> displayedHeroes,
                bool shouldAppend = true,
                string llmId = null) {
            Title = title;
            Text = text;
            ProvinceIds = provinceIds;

            DisplayedHeroes = displayedHeroes.Distinct().ToList();

            ShouldAppend = shouldAppend;

            LlmId = llmId;
        }

        public Notification(
                string title,
                string text,
                List<ProvinceId> provinceIds,
                HeroView singleDisplayedHero = null,
                bool shouldAppend = true) {
            Title = title;
            Text = text;

            ShouldAppend = shouldAppend;

            ProvinceIds = provinceIds;

            DisplayedHeroes = ListFromHeroViews(singleDisplayedHero);
        }

        public Notification(
                string title,
                string text,
                ProvinceId singleProvinceId,
                List<HeroView> displayedHeroes,
                bool shouldAppend = true)
            : this(title,
                   text,
                   new List<ProvinceId> { singleProvinceId },
                   displayedHeroes.Distinct().ToList(),
                   shouldAppend) {}

        public Notification(
                string title,
                string text,
                ProvinceId singleProvinceId,
                HeroView singleDisplayedHero = null,
                bool shouldAppend = true)
            : this(title,
                   text,
                   new List<ProvinceId> { singleProvinceId },
                   singleDisplayedHero,
                   shouldAppend) {}

        public Notification(string title, string text, bool shouldAppend = false)
            : this(title, text, new List<ProvinceId>(), new List<HeroView>(), shouldAppend) {}

        public static Notification StreamingNotification(
                string title,
                string prefixedText,
                string llmId,
                List<ProvinceId> provinceIds,
                List<HeroView> displayedHeroes) {
            return new Notification(
                    title: title,
                    text: prefixedText,
                    provinceIds: provinceIds,
                    displayedHeroes: displayedHeroes,
                    shouldAppend: false,
                    llmId: llmId);
        }

        public void Append(string text, List<ProvinceId> provinceIds) {
            if (ShouldAppend) {
                Text += "\n" + text;
                ProvinceIds.AddRange(provinceIds);
            } else {
                Text = text;
                ProvinceIds.AddRange(provinceIds);
            }
        }
    }
}