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

using static RimWorld.HistoryEventArgsNames;
using static SCE.SCEHistoryEventArgNames;

namespace SCE
{
    public static class EventUtility
    {
        public static IEnumerable<HistoryEvent> GetSexEventsFor(Pawn doer, SexProps props)
        {
            HistoryEvent MakeSexEvent(HistoryEventDef def, params NamedArgument[] args)
            {
                var result = new HistoryEvent(def, doer.Named(Doer), props.Named(Props));
                result.args.Add(args);
                return result;
            }

            bool isInitiator = doer == props.pawn;
            Pawn partner = isInitiator ? props.partner : props.pawn;

            if (isInitiator && props.isWhoring)
            {
                // yield return MakeSexEvent(SCEEventDefOf.SoldBody);
            }

            if (partner == null)
            {
                yield break;
            }

            if (isInitiator || !props.isRape)
            {
                if (doer.GetRelations(partner).Any(rel => rel.familyByBloodRelation))
                {
                    yield return MakeSexEvent(SCEEventDefOf.CommittedIncest);
                }

                if (xxx.is_animal(partner))
                {
                    yield return MakeSexEvent(SCEEventDefOf.CommittedBestiality);
                }
            }

            if (isInitiator && props.isRape)
            {
                yield return MakeSexEvent(SCEEventDefOf.RapedSomeone, partner.Named(Victim));
            }         
		}

        public static bool Notify_MemberHadSex(this Ideo ideo, HistoryEvent ev)
        {
            bool gaveThought = false;
            
            foreach (var precept in ideo.PreceptsListForReading)
            {
                foreach (var comp in precept.def.comps.OfType<PreceptComp_SexMemory>())
                {
                    if (comp.TryGiveSexMemory(ev, precept) && comp.allowStandardSexMemories)
                    {
                        gaveThought = true;
                    }
                }
            }

            return gaveThought;
        }
    }
}