using System.Collections.Generic;
using System.Linq;
using Verse;
using RimWorld;
using rjw;

namespace SCE
{
    public class RitualStageAction_MarryPawns : RitualStageAction
    {
        public List<string> roleIds;

        public override void Apply(LordJob_Ritual ritual)
        {
            var pawns = roleIds.Select(id => ritual.PawnWithRole(id)).ToList();
            for (int i = 0; i < pawns.Count; i++)
            {
                var firstPawn = pawns[i];
                if (firstPawn == null)
                {
                    Log.Error($"Got null when trying to marry a pawn with role ID: {roleIds[i]}");
                    continue;
                }
                for (int j = 0; j < i; j++)
                {
                    var secondPawn = pawns[j];
                    if (secondPawn == null)
                    {
                        continue;
                    }
                    if (!firstPawn.relations.DirectRelationExists(PawnRelationDefOf.Fiance, secondPawn))
                    {
                        Log.Warning($"Marrying unengaged pawns {firstPawn} and {secondPawn}");
                    }

                    ritual.Ritual.RemoveObligation(ritual.obligation, completed: true);
                    ritual.obligation = null;
                    MarriageCeremonyUtility.Married(firstPawn, secondPawn);
                    Messages.Message("MessageNewlyMarried".Translate(firstPawn.LabelShort, secondPawn.LabelShort, firstPawn.Named("PAWN1"), secondPawn.Named("PAWN2")), MessageTypeDefOf.PositiveEvent, historical: false);
                }
            }
        }

        public override void ExposeData()
        {
            Scribe_Collections.Look(ref roleIds, "roleIds", LookMode.Value);
        }
    }
}