public static bool CheckLoyalty(Clan clan, Kingdom kingdom = null)
        {
            if (!SettingsHelper.SubSystemEnabled(SubSystemType.EnsuredLoyalty, clan))
            {
                return(false);
            }

            int DaysWithKingdom = (int)(CampaignTime.Now - clan.LastFactionChangeTime).ToDays;

            if
            (
                (clan.IsUnderMercenaryService && DaysWithKingdom <= Settings.Instance.MinorFactionServicePeriod) ||
                (!clan.IsUnderMercenaryService && clan.Kingdom != null && DaysWithKingdom <= (clan.IsMinorFaction ? Settings.Instance.MinorFactionOathPeriod : Settings.Instance.FactionOathPeriod))
            )
            {
                return(true);
            }

            if (Settings.Instance.UseRelationForEnsuredLoyalty && !clan.IsUnderMercenaryService)
            {
                if (!(clan.Leader.GetRelation(clan.Kingdom.Ruler) >= GetRelationThreshold(clan, kingdom)))
                {
                    return(false);
                }
                else
                if (Settings.Instance.UseWithholdPrice)
                {
                    LoyaltyCostManager costManager = new LoyaltyCostManager(clan, kingdom);
                    if (clan.Kingdom.RulingClan == Clan.PlayerClan)
                    {
                        if (costManager.WithholdCost != null)
                        {
                            if (!(clan.Kingdom.RulingClan.Influence > costManager.WithholdCost.InfluenceCost) || !(clan.Kingdom.Ruler.Gold > costManager.WithholdCost.GoldCost))
                            {
                                return(false);
                            }
                            costManager.AwaitPlayerDecision();
                        }
                        return(true);
                    }
                    else
                    {
                        return(costManager.GetAIWithholdDecision());
                    }
                }
                else
                {
                    return(true);
                }
            }
            else
            {
                return(false);
            }
        }
        private static bool SetDebugResult(ELState state, TextObject DebugTextObject, Clan?clan = null, Kingdom?kingdom = null, int DaysWithKingdom = 0, int RequiredDays = 0)
        {
            switch (state)
            {
            case ELState.SystemDisabled:
                DebugTextObject.SetTextVariable("LOYALTY_CHECK_RESULT", ResultFalse);
                DebugTextObject.SetTextVariable("REASON", ReasonIsNotEnabled);
                return(false);

            case ELState.FactionOutOfScope:
                DebugTextObject.SetTextVariable("LOYALTY_CHECK_RESULT", ResultFalse);
                DebugTextObject.SetTextVariable("REASON", ReasonOutOfScope);
                return(false);

            case ELState.UnderRequiredService:
                TextObject ReasonPeriod = new TextObject(ReasonServicePeriod);
                SetNumericVariable(ReasonPeriod, "DAYS_UNDER_SERVICE", DaysWithKingdom);
                SetNumericVariable(ReasonPeriod, "REQUIRED_DAYS_UNDER_SERVICE", RequiredDays);
                DebugTextObject.SetTextVariable("LOYALTY_CHECK_RESULT", ResultTrue);
                DebugTextObject.SetTextVariable("REASON", ReasonPeriod.ToString());
                return(true);

            case ELState.UnaffectedByRelations:
                DebugTextObject.SetTextVariable("LOYALTY_CHECK_RESULT", ResultFalse);
                TextObject ReasonDisabled = new TextObject(ReasonRelationDisabled);
                SetEntityProperties(ReasonDisabled, "LEAVING_CLAN", clan, true);
                DebugTextObject.SetTextVariable("REASON", ReasonDisabled);
                return(false);

            case ELState.AffectedByRelations:
                int  CurrentRelation           = clan !.Leader.GetRelation(clan.Kingdom.Ruler);
                int  RequiredRelation          = GetRelationThreshold(clan, kingdom);
                bool RelationCheckResult       = CurrentRelation >= RequiredRelation;
                LoyaltyCostManager costManager = new LoyaltyCostManager(clan, kingdom);
                bool       HaveResources       = clan.Kingdom.RulingClan.Influence > (costManager.WithholdCost?.InfluenceCost ?? 0) && clan.Kingdom.Ruler.Gold > (costManager.WithholdCost?.GoldCost ?? 0);
                bool       ShouldPay           = Settings.Instance !.UseWithholdPrice && Settings.Instance.WithholdToleranceLimit * 1000000 < costManager.BarterableSum;
                TextObject WithholdPrice       = new TextObject(HaveResources ? LeaderHasResources : LeaderHasNoResources);
                SetEntityProperties(WithholdPrice, "LEAVING_CLAN", clan, true);
                TextObject ReasonRelation = new TextObject(ReasonRelationEnabled);
                ReasonRelation.SetTextVariable("CHECK_RESULT", RelationCheckResult ? RelationHigh : RelationLow);
                SetEntityProperties(ReasonRelation, "LEAVING_CLAN", clan, true);
                SetNumericVariable(ReasonRelation, "CURRENT_RELATION", CurrentRelation);
                SetNumericVariable(ReasonRelation, "REQUIRED_RELATION", RequiredRelation);
                ReasonRelation.SetTextVariable("WITHHOLD_PRICE_INFO", RelationCheckResult && ShouldPay ? WithholdPrice : TextObject.Empty);
                DebugTextObject.SetTextVariable("LOYALTY_CHECK_RESULT", RelationCheckResult ? (ShouldPay ? (HaveResources ? ResultDepends : ResultFalse) : ResultTrue) : ResultFalse);
                DebugTextObject.SetTextVariable("REASON", ReasonRelation.ToString());
                return(RelationCheckResult && (!ShouldPay || HaveResources));

            default:
                return(false);
            }
        }