import json
import os

from Rest.Answer import try_catch_error_answer, Answer, MessageCodes
from Models import main_channel_id, InfoNuggetModel, QuestionsModel
from Rest.UserRegex import get_username_regex, get_display_name_regex

with open("StaticData/TU_course_of_study.json", 'r') as f:
    course_of_study_options = json.loads(f.read())
    for i in course_of_study_options:
        if i['en'] == '':
            i['en'] = i['de']
with open("StaticData/project_type_options.json", 'r') as f:
    project_type_options = json.loads(f.read())
    for i in project_type_options:
        if i['de'] == '':
            i['de'] = i['en']
with open("StaticData/interest_options.json", 'r') as f:
    interest_options = json.loads(f.read())
with open("StaticData/tags.json", 'r') as f:
    tags = json.loads(f.read())
themes = []
for file in os.listdir("StaticData/themes"):
    with open("StaticData/themes/" + file, 'r') as f:
        themes.append(json.loads(f.read()))


@try_catch_error_answer
class ServerVarREST:
    @staticmethod
    def get_main_cannel_ID(user):
        answer = Answer()
        return answer.add_message(MessageCodes.WORKED).add_result({"main_channel_id": main_channel_id})

    @staticmethod
    def get_course_of_study_options(user):
        answer = Answer()
        return answer.add_message(MessageCodes.WORKED).add_result({"options": course_of_study_options})

    @staticmethod
    def get_project_type_options(user):
        answer = Answer()
        return answer.add_message(MessageCodes.WORKED).add_result({"options": project_type_options})

    @staticmethod
    def get_interests_options(user):
        answer = Answer()
        return answer.add_message(MessageCodes.WORKED).add_result({"options": interest_options})

    @staticmethod
    def get_regex(user):
        answer = Answer()
        result = {
            "username_regex": get_username_regex(),
            "display_name_regex": get_display_name_regex()
        }
        return answer.add_message(MessageCodes.WORKED).add_result(result)

    @staticmethod
    def get_themes(user):
        answer = Answer()
        return answer.add_message(MessageCodes.WORKED).add_result({"options": [t["name"] for t in themes]})

    @staticmethod
    def get_tags(user):
        answer = Answer()
        return answer.add_message(MessageCodes.WORKED).add_result(tags)

    @staticmethod
    def get_theme(user, theme_name):
        answer = Answer()
        theme = None
        for t in themes:
            if t["name"] == theme_name:
                theme = t
        return answer.add_message(MessageCodes.WORKED).add_result({"theme": json.dumps(theme)})

    @staticmethod
    def get_all(user):
        answer = Answer()
        theme = None
        for t in themes:
            if t["name"] == user.theme:
                theme = t
        result = {
            "main_channel_id": main_channel_id,
            "course_of_study_options": course_of_study_options,
            "project_type_options": project_type_options,
            "interest_options": interest_options,
            "username_regex": get_username_regex(),
            "display_name_regex": get_display_name_regex(),
            "themes": [t["name"] for t in themes],
            "selected_theme": json.dumps(theme),
            "tags": tags
        }
        return answer.add_message(MessageCodes.WORKED).add_result(result)

    @staticmethod
    def get_card_vars(user):
        answer = Answer()
        nuggets = InfoNuggetModel.query.all()
        questions = QuestionsModel.query.filter(QuestionsModel.official == True).all()
        return answer.add_message(MessageCodes.WORKED).add_result({
            "info_nuggets": nuggets,
            "questions": questions
        })
