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

namespace ArachnaeSwarm
{
    public class JobDriver_Incubate : JobDriver
    {
        private const TargetIndex EggSacInd = TargetIndex.A;
        private const TargetIndex PawnKindInd = TargetIndex.B;

        public override bool TryMakePreToilReservations(bool errorOnFailed)
        {
            return pawn.Reserve(job.GetTarget(EggSacInd), job, 1, -1, null, errorOnFailed);
        }

        protected override IEnumerable<Toil> MakeNewToils()
        {
            this.FailOnDespawnedNullOrForbidden(EggSacInd);

            yield return Toils_Goto.GotoThing(EggSacInd, PathEndMode.Touch);

            Toil incubate = new Toil();
            incubate.initAction = () =>
            {
                CompSpawnPawnFromList comp = job.GetTarget(EggSacInd).Thing.TryGetComp<CompSpawnPawnFromList>();
                if (comp != null)
                {
                    comp.StartIncubation();
                }
            };
            incubate.defaultCompleteMode = ToilCompleteMode.Instant;
            yield return incubate;
        }
    }
}