#!/usr/bin/env python3
"""A clean, isolated Kodi for testing the first install of the addon (docs/plan/onboarding/000-plan.md).

Never the workspace Kodi: AGENTS.md forbids a real install, update or first run there. This Kodi has a profile of its own
(HOME=<root>/home), its own log and its own JSON-RPC port, and it is deleted after every test (`down`).

The addon comes from the workspace as it is now, unpublished: build_zip.sh builds the release ZIP and a local repository
on 127.0.0.1 serves it with a test copy of repository.milionar. Kodi then installs it the way a user does: the repository
first, then InstallAddon(plugin.video.milionar) with Kodi's own dependency resolution.

Commands:
  up [--published]           build, serve the repository, start the clean Kodi in Czech with the repository (no addon)
  install                    InstallAddon(plugin.video.milionar) from the repository, answer Kodi's Yes
  key                        TMDb key from ~/.milionar-test/keys.json into the test addon's settings (never printed)
  typekey                    the same key typed into Kodi's open keyboard and confirmed, from inside Kodi (never printed)
  expect LABEL VALUE         wait until an info label has this value (e.g. Window.Property(ob.step) tmdb)
  click ID                   click control ID of the top window (no navigation guesswork)
  shot NAME                  screenshot -> <root>/shots/NAME.png (1920x1080)
  keys Down Down Select ...  Input.* actions (Up Down Left Right Select Back Home ContextMenu Info), text:"..." sends text
  builtin 'ActivateWindow(Home)'   run a Kodi builtin in the test Kodi
  rpc Method '{"params"}'    any JSON-RPC call
  log [--all]                log lines since the last `log` (or all)
  info Label ...             info labels
  restart                    quit the test Kodi and start it again on the same profile (a restart of Kodi)
  try [--published]          for Standa to try: up, install, the TMDb key entered in the guide, then hands off
                             (the guide waits on its TMDb step, key verified); nothing else is pressed until `down`
  down                       quit the test Kodi, stop the repository, delete <root>

The root defaults to $MILIONAR_FRESH_ROOT or <tmp>/milionar-fresh.
"""
import argparse
import json
import os
import shutil
import signal
import socket
import subprocess
import sys
import tempfile
import time
import xml.etree.ElementTree as ET
import zipfile

HERE = os.path.dirname(os.path.abspath(__file__))
WORKSPACE = os.path.abspath(os.path.join(HERE, '..', '..'))
ADDON_ID = 'plugin.video.milionar'
REPO_ID = 'repository.milionar'
HELPER_ID = 'script.milionar.harness'
KODI = '/Applications/Kodi.app/Contents/MacOS/Kodi'
REAL_PROFILE = os.path.realpath(os.path.expanduser('~/Library/Application Support/Kodi'))
KEYS = os.path.expanduser('~/.milionar-test/keys.json')
PUBLISHED = 'https://hosting.pilsfree.net/chudy/kodi/repo/'
CZECH = 'resource.language.cs_cz'
RPC_PORT = 9191                                  # the workspace Kodi keeps 9090
REPO_PORT = 8765

ROOT = os.path.realpath(os.environ.get('MILIONAR_FRESH_ROOT') or os.path.join(tempfile.gettempdir(), 'milionar-fresh'))
HOME = os.path.join(ROOT, 'home')
PROFILE = os.path.join(HOME, 'Library', 'Application Support', 'Kodi')
LOG = os.path.join(HOME, 'Library', 'Logs', 'kodi.log')
SHOTS = os.path.join(ROOT, 'shots')
STATE = os.path.join(ROOT, 'state.json')


def _guard():
    if ROOT == REAL_PROFILE or ROOT.startswith(REAL_PROFILE + os.sep) or REAL_PROFILE.startswith(ROOT + os.sep):
        raise SystemExit('refusing: {} touches the workspace Kodi profile'.format(ROOT))


def _state(**update):
    data = {}
    if os.path.exists(STATE):
        with open(STATE) as handle:
            data = json.load(handle)
    if update:
        data.update(update)
        with open(STATE, 'w') as handle:
            json.dump(data, handle)
    return data


# --------------------------------------------------------------------------- JSON-RPC

def rpc(method, params=None, timeout=30):
    sock = socket.create_connection(('127.0.0.1', RPC_PORT), timeout=timeout)
    try:
        sock.sendall(json.dumps({'jsonrpc': '2.0', 'id': 1, 'method': method, 'params': params or {}}).encode())
        buffer, decoder = '', json.JSONDecoder()
        while True:
            chunk = sock.recv(1 << 16)
            if not chunk:
                raise OSError('connection closed')
            buffer += chunk.decode('utf-8', 'replace')
            while buffer.strip():
                try:
                    obj, end = decoder.raw_decode(buffer.lstrip())
                except ValueError:
                    break
                buffer = buffer.lstrip()[end:]
                if obj.get('id') == 1:
                    return obj
    finally:
        sock.close()


def _rpc_up():
    try:
        return 'result' in rpc('JSONRPC.Ping', timeout=2)
    except OSError:
        return False


def _wait(ready, seconds, what):
    deadline = time.monotonic() + seconds
    while time.monotonic() < deadline:
        if ready():
            return
        time.sleep(0.5)
    raise SystemExit('timeout: ' + what)


def builtin(command):
    """Kodi's JSON-RPC has no builtins: the helper script runs one."""
    answer = rpc('Addons.ExecuteAddon', {'addonid': HELPER_ID, 'params': ['builtin', command], 'wait': False})
    if 'error' in answer:
        raise SystemExit(json.dumps(answer['error']))


# --------------------------------------------------------------------------- setup

def _build_repo(published):
    repo = os.path.join(ROOT, 'repo')
    os.makedirs(os.path.join(repo, ADDON_ID))
    if published:
        url = PUBLISHED
    else:
        out = subprocess.run(['sh', os.path.join(WORKSPACE, 'scripts', 'build_zip.sh'), os.path.join(repo, ADDON_ID)],
                             check=True, stdout=subprocess.PIPE, text=True).stdout.strip().splitlines()[-1]
        with zipfile.ZipFile(out) as archive:
            plugin_xml = archive.read(ADDON_ID + '/addon.xml').decode('utf-8')
        url = 'http://127.0.0.1:{}/'.format(REPO_PORT)
    # the test copy of repository.milionar: same id, so the addon's own update check reads this repository
    source = os.path.join(WORKSPACE, 'repository', REPO_ID)
    tree = ET.parse(os.path.join(source, 'addon.xml'))
    folder = tree.getroot().find(".//extension[@point='xbmc.addon.repository']/dir")
    folder.find('info').text = url + 'addons.xml'
    folder.find('checksum').text = url + 'addons.xml.md5'
    folder.find('datadir').text = url
    repo_addon = os.path.join(ROOT, 'repo-addon', REPO_ID)
    os.makedirs(repo_addon)
    shutil.copy(os.path.join(source, 'icon.png'), repo_addon)
    tree.write(os.path.join(repo_addon, 'addon.xml'), encoding='UTF-8', xml_declaration=True)
    if not published:
        import hashlib
        import re
        strip = lambda text: re.sub(r'^\s*<\?xml[^>]*\?>\s*', '', text).strip()
        with open(os.path.join(repo_addon, 'addon.xml'), encoding='utf-8') as handle:
            repo_xml = handle.read()
        addons = '<?xml version="1.0" encoding="UTF-8"?>\n<addons>\n{}\n{}\n</addons>\n'.format(
            strip(plugin_xml), strip(repo_xml))
        with open(os.path.join(repo, 'addons.xml'), 'w', encoding='utf-8') as handle:
            handle.write(addons)
        with open(os.path.join(repo, 'addons.xml.md5'), 'w') as handle:
            handle.write(hashlib.md5(addons.encode('utf-8')).hexdigest())
        server = subprocess.Popen([sys.executable, '-m', 'http.server', str(REPO_PORT), '--bind', '127.0.0.1',
                                   '--directory', repo], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        _state(server=server.pid)
    return repo_addon


def _helper():
    folder = os.path.join(PROFILE, 'addons', HELPER_ID)
    os.makedirs(folder)
    with open(os.path.join(folder, 'addon.xml'), 'w', encoding='utf-8') as handle:
        handle.write('<?xml version="1.0" encoding="UTF-8"?>\n<addon id="{}" name="Harness" version="1.0.0" '
                     'provider-name="test"><requires><import addon="xbmc.python" version="3.0.0"/>'
                     '<import addon="script.module.requests" version="2.22.0" optional="true"/></requires>'
                     '<extension point="xbmc.python.script" library="default.py"/>'
                     '<extension point="xbmc.addon.metadata"><platform>all</platform></extension></addon>\n'
                     .format(HELPER_ID))
    with open(os.path.join(folder, 'default.py'), 'w', encoding='utf-8') as handle:
        handle.write('''import json, sys, xbmc, xbmcaddon
step = sys.argv[1] if len(sys.argv) > 1 else ''
if step == 'builtin':
    xbmc.executebuiltin(sys.argv[2])
elif step == 'setting':                  # a non-secret setting of the test addon (e.g. account_server)
    xbmcaddon.Addon('plugin.video.milionar').setSetting(sys.argv[2], sys.argv[3])
elif step == 'key':                      # the value never leaves this process
    with open(sys.argv[2]) as handle:
        key = json.load(handle).get('tmdb_api_key', '')
    xbmcaddon.Addon('plugin.video.milionar').setSetting('tmdb_api_key', key)
    xbmc.log('[Harness] tmdb key written: {}'.format(bool(key)), xbmc.LOGINFO)
elif step == 'typekey':                  # the user's way: Kodi's keyboard; the value never leaves this process
    with open(sys.argv[2]) as handle:
        key = json.load(handle).get('tmdb_api_key', '')
    monitor = xbmc.Monitor()
    for _ in range(100):
        if xbmc.getCondVisibility('Window.IsActive(virtualkeyboard)'):
            break
        monitor.waitForAbort(0.1)
    xbmc.executeJSONRPC(json.dumps({'jsonrpc': '2.0', 'id': 1, 'method': 'Input.SendText',
                                    'params': {'text': key, 'done': True}}))
    xbmc.log('[Harness] tmdb key typed: {}'.format(bool(key)), xbmc.LOGINFO)
elif step == 'quickbuild':               # the guide's first Pro tebe alone, for its timing (nothing is saved)
    import time, xbmcvfs
    sys.path.insert(0, xbmcvfs.translatePath('special://home/addons/plugin.video.milionar'))
    from wcs.foryou import engine
    started = time.time()
    engine.build(log=lambda m: xbmc.log('[Harness] ' + m, xbmc.LOGINFO), verify=False, quick=True)
    xbmc.log('[Harness] quick build {:.1f} s'.format(time.time() - started), xbmc.LOGINFO)
''')


def cmd_up(args):
    _guard()
    if os.path.exists(ROOT):
        raise SystemExit('{} exists: run `down` first'.format(ROOT))
    if _rpc_up():
        raise SystemExit('port {} is taken'.format(RPC_PORT))
    os.makedirs(os.path.join(PROFILE, 'userdata'))
    os.makedirs(os.path.join(PROFILE, 'addons'))
    os.makedirs(SHOTS)
    shutil.copytree(os.path.join(REAL_PROFILE, 'addons', CZECH), os.path.join(PROFILE, 'addons', CZECH))   # read only
    with open(os.path.join(PROFILE, 'userdata', 'advancedsettings.xml'), 'w') as handle:
        handle.write('<advancedsettings><jsonrpc><tcpport>{}</tcpport></jsonrpc></advancedsettings>\n'.format(RPC_PORT))
    repo_addon = _build_repo(args.published)
    shutil.copytree(repo_addon, os.path.join(PROFILE, 'addons', REPO_ID))
    _helper()
    env = dict(os.environ, HOME=HOME)
    kodi = subprocess.Popen([KODI], env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, cwd=ROOT)
    _state(kodi=kodi.pid, log_offset=0)
    _wait(_rpc_up, 60, 'JSON-RPC of the test Kodi')
    # a user installs the repository ZIP (enabled at once); files put there by hand start disabled
    for addon in (REPO_ID, HELPER_ID, CZECH):
        rpc('Addons.SetAddonEnabled', {'addonid': addon, 'enabled': True})
    rpc('Settings.SetSettingValue', {'setting': 'debug.screenshotpath', 'value': SHOTS + '/'})
    # Czech like our users' Kodi, kept in this profile's guisettings.xml (a restart keeps it)
    rpc('Settings.SetSettingValue', {'setting': 'input.enablemouse', 'value': False})   # the Mac's cursor takes no focus
    rpc('Settings.SetSettingValue', {'setting': 'locale.country', 'value': 'CZ'})
    rpc('Settings.SetSettingValue', {'setting': 'locale.language', 'value': CZECH})
    _wait(lambda: _info('System.Language') == 'Czech', 30, 'Kodi in Czech')
    print('test Kodi up: pid {}, profile {}'.format(kodi.pid, PROFILE))


SENTINEL = 'script.module.requests'               # a dependency only Kodi's own repository has


def _repositories_ready(timeout=240):
    """Kodi fetches its repositories a while after start; installing before that fails on the dependencies ("Závislost
    na script.module.requests nemohla být vyřešena"). Addons.GetAddons with installed="all" lists the installed addons
    and what the repositories offer (GetInstallableAddons, xbmc/interfaces/json-rpc/AddonsOperations.cpp)."""
    deadline = time.monotonic() + timeout
    while time.monotonic() < deadline:
        offered = {a.get('addonid') for a in (rpc('Addons.GetAddons', {'installed': 'all'}).get('result') or {})
                   .get('addons') or []}
        if ADDON_ID in offered and SENTINEL in offered:
            return
        time.sleep(2)
    raise SystemExit('the repositories did not load in {} s'.format(timeout))


def cmd_install(args):
    _repositories_ready()
    builtin('InstallAddon({})'.format(ADDON_ID))
    deadline = time.monotonic() + 15                   # Kodi asks "Do you want to install?"
    while time.monotonic() < deadline:
        if rpc('XBMC.GetInfoBooleans', {'booleans': ['Window.IsActive(yesnodialog)']})['result'].get(
                'Window.IsActive(yesnodialog)'):
            builtin('SendClick(yesnodialog,11)')       # Yes; the focus starts on No
            print('confirmed the install')
            return
        time.sleep(0.3)
    print('no confirmation dialog seen')


def _info(label):
    try:
        return rpc('XBMC.GetInfoLabels', {'labels': [label]}, timeout=5)['result'].get(label, '')
    except (OSError, KeyError):
        return ''


def cmd_typekey(args):
    if not os.path.isfile(KEYS):
        raise SystemExit('missing ' + KEYS)
    rpc('Addons.ExecuteAddon', {'addonid': HELPER_ID, 'params': ['typekey', KEYS], 'wait': False})
    print('key typed into Kodi\'s keyboard (value not shown)')


def expect(label, wanted, timeout):
    deadline = time.monotonic() + timeout
    value = None
    while time.monotonic() < deadline:
        value = _info(label)
        if value == wanted:
            print('{} = {}'.format(label, value))
            return
        time.sleep(0.2)
    raise SystemExit('expected {} = {}, got {!r}'.format(label, wanted, value))


def cmd_expect(args):
    expect(args.label, args.value, args.timeout)


STEP = 'Window.Property(ob.step)'                  # the guide's step (wcs/onboarding/wizard.py)


def installed_version():
    answer = rpc('Addons.GetAddonDetails', {'addonid': ADDON_ID, 'properties': ['version']})
    return ((answer.get('result') or {}).get('addon') or {}).get('version', '')


def handover(harness, args):
    """`try` of this harness or of fresh_tv.py (`harness`, its own commands first): a clean Kodi with Milionář, the
    TMDb key entered in the guide's own step as the household enters it, then back on that step, key verified, and the
    Kodi is Standa's. Nothing more is pressed; `down` deletes it when he is done."""
    def run(name):
        (getattr(harness, 'cmd_' + name, None) or globals()['cmd_' + name])(args)
    run('up')
    run('install')
    _wait(installed_version, 180, 'Milionář installed')
    expect(STEP, 'welcome', 240)
    if hasattr(harness, 'after_install'):          # the TV: the workspace's version over the published one
        harness.after_install(args)
        expect(STEP, 'welcome', 240)
    builtin('SendClick(11)')                       # Začít
    expect(STEP, 'tmdb', 30)
    builtin('SendClick(11)')                       # Zadat klíč: Kodi's keyboard
    run('typekey')
    expect(STEP, 'webshare', 60)                   # the guide checked the key and went on
    rpc('Input.Back')                              # back on the TMDb step: "key verified", Pokračovat
    expect(STEP, 'tmdb', 30)
    print('ready: Milionář {}, the guide on its TMDb step with the key verified; `down` deletes it'.format(
        installed_version()))


def cmd_try(args):
    handover(sys.modules[__name__], args)


def cmd_click(args):
    builtin('SendClick({})'.format(args.id))


def cmd_setting(args):
    rpc('Addons.ExecuteAddon', {'addonid': HELPER_ID, 'params': ['setting', args.id, args.value], 'wait': False})
    print('setting {} written'.format(args.id))


def cmd_key(args):
    if not os.path.isfile(KEYS):
        raise SystemExit('missing ' + KEYS)
    rpc('Addons.ExecuteAddon', {'addonid': HELPER_ID, 'params': ['key', KEYS], 'wait': False})
    print('key handed to the test Kodi (value not shown)')


def cmd_shot(args):
    for name in os.listdir(SHOTS):
        if name.startswith('screenshot'):
            os.remove(os.path.join(SHOTS, name))
    rpc('Input.ExecuteAction', {'action': 'screenshot'})
    deadline = time.monotonic() + 10
    while time.monotonic() < deadline:
        found = [name for name in os.listdir(SHOTS) if name.startswith('screenshot')]
        if found:
            time.sleep(0.4)
            source, target = os.path.join(SHOTS, found[0]), os.path.join(SHOTS, args.name + '.png')
            subprocess.run(['sips', '-z', '1080', '1920', source, '--out', target], check=True,
                           stdout=subprocess.DEVNULL)
            os.remove(source)
            print(target)
            return
        time.sleep(0.2)
    raise SystemExit('no screenshot')


def cmd_keys(args):
    for key in args.keys:
        if key.startswith('text:'):
            rpc('Input.SendText', {'text': key[5:], 'done': False})
        else:
            rpc('Input.' + key)
        time.sleep(args.gap)


def cmd_builtin(args):
    builtin(args.command)


def cmd_rpc(args):
    print(json.dumps(rpc(args.method, json.loads(args.params) if args.params else None), ensure_ascii=False, indent=1))


def cmd_info(args):
    print(json.dumps(rpc('XBMC.GetInfoLabels', {'labels': args.labels})['result'], ensure_ascii=False, indent=1))


def cmd_log(args):
    state = _state()
    with open(LOG, 'rb') as handle:
        handle.seek(0 if args.all else state.get('log_offset', 0))
        data = handle.read()
        _state(log_offset=handle.tell())
    sys.stdout.write(data.decode('utf-8', 'replace'))


def _quit(pid):
    if _rpc_up():
        try:
            rpc('Application.Quit', timeout=5)
        except OSError:
            pass
    deadline = time.monotonic() + 15
    while pid and time.monotonic() < deadline:
        try:
            os.kill(pid, 0)
        except OSError:
            return
        time.sleep(0.5)
    if pid:
        try:
            os.kill(pid, signal.SIGKILL)
        except OSError:
            pass


def cmd_restart(args):
    _guard()
    _quit(_state().get('kodi'))
    kodi = subprocess.Popen([KODI], env=dict(os.environ, HOME=HOME), stdout=subprocess.DEVNULL,
                            stderr=subprocess.DEVNULL, cwd=ROOT)
    _state(kodi=kodi.pid)
    _wait(_rpc_up, 60, 'JSON-RPC of the test Kodi')
    print('test Kodi restarted: pid {}'.format(kodi.pid))


def cmd_down(args):
    _guard()
    state = _state() if os.path.exists(STATE) else {}
    _quit(state.get('kodi'))
    if state.get('server'):
        try:
            os.kill(state['server'], signal.SIGTERM)
        except OSError:
            pass
    shutil.rmtree(ROOT, ignore_errors=True)
    print('test Kodi down, {} deleted'.format(ROOT))


def main():
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    sub = parser.add_subparsers(dest='cmd', required=True)
    p = sub.add_parser('up')
    p.add_argument('--published', action='store_true', help='the published repository instead of the workspace')
    sub.add_parser('install')
    sub.add_parser('key')
    p = sub.add_parser('setting')
    p.add_argument('id')
    p.add_argument('value')
    sub.add_parser('typekey')
    p = sub.add_parser('expect')
    p.add_argument('label')
    p.add_argument('value')
    p.add_argument('--timeout', type=float, default=20)
    p = sub.add_parser('click')
    p.add_argument('id')
    p = sub.add_parser('shot')
    p.add_argument('name')
    p = sub.add_parser('keys')
    p.add_argument('keys', nargs='+')
    p.add_argument('--gap', type=float, default=0.4)
    p = sub.add_parser('builtin')
    p.add_argument('command')
    p = sub.add_parser('rpc')
    p.add_argument('method')
    p.add_argument('params', nargs='?')
    p = sub.add_parser('info')
    p.add_argument('labels', nargs='+')
    p = sub.add_parser('log')
    p.add_argument('--all', action='store_true')
    sub.add_parser('restart')
    p = sub.add_parser('try')
    p.add_argument('--published', action='store_true', help='the published repository instead of the workspace')
    sub.add_parser('down')
    args = parser.parse_args()
    globals()['cmd_' + args.cmd](args)


if __name__ == '__main__':
    main()
