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

namespace Rimworld_Animations
{
    [HarmonyPatch(typeof(Dialog_DebugRenderTree), "RightRect")]
    public static class HarmonyPatch_Dialog_DebugRenderTree
    {

        static MethodInfo replaceFloatRangeMethod = SymbolExtensions.GetMethodInfo(() => HarmonyPatch_Dialog_DebugRenderTree.ReplaceFloatValueRange());

        public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
        {
            var codes = new List<CodeInstruction>(instructions);

            for (int i = 0; i < codes.Count; i++)
            {
                //increase granularity of x and z sliders to be 0.01 instead
                if (codes[i].opcode == OpCodes.Ldc_R4 && (float)codes[i].operand == 0.05f)
                {
                    codes[i].operand = 0.001f;
                    codes[i - 8].opcode = OpCodes.Call;
                    codes[i - 8].operand = replaceFloatRangeMethod;

                }

            }

            return codes.AsEnumerable();

        }

        public static FloatRange ReplaceFloatValueRange()
        {
            return new FloatRange(-RJWAnimationSettings.floatRangeInRenderTreeMenu, RJWAnimationSettings.floatRangeInRenderTreeMenu);
        }    

    }
}
