#!/usr/bin/env python

from typing import Dict

import click

TEMPLATE_START = '{%'
TEMPLATE_END = '%}'

GERMAN = {
    'DOCUMENT_CLASS_LANG': ', ngerman',
    'DOCUMENT_LANGUAGE': 'ngerman',
    'TITLE_PAGE_COMMENT': 'TITELSEITE',
    'TITLE_DEPARTEMENT_NAME': 'Institut für Informatik',
    'TITLE_GROUP_NAME': 'Datenbanken und Informationssysteme',
    'TITLE_FOOTER_MASTER': 'Masterarbeit',
    'TITLE_FOOTER_BACHELOR': 'Bachelorarbeit',
    'TITLE_SUPERVISED_BY': 'betreut von',
    'STATUTORY_DECLARATION_COMMENT': 'Eidesstattliche Erklärung',
    'GERMAN_ABSTRACT_COMMENT': 'ZUSAMMENFASSUNG',
    'ENGLISH_ABSTRACT_COMMENT': 'ABSTRACT',
    'ACKNOWLEDGEMENTS_COMMENT': 'DANKSAGUNG',
    'TABLE_OF_CONTENTS_COMMENT': 'INHALTSVERZEICHNIS',
    'CHAPTER_COMMENT': 'KAPITEL',
    'CHAPTER_1_NAME': 'Einleitung',
    'CHAPTER_2_NAME': 'Kapitel 2',
    'SECTION_COMMENT': 'ABSCHNITT',
    'SECTION_NAME': 'Abschnitt',
    'SUB_SECTION_NAME': 'Teilabschnitt',
    'APPENDIX_COMMENT': 'ANHANG',
    'APPENDIX_TITLE': 'Anhang',
    'APPENDIX_SECTION_NAME': 'Anhang Abschnitt',
    'BIBLIOGRAPHY_COMMENT': 'LITERATURVERZEICHNIS',
    'BIBLIOGRAHPY_TITLE': 'Literaturverzeichnis',

}

ENGLISH = {
    'DOCUMENT_CLASS_LANG': '',
    'DOCUMENT_LANGUAGE': 'english',
    'TITLE_PAGE_COMMENT': 'TITLEPAGE',
    'TITLE_DEPARTEMENT_NAME': 'Department of Computer Science',
    'TITLE_GROUP_NAME': 'Databases and Information Systems',
    'TITLE_FOOTER_MASTER': 'Master Thesis',
    'TITLE_FOOTER_BACHELOR': 'Bachelor Thesis',
    'TITLE_SUPERVISED_BY': 'supervised by',
    'STATUTORY_DECLARATION_COMMENT': 'STATUTORY DECLARATION',
    'GERMAN_ABSTRACT_COMMENT': 'german ABSTRACT',
    'ENGLISH_ABSTRACT_COMMENT': 'ABSTRACT',
    'ACKNOWLEDGEMENTS_COMMENT': 'ACKNOWLEDGEMENTS',
    'TABLE_OF_CONTENTS_COMMENT': 'TABLE OF CONTENTS',
    'CHAPTER_COMMENT': 'CHAPTER',
    'CHAPTER_1_NAME': 'Introduction',
    'CHAPTER_2_NAME': 'Chapter 2',
    'SECTION_COMMENT': 'SECTION',
    'SECTION_NAME': 'Section',
    'SUB_SECTION_NAME': 'Subsection',
    'APPENDIX_COMMENT': 'APPENDIX',
    'APPENDIX_TITLE': 'Appendix',
    'APPENDIX_SECTION_NAME': 'Appendix Section',
    'BIBLIOGRAPHY_COMMENT': 'BIBLIOGRAPHY',
    'BIBLIOGRAHPY_TITLE': 'Bibliography',
}

THESIS_TITLE = 'THESIS_TITLE'
STUDENT_NAME = 'STUDENT_NAME'
STUDENT_NAME_TITLE = 'STUDENT_NAME_TITLE'
SUPERVISOR_NAME = 'SUPERVISORS'


def __load_template() -> str:
    with open(f"./templates/template.tex", 'r') as fp:
        return ''.join(fp.readlines())


def _load_statutory_declaration(type: str) -> str:
    file = 'master_statutory_declaration.tex' if type == 'master' else 'bachelor_statutory_declaration.tex'
    with open(f"./templates/{file}", 'r') as fp:
        return ''.join(fp.readlines())


def _template_replace(template: str, key: str, value: str) -> str:
    return template.replace(f"{TEMPLATE_START} {key} {TEMPLATE_END}", value)


def _insert_translations(template: str, translations: Dict[str, str]) -> str:
    _template = template
    for key, translation in translations.items():
        _template = _template_replace(_template, key, translation)
    return _template


def _insert_title_page_content(students: str, template: str, title: str) -> str:
    template = _template_replace(template, THESIS_TITLE, title)
    template = _template_replace(template, STUDENT_NAME, students)
    template = _template_replace(template, STUDENT_NAME_TITLE, ('\\\\').join(students.split(',')))
    return template


def _title_footer(type: str, language: str) -> str:
    if language == 'en':
        return ENGLISH['TITLE_FOOTER_MASTER'] if type == 'master' else ENGLISH['TITLE_FOOTER_BACHELOR']
    else:
        return GERMAN['TITLE_FOOTER_MASTER'] if type == 'master' else GERMAN['TITLE_FOOTER_BACHELOR']


def _insert_type_specific_content(template: str, type: str, language: str) -> str:
    thesis_type = 'MASTER' if type == 'master' else 'BACHELOR'
    template = _template_replace(template, 'THESIS_TYPE', thesis_type)
    template = _template_replace(template, 'STATUTORY_DECLARATION', _load_statutory_declaration(type))
    template = _template_replace(template, 'TITLE_FOOTER', _title_footer(type, language))
    return template


def _create_thesis_template(language: str, type: str, title: str, students: str, supervisors: str) -> str:
    translations = ENGLISH if language == 'en' else GERMAN
    template = _insert_translations(__load_template(), translations)
    template = _insert_title_page_content(students, template, title)

    template = _insert_type_specific_content(template, type, language)
    return _template_replace(template, SUPERVISOR_NAME, supervisors)


@click.command()
@click.option('--lang', default='en', prompt='Language of the Thesis', help='Language of thesis template',
              type=click.Choice(['en', 'de']))
@click.option('--type', default='bachelor', prompt='Thesis type', help='Select type of thesis',
              type=click.Choice(['bachelor', 'master']))
@click.option('--title', default='title', prompt='Thesis title', help='Title of the thesis')
@click.option('--student', prompt='Name(s) of Student(s) (comma separated)',
              help='Names of contributing students comma separated')
@click.option('--supervisor', prompt='Name(s) of Supervisor(s) (comma separated)',
              help='Names of supervisor comma separated')
@click.option('-o', '--output', required=False, help='Name of outputfile')
def create_template(lang, type, title, student, supervisor, output=None) -> None:
    template = _create_thesis_template(lang, type, title, student, supervisor)
    if output is None:
        print(_create_thesis_template(lang, type, title, student, supervisor))
    else:
        with open(output, 'w+') as fp:
            fp.writelines(template)


if __name__ == '__main__':
    create_template()