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

namespace SCE
{
    public class PreceptComp_Taboo : PreceptComp
    {
        public HistoryEventDef eventDef;

        public float guiltDurationDays;

        [Unsaved(false)]
        private HistoryEvent lastEvent;

        [Unsaved(false)]
        private Dictionary<Pawn, bool> guiltyPawns = new();

        public override void Notify_MemberTookAction(HistoryEvent ev, Precept precept, bool canApplySelfTookThoughts)
        {
            if (ev.def != eventDef)
            {
                return;
            }
            
            var doer = ev.args.GetArg<Pawn>(HistoryEventArgsNames.Doer);
            if (BrokeTaboo(doer, ev, precept))
            {
                if (guiltDurationDays > 0f && precept.ideo == Faction.OfPlayer.ideos.PrimaryIdeo)
                {
                    doer.guilt?.Notify_Guilty((int) (guiltDurationDays * GenDate.TicksPerDay));
                }
            }
        }

        public bool BrokeTaboo(Pawn pawn, HistoryEvent ev, Precept precept)
        {
            if (!ev.Equals(lastEvent))
            {
                lastEvent = ev;
                guiltyPawns.Clear();
            }

            if (guiltyPawns.TryGetValue(pawn, out bool guilty))
            {
                return guilty;
            }

            return guiltyPawns[pawn] = BrokeTabooInt(pawn, ev, precept);
        }


        protected virtual bool BrokeTabooInt(Pawn pawn, HistoryEvent ev, Precept precept) => xxx.is_human(pawn);
    }
}