package app.revanced.patches.music.layout.premium

import app.revanced.patcher.extensions.addInstruction
import app.revanced.patcher.extensions.addInstructionsWithLabels
import app.revanced.patcher.extensions.getInstruction
import app.revanced.patcher.extensions.replaceInstruction
import app.revanced.patcher.patch.bytecodePatch
import app.revanced.patches.all.misc.resources.addResources
import app.revanced.patches.all.misc.resources.addResourcesPatch
import app.revanced.patches.music.misc.extension.sharedExtensionPatch
import app.revanced.patches.music.misc.settings.PreferenceScreen
import app.revanced.patches.music.misc.settings.settingsPatch
import app.revanced.patches.shared.misc.settings.preference.SwitchPreference
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction

private const val EXTENSION_CLASS_DESCRIPTOR =
    "Lapp/revanced/extension/music/patches/HideGetPremiumPatch;"

@Suppress("unused")
val hideGetMusicPremiumPatch = bytecodePatch(
    name = "Hide 'Get Music Premium'",
    description = "Adds an option to hide the \"Get Music Premium\" label in the settings and account menu.",
) {
    dependsOn(
        sharedExtensionPatch,
        settingsPatch,
        addResourcesPatch,
    )

    compatibleWith(
        "com.google.android.apps.youtube.music"(
            "7.29.52",
            "8.10.52",
            "8.37.56",
            "8.40.54",
        ),
    )

    apply {
        addResources("music", "layout.premium.hideGetPremiumPatch")

        PreferenceScreen.ADS.addPreferences(
            SwitchPreference("revanced_music_hide_get_premium_label"),
        )

        hideGetPremiumMethodMatch.let {
            val insertIndex = it[-1]

            it.method.apply {
                val setVisibilityInstruction = getInstruction<FiveRegisterInstruction>(insertIndex)
                val getPremiumViewRegister = setVisibilityInstruction.registerC
                val visibilityRegister = setVisibilityInstruction.registerD

                replaceInstruction(
                    insertIndex,
                    "const/16 v$visibilityRegister, 0x8",
                )

                addInstruction(
                    insertIndex + 1,
                    "invoke-virtual {v$getPremiumViewRegister, v$visibilityRegister}, " +
                            "Landroid/view/View;->setVisibility(I)V",
                )
            }
        }

        membershipSettingsMethod.addInstructionsWithLabels(
            0,
            """
                invoke-static { }, $EXTENSION_CLASS_DESCRIPTOR->hideGetPremiumLabel()Z
                move-result v0
                if-eqz v0, :show
                const/4 v0, 0x0
                return-object v0
                :show
                nop
            """,
        )
    }
}
