package app.revanced.patches.reddit.customclients.sync.syncforreddit.fix.redgifs

import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.extensions.getInstruction
import app.revanced.patcher.extensions.replaceInstruction
import app.revanced.patches.reddit.customclients.INSTALL_NEW_CLIENT_METHOD
import app.revanced.patches.reddit.customclients.fixRedgifsApiPatch
import app.revanced.patches.reddit.customclients.sync.syncforreddit.extension.sharedExtensionPatch
import app.revanced.util.getReference
import app.revanced.util.indexOfFirstInstructionOrThrow
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
import com.android.tools.smali.dexlib2.iface.reference.MethodReference

internal const val EXTENSION_CLASS_DESCRIPTOR = "Lapp/revanced/extension/syncforreddit/FixRedgifsApiPatch;"

@Suppress("unused")
val fixRedgifsApi = fixRedgifsApiPatch(
    extensionPatch = sharedExtensionPatch
) {
    compatibleWith(
        "com.laurencedawson.reddit_sync",
        "com.laurencedawson.reddit_sync.pro",
        "com.laurencedawson.reddit_sync.dev",
    )

    apply {
        // region Patch Redgifs OkHttp3 client.

        val index = createOkHttpClientMethod.indexOfFirstInstructionOrThrow {
            val reference = getReference<MethodReference>()
            reference?.name == "build" && reference.definingClass == $$"Lokhttp3/OkHttpClient$Builder;"
        }
        val register = createOkHttpClientMethod.getInstruction<FiveRegisterInstruction>(index).registerC

        createOkHttpClientMethod.replaceInstruction(
            index,
            """
            invoke-static { v$register }, $EXTENSION_CLASS_DESCRIPTOR->$INSTALL_NEW_CLIENT_METHOD
            """
        )

        getDefaultUserAgentMethod.addInstructions(
            0,
            """
            invoke-static { }, $getOriginalUserAgentMethod
            move-result-object v0
            return-object v0
            """
        )

        // endregion
    }
}
