using System;
using Net.Eagle0.Eagle.Views;

namespace eagle {
    public sealed class DynamicPlaceholder {
        public DynamicTextPlaceholderName Name { get; }
        public string TextId { get; }
        public string Fallback { get; }

        private DynamicPlaceholder(
                DynamicTextPlaceholderName name,
                string textId,
                string fallback) {
            Name = name;
            TextId = textId;
            Fallback = fallback ?? throw new ArgumentNullException(nameof(fallback));
        }

        private static DynamicPlaceholder
        GeneratedText(DynamicTextPlaceholderName name, string textId, string fallback) {
            return new DynamicPlaceholder(name, textId, fallback);
        }

        public static DynamicPlaceholder StaticText(DynamicTextPlaceholderName name, string text) {
            return GeneratedText(name, null, text);
        }

        public static DynamicPlaceholder
        HeroName(DynamicTextPlaceholderName name, HeroView hero, string fallback) {
            return GeneratedText(name, hero.NameTextId, fallback);
        }

        public static DynamicPlaceholder FactionDisplayName(
                DynamicTextPlaceholderName name,
                FactionView faction) {
            return StaticText(name, faction.Name);
        }

        public static DynamicPlaceholder FactionLeaderName(
                DynamicTextPlaceholderName name,
                FactionView faction,
                IGameModel currentModel,
                string fallback) {
            return HeroName(name, currentModel.Heroes[faction.FactionHeadId], fallback);
        }

        public static DynamicPlaceholder FactionDisplayNameOrLeaderName(
                DynamicTextPlaceholderName name,
                int factionId,
                IGameModel currentModel,
                string leaderFallback) {
            var faction = currentModel.MaybeDestroyedFaction(factionId);
            if (DisplayNames.DisplayFactionName) { return FactionDisplayName(name, faction); }

            return FactionLeaderName(name, faction, currentModel, leaderFallback);
        }
    }
}
