#!/usr/bin/env python3
"""Fix: replace mock classes with lambda-callable factories in _run_site_in_subprocess"""
import re

path = '/home/matthiasberner/Schreibtisch/kodi/xstreamDownloader/mini_app.py'
with open(path) as f:
    c = f.read()

# The old mock classes block in worker()
old = """        class _A:
            def __init__(self, *a, **kw): self._id = a[0] if a else ''
            def getAddonInfo(self, k): return {'id': self._id, 'name': 'xStream', 'version': '1.0.0', 'icon': '', 'profile': '/tmp'}.get(k, '')
            def openSettings(self, *a, **kw): pass
            def getSetting(self, k): return ''
            def setSetting(self, k, v): pass
            def getLocalizedString(self, k): return str(k)

        class _D:
            def ok(self, *a, **kw): pass
            def yesno(self, *a, **kw): return True
            def select(self, *a, **kw): return 0
            def notification(self, *a, **kw): pass
            def browse(self, *a, **kw): return ''

        def _m(n, **a):
            m = types.ModuleType(n)
            for k, v in a.items(): setattr(m, k, v)
            return m

        sys.modules['xbmcaddon'] = _m('xbmcaddon', Addon=_A)
        sys.modules['xbmcgui']   = _m('xbmcgui', Dialog=_D())"""

new = """        def _m(n, **a):
            m = types.ModuleType(n)
            for k, v in a.items(): setattr(m, k, v)
            return m

        def _A(*a, **kw):
            inst = types.SimpleNamespace()
            inst._id = a[0] if a else ''
            inst.getAddonInfo = lambda k: {'id': inst._id, 'name': 'xStream', 'version': '1.0.0', 'icon': '', 'profile': '/tmp'}.get(k, '')
            inst.openSettings = lambda *a, **kw: None
            inst.getSetting = lambda k: ''
            inst.setSetting = lambda k, v: None
            inst.getLocalizedString = lambda k: str(k)
            return inst

        def _D(*a, **kw):
            inst = types.SimpleNamespace()
            inst.ok = lambda *a, **kw: None
            inst.yesno = lambda *a, **kw: True
            inst.select = lambda *a, **kw: 0
            inst.notification = lambda *a, **kw: None
            inst.browse = lambda *a, **kw: ''
            return inst

        def _PD(*a, **kw):
            inst = types.SimpleNamespace()
            inst.create = lambda *a, **k: None
            inst.update = lambda *a, **k: None
            inst.close = lambda: None
            inst.isFinished = lambda: True
            return inst

        sys.modules['xbmcaddon'] = _m('xbmcaddon', Addon=_A)
        sys.modules['xbmcgui']   = _m('xbmcgui',
            Dialog=_D, DialogBusy=_PD,
            WindowDialog=_D, WindowXMLDialog=_D,
            ControlButton=_D, ControlImage=_D, ControlFadeLabel=_D,
            ACTION_MOUSE_LEFT=100, ACTION_PREVIOUS_MENU=10,
            ACTION_NAV_BACK=92,
        )"""

if old in c:
    c = c.replace(old, new)
    open(path, 'w').write(c)
    print("Patched OK")
else:
    print("Pattern not found — already patched or structure changed")
    # Show what's there
    idx = c.find("class _A:")
    if idx >= 0:
        print(c[idx:idx+500])
