"""
This file handles the structure (strict) of the configuration file,
Not all elements are programmatically editable (for now)
"""
# import meta
class Section :

    def build (self,**_args):
        _data = {}
        for _attr in dir(self):
            if not _attr.startswith('_') and _attr not in ['build','update']:
                _kwargs = _args if _attr not in _args or type(_args[_attr]) !=dict else _args[_attr]
                _data[_attr] = getattr(self,_attr)(**_kwargs)
        _name =  type (self).__name__.lower()
        
        return {_name:_data }

    
            
    def update(self,_default,**_args) :
        for _attr in _default.keys():
            
            if _attr in _args :
                _default[_attr] = _args[_attr]
        return _default
    
class System (Section) :

    def logo(self,**_args):
        
        return 'www/html/_assets/images/logo.png' if 'logo' not in _args else _args['logo']
    def theme (self,**_args):
        """
        setting the theme given the name of the theme
        :name name of the theme to set (optional)
        """
        return 'default' if 'name' not in _args else _args['name']
    def version(self,**_args):
        import cms
        return cms.meta.__version__ if 'version' not in _args else _args['version']
    def context (self,**_args):
        return "" if 'context' not in _args else _args['context']
    def app (self,**_args):
        _data = {'debug':True,'port':8084,'threaded':True,'host':'0.0.0.0'}
        return self.update(_data,**_args)
        
    def source(self,**_args):
        _data = {'id':'disk'}
        if set(['key','auth']) & set(_args.keys()):
            #
            # key: reboot the app & auth: for cloud access
            #
            for _attr in ['key','auth'] :
                if _attr in _args:
                    _data[_attr] = _args[_attr]
        
        _data = self.update(_data,**_args)
        return _data

class Layout (Section):
    def index (self,**_args):
        return "index.html" if 'index' not in _args else _args['index']
    def root(self,**_args):
        return 'wwww/html' if 'root' not in _args else _args['root']
    def footer(self,**_args):
        return [{'text':'Powered by QCMS'}] if 'footer' not in _args else _args['footer']
    def on(self,**_args):
        return {'load':{}} if 'on' not in _args else _args['on']
    def order(self,**_args):
        return {'menu':[]} if 'order' not in _args else _args['order']
    # def menu (self,**_args):
    #     return {'menu':[]} if 'menu' not in _args else _args['menu']
    def overwrite(self,**_args):
        return {}
    def header (self,**_args):
        _data =  {"title":"QCMS Project", "subtitle":"Powered by Python Flask"}
        return self.update(_data,**_args)
    
