import sys, os
import tarfile
import shutil
from edx_gen import  _edx_consts
from edx_gen import  _read_metadata
from edx_gen import  _write_structure
from edx_gen import  _write_comps
from edx_gen import  _write_comp_html
from edx_gen import  _write_comp_checkboxes
from edx_gen import  _write_comp_video
from edx_gen import  _xml_google_doc
from edx_gen import  _markdown
from edx_gen import  _util
import __SETTINGS__

#--------------------------------------------------------------------------------------------------
# Text strings
WARNING = "      WARNING:"

#--------------------------------------------------------------------------------------------------
# write to either units folder or problems folder, depending on the type
def writeCompsForUnit(md_filepath, unit_filename):

    # print("component_path", component_path)

    # generate the files in the right folders
    tree_snippets = _markdown.convertMd(md_filepath)

    # check we have at least 2 snippets, the header and one component
    if len(tree_snippets) <= 1:
        print(WARNING, 'The markdown file does not seem to contain any components:', md_filepath)

    # get the display name of the unit
    first_h1_tag = list(tree_snippets[0].iter('h1'))[0]
    unit_display_name = first_h1_tag.get('display_name')

    # list to store all files
    unit_comps = []

    # process components
    for i in range(1, len(tree_snippets)):
        tree_snippet = tree_snippets[i]

        # generate the files
        new_filename = unit_filename + '_c' + str(i)
        comp_files = _writeFilesForSnippet(md_filepath, new_filename, tree_snippet, unit_filename, unit_display_name)
        unit_comps.extend(comp_files)

    # return the result
    return unit_comps

#--------------------------------------------------------------------------------------------------
# write to either units folder or problems folder, depending on the type
def _writeFilesForSnippet(md_filepath, comp_filename, tree_snippet, unit_filename, unit_display_name):

    meta_tag = None
    comp_type = None
    # meta_text = None

    # get the h1 tags
    h1_tags = list(tree_snippet.iter('h1'))
    if len(h1_tags) == 0:
        print(WARNING, 'The snippet does not start with any settings:', md_filepath)
        return

    # get the meta tag for the snippet
    meta_tag = h1_tags[0] # the first h1 the should contain the meta data

    # # check the meta tag text
    # meta_text = meta_tag.text.strip()
    # if meta_text == None or meta_text != 'UNIT':
    #     print(WARNING, 'The markdown file must start with the "UNIT" settings:', component_path)
    #     print(WARNING, 'Make sure that the first line of the markdown file is blank')

    # get the type for this component
    comp_type = meta_tag.get('type')
    if comp_type == None or comp_type not in _edx_consts.METADATA_ENUMS['type']:
        print(WARNING, 'The "type" setting is not recognised:', md_filepath)
        print(WARNING, '  Found:', comp_type)
        print(WARNING, '  Valid options:', _edx_consts.METADATA_ENUMS['type'])

    # write xml and/or html files
    if comp_type == 'html':
        print("    |_ HTML COMP")

        # get the setting out of the meta_tag
        settings = _read_metadata.getMetaSettings(md_filepath, meta_tag, 
            _edx_consts.COMP_HTML_REQ, _edx_consts.COMP_HTML_OPT )

        # check that we have settings
        if not settings:
            print(WARNING, 'There seem to be no settings for this "html" component:', md_filepath)
            return

        # remove h1 meta_tag from the tree so it does not end up in the output
        tree_snippet.remove(meta_tag)

        # write .html file to COMP_HTML_FOLDER
        # write .xml file to COMP_HTML_FOLDER
        # return the list of files
        return _write_comp_html.writeXmlForHtmlComp(
            md_filepath, comp_filename, tree_snippet, settings, unit_filename)
    
    elif comp_type == 'problem-checkboxes':
        print("    |_ PROBLEM CHECKBOXES")

        # get the setting out of the meta_tag
        settings = _read_metadata.getMetaSettings(md_filepath, meta_tag, 
            _edx_consts.COMP_PROB_QUIZ_REQ, _edx_consts.COMP_PROB_QUIZ_OPT )

        # check that we have settings
        if not settings:
            print(WARNING, 'There seem to be no settings for this "problem-checkboxes" component:', md_filepath)
            return

        # remove h1 meta_tag from the tree so it does not end up in the output
        tree_snippet.remove(meta_tag)

        # write .xml file to COMP_PROBS_FOLDER
        # return the list of files
        return _write_comp_checkboxes.writeXmlForProbCheckboxesComp(
            md_filepath, comp_filename, tree_snippet, settings, unit_filename)
    
    
    elif comp_type == 'video':
        print("    |_ VIDEO COMP")

        # get the setting out of the meta_tag
        settings = _read_metadata.getMetaSettings(
            md_filepath, meta_tag, _edx_consts.COMP_VIDEO_REQ, _edx_consts.COMP_VIDEO_OPT )

        # check that we have settings
        if not settings:
            print(WARNING, 'There seem to be no settings for this "video" component:', md_filepath)
            return

        # remove h1 meta_tag from the tree so it does not end up in the output
        tree_snippet.remove(meta_tag)

        # write .xml file to COMP_VIDS_FOLDER
        # for each language
        # write .html file to COMP_HTML_FOLDER
        # write .xml file to COMP_HTML_FOLDER
        # return the list of files
        return _write_comp_video.writeXmlForVidComp(
            md_filepath, comp_filename, settings, unit_filename)


    elif comp_type == 'google-doc':
        print("    |_ GOOGLE DOC COMP")

        # get the setting out of the meta_tag
        settings = _read_metadata.getMetaSettings(md_filepath, meta_tag,
            _edx_consts.COMP_GOOGLE_DOC_REQ, _edx_consts.COMP_GOOGLE_DOC_OPT)

        # check that we have settings
        if not settings:
            print(WARNING, 'There seem to be no settings for this "Google Doc" component:', md_filepath)
            return

        # in this case, no files are written
        # we return the component tag instead
        return _xml_google_doc.tagForGoogleDocComp(comp_filename, settings, unit_filename)


    else:
        print(WARNING, 'Component type not recognised:', comp_type, "in", md_filepath)

#--------------------------------------------------------------------------------------------------

[((27, 20, 27, 52), 'edx_gen._markdown.convertMd', '_markdown.convertMd', ({(27, 40, 27, 51): 'md_filepath'}, {}), '(md_filepath)', False, 'from edx_gen import _markdown\n'), ((87, 19, 88, 66), 'edx_gen._read_metadata.getMetaSettings', '_read_metadata.getMetaSettings', ({(87, 50, 87, 61): 'md_filepath', (87, 63, 87, 71): 'meta_tag', (88, 12, 88, 37): '_edx_consts.COMP_HTML_REQ', (88, 39, 88, 64): '_edx_consts.COMP_HTML_OPT'}, {}), '(md_filepath, meta_tag, _edx_consts.\n    COMP_HTML_REQ, _edx_consts.COMP_HTML_OPT)', False, 'from edx_gen import _read_metadata\n'), ((101, 15, 102, 78), 'edx_gen._write_comp_html.writeXmlForHtmlComp', '_write_comp_html.writeXmlForHtmlComp', ({(102, 12, 102, 23): 'md_filepath', (102, 25, 102, 38): 'comp_filename', (102, 40, 102, 52): 'tree_snippet', (102, 54, 102, 62): 'settings', (102, 64, 102, 77): 'unit_filename'}, {}), '(md_filepath, comp_filename,\n    tree_snippet, settings, unit_filename)', False, 'from edx_gen import _write_comp_html\n'), ((108, 19, 109, 76), 'edx_gen._read_metadata.getMetaSettings', '_read_metadata.getMetaSettings', ({(108, 50, 108, 61): 'md_filepath', (108, 63, 108, 71): 'meta_tag', (109, 12, 109, 42): '_edx_consts.COMP_PROB_QUIZ_REQ', (109, 44, 109, 74): '_edx_consts.COMP_PROB_QUIZ_OPT'}, {}), '(md_filepath, meta_tag, _edx_consts.\n    COMP_PROB_QUIZ_REQ, _edx_consts.COMP_PROB_QUIZ_OPT)', False, 'from edx_gen import _read_metadata\n'), ((121, 15, 122, 78), 'edx_gen._write_comp_checkboxes.writeXmlForProbCheckboxesComp', '_write_comp_checkboxes.writeXmlForProbCheckboxesComp', ({(122, 12, 122, 23): 'md_filepath', (122, 25, 122, 38): 'comp_filename', (122, 40, 122, 52): 'tree_snippet', (122, 54, 122, 62): 'settings', (122, 64, 122, 77): 'unit_filename'}, {}), '(md_filepath,\n    comp_filename, tree_snippet, settings, unit_filename)', False, 'from edx_gen import _write_comp_checkboxes\n'), ((129, 19, 130, 91), 'edx_gen._read_metadata.getMetaSettings', '_read_metadata.getMetaSettings', ({(130, 12, 130, 23): 'md_filepath', (130, 25, 130, 33): 'meta_tag', (130, 35, 130, 61): '_edx_consts.COMP_VIDEO_REQ', (130, 63, 130, 89): '_edx_consts.COMP_VIDEO_OPT'}, {}), '(md_filepath, meta_tag, _edx_consts.\n    COMP_VIDEO_REQ, _edx_consts.COMP_VIDEO_OPT)', False, 'from edx_gen import _read_metadata\n'), ((145, 15, 146, 64), 'edx_gen._write_comp_video.writeXmlForVidComp', '_write_comp_video.writeXmlForVidComp', ({(146, 12, 146, 23): 'md_filepath', (146, 25, 146, 38): 'comp_filename', (146, 40, 146, 48): 'settings', (146, 50, 146, 63): 'unit_filename'}, {}), '(md_filepath, comp_filename, settings,\n    unit_filename)', False, 'from edx_gen import _write_comp_video\n'), ((153, 19, 154, 77), 'edx_gen._read_metadata.getMetaSettings', '_read_metadata.getMetaSettings', ({(153, 50, 153, 61): 'md_filepath', (153, 63, 153, 71): 'meta_tag', (154, 12, 154, 43): '_edx_consts.COMP_GOOGLE_DOC_REQ', (154, 45, 154, 76): '_edx_consts.COMP_GOOGLE_DOC_OPT'}, {}), '(md_filepath, meta_tag, _edx_consts.\n    COMP_GOOGLE_DOC_REQ, _edx_consts.COMP_GOOGLE_DOC_OPT)', False, 'from edx_gen import _read_metadata\n'), ((163, 15, 163, 90), 'edx_gen._xml_google_doc.tagForGoogleDocComp', '_xml_google_doc.tagForGoogleDocComp', ({(163, 51, 163, 64): 'comp_filename', (163, 66, 163, 74): 'settings', (163, 76, 163, 89): 'unit_filename'}, {}), '(comp_filename, settings, unit_filename)', False, 'from edx_gen import _xml_google_doc\n')]