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

namespace SCE
{
    public class JobDriver_LewdTrainFeed : JobDriver_Train
    {
        protected override IEnumerable<Toil> MakeNewToils()
        {
            SetFinalizerJob(cond => 
                cond == JobCondition.Succeeded 
                    ? JobMaker.MakeJob(SCEJobDefOf.TrainLewd_Lovin, Animal) 
                    : null
            );

            bool skipping = false;
            foreach (var toil in base.MakeNewToils())
            {
                // Base has two sets of toils for feeding, which begin with a toil named 'FeedAnimal'.
                // Since we only want to feed once, we skip everything between the two.
                if (toil.debugName == "FeedToils")
                {
                    skipping = !skipping;
                }
                // Hold off on setting last interact time until sex is finished
                if (skipping || toil.debugName == nameof(Toils_Interpersonal.SetLastInteractTime))
                {
                    continue;
                }

                // Training to be handled by sex job
                if (toil.debugName == nameof(Toils_Interpersonal.TryTrain))
                {
                    break;
                }

                // Replace talk to animal interaction with RJW's one
                if (toil.debugName == "TalkToAnimal")
                {
                    toil.initAction = delegate
                    {
                        pawn.interactions.TryInteractWith(Animal, SexUtility.AnimalSexChat);
                    };
                }
                yield return toil;
            }
        }
    }
}