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

namespace Rimworld_Animations
{
    // Head Rotation Code - Textures
    // it's fine to just edit each AppendRequests individually
    // because they all the parms are passed down to each child node recursively
    [HarmonyPatch(typeof(PawnRenderNode), "AppendRequests")]
    public static class HarmonyPatch_PawnRenderNode
    {
        //if rendernodetag is head, update PawnDrawParms so that head, and all children, are rotated for anim
        public static bool Prefix(ref PawnRenderNode __instance, ref PawnDrawParms parms)
        {
            if (__instance.AnimationWorker is AnimationWorker_KeyframesExtended extendedAnimWorker)
            {

                if (parms.Portrait) return true;

                // ADJUST FACING get rotated textures
                // compare the previous tick to the current tick; if the current tick rotation is different, recache
                parms.facing = extendedAnimWorker.facingAtTick(__instance.tree.AnimationTick);


                //INVIS IF ANIM CALLS FOR IT
                //replace maybe?

                //cheaper call now comparing prev tick to cur tick

                //not necessary because of new rendernodeworker hiding props now
                //nvm, keep it because you can hide head and body too, if need be
                return extendedAnimWorker.visibleAtTick(__instance.tree.AnimationTick);

            }

            return true;

        }
    }
    /*
     * no longer needed; taken care of by graphic variants
     * 
    // For changing texture path of thing to variant
    [HarmonyPatch(typeof(PawnRenderNode), "TexPathFor")]
    public static class HarmonyPatch_PawnRenderNode2
    {
        public static void Postfix(ref PawnRenderNode __instance, ref string __result)
        {
            if (__instance.AnimationWorker is AnimationWorker_KeyframesExtended animWorker)
            {
                __result += animWorker.TexPathVariantAtTick(__instance.tree.AnimationTick);
            }
        }
    }

    */
}

1.5/Source/Patches/RimworldPatches/HarmonyPatch_PawnRenderNode.cs