﻿using HarmonyLib;
using rjw;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;

namespace Rimworld_Animations
{
    [HarmonyPatch(typeof(JobDriver_Sex), "Animate")]
    public class HarmonyPatch_Animate
    {
        public static bool Prefix(ref JobDriver_Sex __instance, ref Pawn pawn, ref Thing target)
        {
			//remove all bumping stuff in animations; keep draw nude code
			__instance.RotatePawns(pawn, __instance.Partner);
			if (target != null)
			{
				Pawn pawn2 = target as Pawn;

				if (pawn2 != null && !__instance.Sexprops.isRapist)
				{
					// if not (pawn has root node and rootnode is animating)
					if (!(pawn2?.Drawer?.renderer?.renderTree?.rootNode is PawnRenderNode rootNode
					&& (rootNode.AnimationWorker is AnimationWorker_KeyframesExtended || rootNode.children.Any(x => x.AnimationWorker is AnimationWorker_KeyframesExtended))))
                    {
						//play bumpin anim
						pawn.Drawer.Notify_MeleeAttackOn(target);
					}

				}

				if (!__instance.isEndytophile)
				{
					SexUtility.DrawNude(pawn, false);
					if (pawn2 != null)
					{
						SexUtility.DrawNude(pawn2, false);
						return false;
					}
				}
			}
			else if (!__instance.isEndytophile)
			{
				SexUtility.DrawNude(pawn, false);
			}

			return false;
        }
    }
}
