# -*- coding: utf-8 -*-
"""One push and one pull of the account (used by the service's sync threads and by the sign-in / sign-out).

push_once: the documents round (docs.round_ops) is queued, then the outbox goes to the server in batches; applied,
duplicate and rejected ops leave the outbox (a rejected op lost to a newer state: the next pull brings the truth);
network errors keep them with a growing back-off. pull_once: one long-poll page, applied with apply.apply_page."""
import json
import threading
import time

from wcs.account import apply, client, docs, hooks, schema, state

BATCH = 200
BATCH_BYTES = 512 * 1024                      # the server takes 2 MB per request; a merge of big channels stays far below
BACKOFF = (1, 2, 5, 15, 30, 60)
docs_lock = threading.RLock()                 # a documents round and a documents apply never interleave


def _store():
    from wcs.library2 import store
    return store


def _log(message):
    client._log(message)


def queue_docs(dialog_open=False, settings=True):
    """Diff the documents / settings / onboarding against their bases and queue the ops (outbox first, then bases)."""
    store = _store()
    with docs_lock:
        conn = store.connect()
        try:
            now = hooks.now_ms(conn)
        finally:
            conn.close()
        ops, bases = docs.round_ops(schema.settings(), now, dialog_open, settings)
        if ops:
            with store.tx() as conn:
                for op in ops:
                    conn.execute('INSERT INTO sync_outbox(op_id, type, key, at, payload) VALUES(?,?,?,?,?)',
                                 (op['op_id'], op['type'], op['key'], op['at'], json.dumps(op['payload'], ensure_ascii=False)))
        docs.save_bases(bases)
    return len(ops)


def _entity_of(op_type, key):
    if op_type in apply.WATCH_OPS:
        return [('watch', key)]
    if op_type in ('list_set', 'list_move'):
        return [('library', key)]
    if op_type == 'hide_continue':
        return [('title', key)]
    if op_type.startswith('feedback'):
        return [('feedback', key)]
    if op_type.startswith('channel_') and not op_type.startswith('channel_progress'):
        return [('channel', key.split(':')[0])]
    if op_type.startswith('setting_'):
        return [('setting', key)]
    return []


def _send(rows):
    ops = [{'op_id': r['op_id'], 'type': r['type'], 'key': r['key'], 'at': r['at'], 'payload': json.loads(r['payload'] or '{}')}
           for r in rows]
    _status, data = client.request('POST', 'sync/push', {'ops': ops}, timeout=8)
    return data


def push_once():
    """Send due ops. Returns the number sent; raises client.AccountError (offline / unauthorized / upgrade)."""
    store = _store()
    conn = store.connect()
    try:
        queued = [dict(r) for r in conn.execute('SELECT * FROM sync_outbox ORDER BY id LIMIT ?', (BATCH,))]
    finally:
        conn.close()
    # in queue order only: a newer op never overtakes an older one that waits for its back-off
    # (a channel's delete before its create would be `gone`, and the create would bring it back)
    now = time.time()
    rows = []
    size = 0
    for row in queued:
        if row['next_attempt'] > now:
            break
        size += len(row['payload'] or '') + len(row['key']) + 120
        if rows and size > BATCH_BYTES:
            break                                 # the rest goes in the next request
        rows.append(row)
    if not rows:
        return 0
    while True:
        try:
            data = _send(rows)
            break
        except client.AccountError as exc:
            if exc.status == 413 and len(rows) > 1:
                rows = rows[:len(rows) // 2]          # too large for the server: smaller requests
                continue
            if exc.status == 413:
                # one op larger than the server takes can never be sent; it must not hold the queue behind it
                _log('op {} too large, dropped'.format(rows[0]['type']))
                with store.tx() as conn:
                    conn.execute('DELETE FROM sync_outbox WHERE id=?', (rows[0]['id'],))
                return 1
            if exc.code in ('offline', 'slow_down'):
                with store.tx() as conn:
                    for r in rows:
                        delay = BACKOFF[min(r['attempts'], len(BACKOFF) - 1)]
                        conn.execute('UPDATE sync_outbox SET attempts=attempts+1, next_attempt=? WHERE id=?',
                                     (time.time() + delay, r['id']))
            raise
    rev = int(data.get('rev') or 0)
    by_id = {r['op_id']: r for r in rows}
    done = []
    refresh = set()
    for result in data.get('results') or []:
        row = by_id.get(result.get('op_id'))
        if not row:
            continue
        done.append(row['id'])
        if result.get('status') == 'applied':
            for entity_key in _entity_of(row['type'], row['key']):
                apply.acked[entity_key] = int(result.get('rev') or rev)     # rows older than this op are stale
        else:
            # rejected (lost to a newer state) or duplicate (an earlier answer was lost): our copy may differ from
            # the server's and no change may be coming for it — fetch the key again (refresh_skipped)
            refresh.update(_entity_of(row['type'], row['key']))
            if result.get('status') == 'rejected':
                _log('op {} {} rejected: {}'.format(row['type'], row['key'].split(':')[0], result.get('reason')))
    if done:
        with store.tx() as conn:
            conn.executemany('DELETE FROM sync_outbox WHERE id=?', [(i,) for i in done])
            if refresh:
                apply.save_skipped(conn, apply.skipped_keys(conn) | refresh)
    return len(done)


def outbox_size():
    store = _store()
    conn = store.connect()
    try:
        return conn.execute('SELECT COUNT(*) FROM sync_outbox').fetchone()[0]
    finally:
        conn.close()


def secrets_rev():
    store = _store()
    conn = store.connect()
    try:
        row = conn.execute("SELECT value FROM sync_state WHERE key='secrets_rev'").fetchone()
        return int(row[0]) if row else -1
    finally:
        conn.close()


def fetch_secrets():
    from wcs.account import secrets
    _status, data = client.request('GET', 'sync/secrets', timeout=10)
    secrets.apply(data.get('values') or {})
    with _store().tx() as conn:
        conn.execute("INSERT OR REPLACE INTO sync_state(key, value) VALUES('secrets_rev', ?)", (str(int(data.get('secrets_rev') or 0)),))


def bootstrap(replace=True):
    """The whole account into the local copy (a resync; the sign-in uses switch.py with its backup)."""
    _status, snap = client.request('GET', 'sync/bootstrap', timeout=60)
    changes = apply.bootstrap_changes(snap)
    shown = _progress_begin(len(changes))
    try:
        with docs_lock:
            apply.apply_page(changes, snap.get('rev') or 0, replace=replace)
    finally:
        if shown:
            _progress_release()
    fetch_secrets()


def refresh_skipped():
    """Rows skipped while our op on them was queued (T6) are fetched again once the op was answered: the server's
    row may differ from ours (a merged play count, a newer change from elsewhere, our op rejected as stale)."""
    store = _store()
    conn = store.connect()
    try:
        keys = apply.due_refresh(conn)
    finally:
        conn.close()
    if not keys:
        return 0
    _status, data = client.request('POST', 'sync/rows', {'keys': [list(k) for k in keys]}, timeout=10)
    with docs_lock:
        documents = [c for c in data.get('changes') or [] if c.get('entity') not in apply.SQL_ENTITIES]
        if documents:
            queue_docs(dialog_open=_dialog_open(), settings=False)
        apply.apply_page(data.get('changes') or [], None)
    return len(keys)


def pull_once(wait, abort):
    """One long-poll; returns 'changes', 'idle', 'logout' or 'resync'. Raises client.AccountError."""
    refresh_skipped()
    since = apply.cursor()
    _status, data = client.long_poll(since, wait, abort)
    if data.get('logout'):
        return 'logout'
    if data.get('resync'):
        bootstrap(replace=True)
        return 'resync'
    if int(data.get('secrets_rev') or 0) != secrets_rev():
        fetch_secrets()
    changes = data.get('changes') or []
    if changes or int(data.get('rev') or since) != since:
        started = time.time()
        shown = _progress_begin(len(changes))
        try:
            with docs_lock:
                documents = [c for c in changes if c.get('entity') not in apply.SQL_ENTITIES]
                if documents:
                    queue_docs(dialog_open=_dialog_open(), settings=False)   # local edits become ops before a row lands
                queued = time.time()
                apply.apply_page(changes, int(data.get('rev') or since))
        finally:
            if shown:
                _progress_release()
        if time.time() - started > 0.5:
            _log('pull applied {} changes in {:.2f} s (documents round {:.2f} s)'.format(
                len(changes), time.time() - started, queued - started))
    return 'changes' if data.get('more') or changes else 'idle'


def _progress_begin(count):
    """A large page shows Kodi's background progress until its Home rows are built (wcs/account/progress.py)."""
    try:
        from wcs.account import progress
        return progress.begin_changes(count)
    except Exception:
        return False


def _progress_release():
    try:
        from wcs.account import progress
        progress.release()
    except Exception:
        pass


def _dialog_open():
    try:
        import xbmc
        return xbmc.getCondVisibility('Window.IsActive(addonsettings)')
    except Exception:
        return False
