# Unlock + analytics block + small cheat menu
# Install: run apply_unlock_patch.ps1, or copy into game/
# Open cheat menu in-game with F8
#
# IMPORTANT: never `import renpy` / `import renpy.exports` in store python —
# that rebinds store.renpy away from renpy.exports and breaks renpy.music etc.

init 999 python:
    import sys

    _UNLOCK_CHARS = (
        "Nari", "Marin", "Elira", "Violet", "Luna", "Faye",
        "Ivy", "Ruby", "Rose", "Mimi", "Albeddo",
    )

    def _verify_patreon():
        return True

    def is_patreon_active():
        return True

    def checkIntegrity():
        store.patreon_unlocked = True
        try:
            setBadge(VIP_STATE_ACTIVE)
        except Exception:
            pass

    def aaaa(event_type="game_start"):
        """No-op replacement for the planbakers analytics ping."""
        return None

    def _force_patreon_unlock():
        store.patreon_unlocked = True
        store.patreon_version_mismatch = False
        store.patreon_code_popup = False
        store.patreon_gate_popup = False
        try:
            setBadge(VIP_STATE_ACTIVE)
        except Exception:
            pass

        try:
            h = next(iter(_VALID_PATREON_HASHES))
            persistent.patreon_hash = h
            persistent.patreon_proof = _compute_proof(h, config.version)
            persistent.patreon_version = config.version
            persistent.hashed_patreon_hash = _hash_patreon_value(h)
        except Exception:
            pass

    def _cheat_max_all_stats():
        for character in _UNLOCK_CHARS:
            try:
                cheat_set_stat(character, "affection", 999)
                cheat_set_stat(character, "h", 999)
                cheat_set_stat(character, "life_lesson", 9999)
            except Exception:
                pass
        renpy.notify("Maxed affection / H / life lesson for all characters.")

    def _cheat_unlock_all_lessons():
        for character in _UNLOCK_CHARS:
            try:
                cheat_get_all_lessons(character)
            except Exception:
                pass
        renpy.notify("Unlocked all life lessons.")

    def _cheat_max_money():
        try:
            cheat_set_money(999999)
            renpy.notify("Money set to 999999.")
        except Exception:
            renpy.notify("Could not set money.")

    def _cheat_max_items():
        try:
            cheat_set_all_items_max()
            renpy.notify("All items set to max.")
        except Exception:
            renpy.notify("Could not max items.")

    def _cheat_refill_stamina():
        try:
            store.mui_stamina = store.mui_stamina_max
            renpy.notify("Stamina refilled.")
        except Exception:
            renpy.notify("Could not refill stamina.")

    def _block_analytics_fetch(*args, **kwargs):
        url = ""
        if args:
            url = str(args[0])
        elif "url" in kwargs:
            url = str(kwargs["url"])
        url_l = url.lower()
        if ("planbakers.com" in url_l) or ("game-analytics" in url_l) or ("/analytic/" in url_l):
            result = kwargs.get("result", "bytes")
            if result == "json":
                return {}
            if result == "text":
                return ""
            return b""
        return _original_renpy_fetch(*args, **kwargs)

    # Patch fetch on renpy.exports without rebinding store.renpy
    _renpy_exports = sys.modules["renpy.exports"]
    _original_renpy_fetch = _renpy_exports.fetch
    _renpy_exports.fetch = _block_analytics_fetch

    config.start_callbacks.append(_force_patreon_unlock)
    config.after_load_callbacks.append(_force_patreon_unlock)
    config.overlay_screens.append("patch_cheat_hotkey")

    _force_patreon_unlock()


screen patch_cheat_hotkey():
    key "K_F8" action Show("patch_cheat_menu")


screen patch_cheat_menu():
    modal True
    zorder 200
    key "K_ESCAPE" action Hide("patch_cheat_menu")
    key "K_F8" action Hide("patch_cheat_menu")

    button:
        xysize (config.screen_width, config.screen_height)
        background "#000000aa"
        action Hide("patch_cheat_menu")
        keyboard_focus False

    frame:
        background "#12122af0"
        padding (28, 24)
        xalign 0.5
        yalign 0.5
        xmaximum 420

        vbox:
            spacing 10
            xfill True

            text "Quick Cheats":
                size 22
                color "#eeeef8"
                bold True
                xalign 0.5

            text "VIP is active · analytics blocked · F8 to toggle":
                size 12
                color "#8a9ab8"
                xalign 0.5

            null height 6

            textbutton "Max all character stats":
                xfill True
                action Function(_cheat_max_all_stats)
                text_size 16
                text_color "#eeeef8"
                text_hover_color "#ff90b8"

            textbutton "Unlock all life lessons":
                xfill True
                action Function(_cheat_unlock_all_lessons)
                text_size 16
                text_color "#eeeef8"
                text_hover_color "#ff90b8"

            textbutton "Max money":
                xfill True
                action Function(_cheat_max_money)
                text_size 16
                text_color "#eeeef8"
                text_hover_color "#ff90b8"

            textbutton "Max all items":
                xfill True
                action Function(_cheat_max_items)
                text_size 16
                text_color "#eeeef8"
                text_hover_color "#ff90b8"

            textbutton "Refill stamina":
                xfill True
                action Function(_cheat_refill_stamina)
                text_size 16
                text_color "#eeeef8"
                text_hover_color "#ff90b8"

            null height 8

            textbutton "Close (Esc / F8)":
                xfill True
                action Hide("patch_cheat_menu")
                text_size 15
                text_color "#8a9ab8"
                text_hover_color "#c8d8f0"
                text_xalign 0.5
