"""
This class implements the base infrastructure to handle themes, the class must leverage
default themes will be stored in layout.root._assets.themes, The expected files in a theme are the following:
    - borders
    - buttons
    - layout
    - menu
    - header
If the following are not available then we should load the default one. 
This would avoid crashes but would come at the expense of a lack of consistent visual layout (alas)
"""
import requests
import os
URL = os.environ['QCMS_HOME_URL'] if 'QCMS_HOME_URL' in os.environ else 'https://dev.the-phi.com/qcms'
def current (_system) :
    return _system['theme']
def List (_url = URL) :
    """
    calling qcms to list the available URL
    """
    try:
        _url = '/'.join([_url,'api','themes','List'])
        return requests.get(_url).json()
    except Exception as e:
        
        pass
    return []
def Get(theme,_url= URL) :
    """
    This function retrieves a particular theme from a remote source
    The name should be in the list provided by the above function
    """
    try:
        _url = '/'.join([_url,'api','themes','Get']) +f'?theme={theme}'
        return requests.get(_url).json()
    except Exception as e:
        pass
    return {}
def installed (path):
    return os.listdir(os.sep.join([path,'_assets','themes']))

def Set(theme,_system) :
    _system['theme'] = theme
    return _system
