#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pandas as pd
from include.config import CleaningConfig, CountingConfig, NotebookConfig
from include.preprocessing import CleaningData
from include.generate_notebook import GenerateNotebook
import nbformat
from nbconvert import HTMLExporter
import codecs

"""
Action file that holds all the different configuration for a
specific dataset.

Import the specific cleaning and plotting methods that are
in the same folder
"""


def main():
    pd.set_option('display.max_rows', 300)

    # Load dataset
    df = pd.read_csv(CleaningConfig.raw_data)

    # Cleaning_process
    cleaning_process = CleaningData(df)
    df = cleaning_process.cleaning()
    cleaning_process.write_df()
    cleaning_process.write_config_file()

    # Get the folder to record df
    folder_df = CountingConfig.folder_df
    # Notebook writing
    notebook_location = '{}{}'.format(NotebookConfig.notebook_folder,
                                      NotebookConfig.notebook_filename)
    notebook = GenerateNotebook(notebook_location)
    notebook.output_total_participants()
    for s in cleaning_process.structure_by_section:
        section = cleaning_process.structure_by_section[s]
        notebook.add_section(s)
        for group in section:
            notebook.add_group(group)
            for question in section[group]:
                list_questions = question['survey_q']
                original_question = question['original_question']
                answer_format = question['answer_format']
                file_answer = question['file_answer']
                order_question = question['order_question']
                # To avoid having each questions written in a new line
                # it joins them together before writing it
                question_to_write = '; '.join(original_question)
                notebook.add_question_title(question_to_write)
                # for txt in original_question:
                #     # notebook.add_question_title(txt)

                if answer_format not in ['freetext', 'datetime', 'freenumeric']:
                    notebook.add_count(list_questions, answer_format, file_answer, order_question,
                                       folder_df)
                    # Need to specify != likert because if likert item == 1 it uses the barchart
                    # and will plot the percentages even if it doesn't make sense to do that for
                    # a likert scale
                    if NotebookConfig.show_percent is True and answer_format != 'likert':
                        notebook.add_percentage()
                        notebook.add_display_all()
                    else:
                        notebook.add_display_count()
                    notebook.add_plot(answer_format)

                if answer_format == 'freetext':
                    notebook.add_wordcloud(list_questions)
                    # notebook.add_count(list_questions, answer_format, file_answer)
                    # notebook.add_plot(answer_format)

                if answer_format == 'freenumeric':
                    notebook.add_count(list_questions, answer_format, file_answer, order_question,
                                       folder_df)
                    notebook.add_plot(answer_format)

                # if answer_format == 'ranking':
                #     notebook.add_count(list_questions, answer_format, file_answer, order_question,
                #                        folder_df)
                #     notebook.add_percentage()
                #     notebook.add_display_percentage()
                #     notebook.add_plot(answer_format)

    print('Running notebook')
    notebook.run_notebook()
    print('Saving notebook')
    notebook.save_notebook()

    # https://stackoverflow.com/questions/37657547/how-to-save-jupyter-notebook-to-html-by-code
    print('Convert the notebook into an html file')
    filepath = notebook_location
    export_path = '{}/{}'.format(NotebookConfig.notebook_folder,
                                 NotebookConfig.notebook_html)

    with open(filepath) as fh:
        nb = nbformat.reads(fh.read(), as_version=4)

    exporter = HTMLExporter()

    # source is a tuple of python source code
    # meta contains metadata
    source, meta = exporter.from_notebook_node(nb)
    codecs.open(export_path, 'w', encoding='utf-8').write(source)


if __name__ == "__main__":
    main()

@@ -39,16 +39,40 @@
 f-8 -*-%0A
+import sys%0Aimport getopt
 %0Aimport 
@@ -502,58 +502,1006 @@
 def 
-main():%0A    pd.set_option('display.max_rows', 300)
+get_arguments(argv):%0A    %22%22%22%0A    %22%22%22%0A    country = None%0A    year = None%0A    try:%0A        opts, args = getopt.getopt(argv, 'hc:y:', %5B'country=', 'year='%5D)%0A    except getopt.GetoptError:%0A        print('run.py -c %3Ccountry%3E -y %3Cyear%3E')%0A        sys.exit(2)%0A    for opt, arg in opts:%0A        if opt == '-h':%0A            print('run.py -c %3Ccountry%3E -y %3Cyear%3E')%0A            sys.exit()%0A        elif opt in ('-c', '--country'):%0A            country = arg%0A        elif opt in ('-y', '--year'):%0A            year = arg%0A    if country and year:%0A        return country, year%0A    else:%0A        print('Need a country and a year. Please use the following command:%5Cn' +%0A              '%5Ctrun.py -c %3Ccountry%3E -y %3Cyear%3E')%0A        sys.exit(2)%0A%0Adef main():%0A%0A    pd.set_option('display.max_rows', 300)%0A%0A    # Get which country and which year to create the analysis%0A    country, year = get_arguments(sys.argv%5B1:%5D)%0A%0A    # Getting the different location files from the config file and%0A    folder = '%7B%7D_%7B%7D'.format(year, country)%0A
 %0A%0A  

add argument parser