﻿using System;
using System.Reflection;
using HarmonyLib;

namespace undead_universal_patch.Patches
{
    [HarmonyPatch]
    class NWebSocket
    {
        static string TargetTypeName = "BestHTTP.WebSocket.OHKCAHPNEGE";
        static string Description = "Late 2018 WebSocket (non-signalr) URL rewrite patch";
        static readonly Type targetType = AccessTools.TypeByName(TargetTypeName);

        static bool Prepare()
        {
            if (GenericConfig.IsNameserver.Value) return false;
            if (targetType == null)
            {
                Plugin.Log.LogWarning($"'{Description}' disabled. The type for this patch was not found.");
                return false;
            }

            Plugin.Log.LogInfo($"'{Description}' succeeded validation.");
            return true;
        }

        static MethodBase TargetMethod()
        {
            return AccessTools.Constructor(targetType, [ typeof(Uri) ]);
        }

        [HarmonyPrefix]
        static void Prefix(ref Uri AOHFKOJPKDN)
        {
            if (PhotonConfig.PatchEvent.Value == "SocketConnect" && PhotonConfig.PatchPhotonIds.Value) Photon.Patch();
            AOHFKOJPKDN = PatchUtils.RewriteUrlForSocket(AOHFKOJPKDN);
        }
    }
}
