# from cms import _system, _header,_layout
import base64
import os
import json
import meta
from cms.engine import themes
# def make (**_args):
#     """
#     :context
#     :port   port to be served
    
#     :title  title of the application
#     :root   folder of the content to be scanned
#     """
#     if 'port' in _args :
#         _args['app'] = {'port':_args['port']}
#         del _args['port']
#     if 'context' not in _args :
#         _args['context'] = ''
#     _info ={'system': _system(**_args)}
#     _hargs = {'title':'QCMS'} if 'title' not in _args else {'title':_args['title']}
#     _hargs['subtitle'] = '' if 'subtitle' not in _args else _args['subtitle']
#     _hargs['logo'] = True
    
    
    
    
#     _info['layout'] = _layout(root=_args['root'],index='index.html')
#     _info['layout']['header'] = _header(**_hargs)
#     #
   

#     if 'footer' in _args :
#         _info['layout']['footer'] = [{'text':_args['footer']}] if type(_args['footer']) != list else [{'text':term} for term in _args['footer']]
#     else:
#         _info['layout']['footer'] = [{'text':'Powered by QCMS'}]
    
#     return _info
def make_folder (projectFolder, webroot):
    """
    This function creates project folders, inside the project folder is the web root folder
    """
    
    if not os.path.exists(projectFolder):
        os.makedirs(projectFolder)
    _path = os.sep.join([projectFolder,webroot])
    folders = ['_assets',os.sep.join(['_assets','images']), os.sep.join(['_assets','themes']), '_plugins']
    for folder in folders :
        _projectPath =  os.sep.join([_path,folder])
        if not os.path.exists(_projectPath) :
            os.makedirs(_projectPath)
def _ilogo (_path):
    """
    This function creates a default logo in a designated folder, after the project folder has been created
    :_path  project path
    """
    _image = """data:image/png;base64,[encoded data, 4732 characters]"""
    _image1 = """data:image/png;base64,[encoded data, 9868 characters]"""
    _image2 = """data:image/png;base64,[encoded data, 9720 characters]"""
    _index = 0
    for _image in [_image1,_image2] :
        _,_image= _image.split(',',1)
        _logoPath = os.sep.join([_path,'_assets','images','logo.png'])
        if _index > 0 :
            _logoPath = _logoPath.replace('.png',f'-{_index}.png')
        f = open(_logoPath,'wb')
        f.write(base64.b64decode(_image))
        f.close()
        _index += 1

def _index (_path,root):
    """
    Creating a default index.html for the site given the project root location
    """
    _html = f"""
    <div align="center">
        <div class="large-text">Thank you for considering QCMS</div>
        <div>version {meta.__version__} by {meta.__author__}, {meta.__email__}</div>
    </div>
    <p>
    
    <div>

    </div>
    <div style="display:grid; grid-template-columns: auto 50%; gap:20px; width:80%; margin-left:10%;">
        <div class="border-round border">
        <img src="{root}/_assets/images/logo-1.png" style="width:250px" align="left">
        <div>
            <b>QCMS</b> is powered by Python/Flask
            <ul>
                Small footprint & can work as either a portal or a standalone application
            </ul>
            <ul>
                Simple paradigm that leverages folder structure to generate a site
            </ul>
        </div>

        </div>
        <div class="border border-round">
        <b>QCMS</b> has built-in support for industry standard frameworks
            <div style="float:right; width:250px;">
                <i class="fa-solid fa-cog fa-spin" style="font-size:125px; color:#ff6500"></i>
                <i class="fa-solid fa-cog fa-spin fa-2x" style="margin-top:-20px; margin-left:-20px; font-size:100px; color:#4682B4"></i>
            </div>
            <ul>
                <i class="fa-solid fa-minus"></i> fontawesome
                <br><i class="fa-solid fa-minus"></i> jQuery
                <br><i class="fa-solid fa-minus"></i> Apexcharts
            </ul>
            <ul>
                As a python/flask enabled framework, there can also be support for the wide range of libraries for various AI/ML projects
            </ul>

        </div>
    </div>
    <p>
        <div align="center">Learn more about <b>QCMS</b> and at {themes.URL}</div>
    </p>
    """
    _indexfile = os.sep.join([_path,'index.html'])
    if not os.path.exists(_indexfile) and os.path.exists(_path):
        #
        # In case a project needs to be upgraded ...
        f = open(_indexfile,'w')
        f.write(_html)
        f.close()
def _itheme(_path,_root,_name='default'):
    _data = themes.Get(_name)[_name]
    _themefolder = os.sep.join([_path,_root,'_assets','themes',_name])
    if not os.path.exists(_themefolder) :
        os.makedirs(_themefolder)

    for _name in _data :
        f = open(os.sep.join([_themefolder,_name+'.css']),'w')
        f.write(_data[_name])
        f.close()
        pass
def make (**_args) :
    """
    This function create a project folder and within the folder are all the elements needed
    """
    _config = _args['config']
    _config['plugins'] = {}
    _folder = _args['folder']
    _root = _config['layout']['root']    #-- web root folder
    make_folder(_folder,_root)
    f = open(os.sep.join([_folder,'qcms-manifest.json']),'w')
    f.write( json.dumps(_config))
    f.close()

    _ilogo(os.sep.join([_folder,_root]))
    print ([_folder,_root])
    _index(os.sep.join([_folder,_root]),_root)
    _itheme(_folder,_root)
    