# -*- coding: utf-8 -*-
"""The server's account schema (GET api/sync/schema, T10), cached in DATA/account/schema.json, and the addon's own
setting defaults (resources/settings.xml) for account settings the account never changed."""
import os
import xml.etree.ElementTree as ET

from wcs.account import docs

_defaults = None


def save(schema):
    docs._write(docs._path('account', 'schema.json'), schema)


def load():
    return docs._read(docs._path('account', 'schema.json'), {})


def settings():
    return (load().get('settings') or {})


def account_ids():
    return list(settings().get('account') or [])


def secret_ids():
    return list(settings().get('secret') or [])


def default(setting_id):
    global _defaults
    if _defaults is None:
        _defaults = {}
        try:
            import xbmcaddon
            path = os.path.join(xbmcaddon.Addon(docs.state.ADDON_ID).getAddonInfo('path'), 'resources', 'settings.xml')
            for node in ET.parse(path).getroot().iter('setting'):
                if node.get('id'):
                    _defaults[node.get('id')] = node.get('default', '')
        except Exception:
            pass
    return _defaults.get(setting_id, '')
