using System;
using HarmonyLib;

namespace BlacksmithMaster {
    public class SeekingProjectileManager {
        [HarmonyPatch(typeof(SeekingProjectile), "OnSpawn")]
        class Patch {
            public static void Postfix(SeekingProjectile __instance) {
                if (Main.debug.Value)
                    Console.WriteLine("SeekingProjectile created");

                SetField(
                    __instance, "m_VisionConeAngle",
                    Main.seekingProjectileVisionConeAngleMultiplier.Value * GetField(__instance, "m_VisionConeAngle"));
                SetField(__instance, "m_VisionRange",
                         Main.seekingProjectileVisionRangeMultiplier.Value * GetField(__instance, "m_VisionRange"));
                SetField(__instance, "m_TurnSpeed",
                         Main.seekingProjectileTurningSpeedMultiplier.Value * GetField(__instance, "m_TurnSpeed"));
            }
        }

        private static float GetField(SeekingProjectile seekingProjectile, string field) {
            return Traverse.Create(seekingProjectile).Field(field).GetValue() as float ? ?? 0f;
        }

        private static void SetField(SeekingProjectile seekingProjectile, string field, float value) {
            Traverse.Create(seekingProjectile).Field(field).SetValue(value);
        }
    }
}
