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

namespace SCE
{
    public static class Toils_Sex
    {
        public static Toil StartPartnerJob(Pawn pawn, Pawn partner, JobDef def)
        {
            var toil = new Toil
            {
                defaultCompleteMode = ToilCompleteMode.Instant,
                initAction = delegate
                {
                    Job partnerJob = JobMaker.MakeJob(def, pawn, partner);
                    partner.jobs.StartJob(partnerJob, JobCondition.InterruptForced);
                }
            };
            return toil;
        }

        public static Toil WaitForPartner(Pawn pawn, Pawn partner, int ticks = 90 * GenTicks.TicksPerRealSecond)
        {
            var toil = Toils_General.Wait(ticks);
            toil.tickAction = delegate
            {
                if(pawn.Position.InHorDistOf(partner.Position, 1f))
                {
                    pawn.jobs.curDriver.ReadyForNextToil();
                }
            };
            return toil;
        }

        public static Toil SendPawnToCell(Pawn pawn, IntVec3 cell, JobDef def = null)
        {
            var toil = new Toil
            {
                initAction = delegate
                {
                    pawn.jobs.StopAll();
                    var job = JobMaker.MakeJob(def ?? JobDefOf.GotoMindControlled, cell);
                    pawn.jobs.StartJob(job, JobCondition.InterruptForced);
                },
                defaultCompleteMode = ToilCompleteMode.Instant
            };
            return toil;
        }

        public static Toil SexToil(JobDriver_SexBaseInitiator driver, JobDef partnerJobDef)
        {
            var pawn = driver.pawn;
            var partner = driver.Partner;
			Toil toil = new() {
			    defaultCompleteMode = ToilCompleteMode.Never,
			    socialMode = RandomSocialMode.Off,
			    defaultDuration = driver.duration,
			    handlingFacing = true
            };

			toil.FailOn(() => partner.CurJobDef != partnerJobDef);

			toil.initAction = delegate
			{
				partner.pather.StopDead();
				partner.jobs.curDriver.asleep = false;
				driver.Sexprops.usedCondom = CondomUtility.TryUseCondom(pawn) || CondomUtility.TryUseCondom(partner);
				driver.Start();
			};

			toil.AddPreTickAction(delegate
			{
                if (pawn.IsHashIntervalTick(driver.ticks_between_hearts))
					driver.ThrowMetaIconF(pawn.Position, pawn.Map, FleckDefOf.Heart);
				driver.SexTick(pawn, partner);
				SexUtility.reduce_rest(partner, 1);
				SexUtility.reduce_rest(pawn, 1);
				if (driver.ticks_left <= 0)
					driver.ReadyForNextToil();
			});

			toil.AddFinishAction(delegate
			{
				driver.End();
			});
            
			return toil;
        }

        public static Toil SexToilReceiver(JobDriver_SexBaseReceiverPartiallyUnfucked driver)
        {
            var pawn = driver.pawn;
            var partner = driver.Partner;
            var toil = new Toil(){
                defaultCompleteMode = ToilCompleteMode.Never,
                socialMode = RandomSocialMode.Off,
                handlingFacing = true,
                tickAction = () => {
                    if (pawn.IsHashIntervalTick(driver.ticks_between_hearts))
                        driver.ThrowMetaIconF(pawn.Position, pawn.Map, FleckDefOf.Heart);
                }
            };

            toil.AddEndCondition(delegate
			{
				if (driver.partners.Count <= 0)
				{
                    return JobCondition.Succeeded;
				}
				return JobCondition.Ongoing;
			});

            toil.AddFinishAction(delegate
			{
				if (xxx.is_human(pawn))
					pawn.Drawer.renderer.SetAllGraphicsDirty();
				GlobalTextureAtlasManager.TryMarkPawnFrameSetDirty(pawn);
			});

            toil.FailOn(() => partner.CurJobDef != driver.partnerJob.def);

            return toil;
        }

        public static Toil ProcessSex(JobDriver_SexBaseInitiator driver)
        {
            var toil = new Toil
            {
                initAction = () => SexUtility.ProcessSex(driver.Sexprops)
            };
            return toil;
        }
    }
}