using GadgetCore.API;
using HarmonyLib;
using UnityEngine;

namespace ShipDecorations.Patches
{

  [HarmonyPatch(typeof(CameraScript))]
  [HarmonyPatch("Update")]
  [HarmonyGadget("ShipDecorations")]
  public static class Patch_CameraScript_Update
  {
    [HarmonyPrefix]
    public static bool Prefix(CameraScript __instance, Transform ___t, Transform ___player, bool ___deathCamera, int ___dLimit)
    {
      var offset = -50f;
      var smoothing = 100;

      if (!___player)
        ___t.rotation = Quaternion.EulerAngles(0, 0, 0);
      else
        ___t.rotation = Quaternion.EulerAngles(0.2f, 0.2f, 0);

      if (!___player)
      {
        ___t.position = new Vector3(0f, 0f, -8f);
      }
      else if (___deathCamera)
      {
        ___t.position = Vector3.Lerp(___t.position, new Vector3(___player.position.x, ___player.position.y + 2f, offset), 5f * Time.smoothDeltaTime * smoothing);
      }
      else if (GameScript.dead)
      {
        Vector3 b = new Vector3(___player.position.x, ___player.position.y, offset);
        ___dLimit = -24;
        ___t.position = Vector3.Lerp(___t.position, b, 5f * Time.smoothDeltaTime * smoothing);
      }
      else if (!GameScript.inventoryOpen && !GameScript.pausing)
      {
        if (!GameScript.combatMode)
        {
          if (GameScript.buildMode)
          {
            ___dLimit = 20;
          }
          else
          {
            ___dLimit = 1;
          }
        }
        else if (GameScript.dead)
        {
          ___dLimit = 15;
        }
        else
        {
          ___dLimit = 7;
        }
        Vector3 b2 = new Vector3(___player.position.x - 10f, ___player.position.y + 10f, offset);
        ___t.position = Vector3.Lerp(___t.position, b2, 5f * Time.smoothDeltaTime * smoothing);
      }
      else
      {
        ___t.position = Vector3.Lerp(___t.position, new Vector3(___player.position.x - 10f, ___player.position.y + 10f, offset), 5f * Time.smoothDeltaTime * smoothing);
      }
      return false;
    }
  }
}