﻿using RimWorld;
using UnityEngine;
using Verse;

namespace Rimworld_Animations { 

	[StaticConstructorOnStartup]
	public static class PawnWoundDrawerExtension
	{
		public static void RenderPawnOverlay(this PawnWoundDrawer pawnWoundDrawer, Vector3 drawLoc, Mesh bodyMesh, Quaternion quat, bool drawNow, PawnOverlayDrawer.OverlayLayer layer, Rot4 pawnRot, bool? overApparel = null, Pawn pawn = null, PawnRenderFlags flags = new PawnRenderFlags())
		{
			if (pawn == null)
			{ return; }

			if (!flags.FlagSet(PawnRenderFlags.Portrait) && layer == PawnOverlayDrawer.OverlayLayer.Head)
			{
				if (CompBodyAnimator.IsAnimating(pawn) && pawn.Drawer.renderer.graphics.headGraphic != null)
				{
					CompBodyAnimator pawnAnimator = pawn.TryGetComp<CompBodyAnimator>();
					pawnRot = pawnAnimator.headFacing;
					quat = Quaternion.AngleAxis(angle: pawnAnimator.headAngle, axis: Vector3.up);
					float y = drawLoc.y;
					drawLoc = pawnAnimator.getPawnHeadPosition() - Quaternion.AngleAxis(pawnAnimator.headAngle, Vector3.up) * pawn.Drawer.renderer.BaseHeadOffsetAt(pawnAnimator.headFacing);
					drawLoc.y = y;
				}
			}

			pawnWoundDrawer.RenderPawnOverlay(drawLoc, bodyMesh, quat, drawNow, layer, pawnRot, overApparel);
		}
	}

}
