from rest_framework import serializers

from utils.validators import dic_validetor
from . import conf


def docx_file_validator(file):
    if not file.name.endswith(conf.allowed_formats_for_docx_files):
        raise serializers.ValidationError(
            f'The file format must be { ", ".join([item[1:] for item in conf.allowed_formats_for_docx_files])}')

    # TODO
    if file.size > (1024 * 1024 * conf.maximum_docx_file_size_MB):
        raise serializers.ValidationError(
            f'The maximum file size is {conf.maximum_docx_file_size_MB} MB')
    return file


def data_file_validator(file):
    if not file.name.endswith(conf.allowed_formats_for_data_files):
        raise serializers.ValidationError(
            f'The file format must be { ", ".join([item[1:] for item in conf.allowed_formats_for_data_files])}')
    return file


def json_data_parser_input_data_validator(input):

    def _html_validator(input):
        pass
        # allowed_types = ["image", "text", "break", "equation"]
        # for element in elements:
        #     type_key = {
        #         'key': 'type',
        #         'type': str,
        #         'validator': lambda item: item in allowed_types,
        #         'validator_error_message': f'type should be one of {", ".join(allowed_types)}'
        #     }
        #     value = {
        #         'key': 'value',
        #         'type': str,
        #     }
        #     width = {
        #         'key': 'width',
        #         'type': float,
        #     }
        #     height = {
        #         'key': 'height',
        #         'type': float,
        #     }
        #     dic_validetor(element, required_items=[type_key],  optional_items=[value, width, height], error_key='element',
        #                   many=False, container_name='element', raise_error_for_unexpected_key=True)

    if type(input) != list:
        raise serializers.ValidationError(
            'invalid format (list)')
    for item in input:
        index = {
            'key': 'index',
            'type': int,
        }
        text = {
            'key': 'text',
            'type': str,
        }
        question = {
            'key': 'question',
            'type': dict,
        }
        choices = {
            'key': 'choices',
            'type': list,
        }
        answer = {
            'key': 'answer',
            'type': dict,
        }
        tags = {
            'key': 'tags',
            'type': dict,
        }
        meta = {
            'key': 'meta',
            'type': dict,
        }

        dic_validetor(item, required_items=[question, index, text], optional_items=[choices, answer, tags, meta], error_key='content',
                      many=False, container_name='content', raise_error_for_unexpected_key=True)

        question_html = {
            'key': 'html',
            'type': str,
        }

        dic_validetor(item["question"], required_items=[question_html], error_key='question',
                      many=False, container_name='question', raise_error_for_unexpected_key=True)

        _html_validator(item["question"]["html"])

        if "answer" in item:
            answer_html = {
                'key': 'html',
                'type': str,
            }

            dic_validetor(item["answer"], optional_items=[answer_html], error_key='answer',
                          many=False, container_name='answer', raise_error_for_unexpected_key=True)
            if item.get("answer"):
                _html_validator(item["answer"]["html"])

        if "choices" in item:
            for choice in item['choices']:
                choice_html = {
                    'key': 'html',
                    'type': str,
                }
                choice_index = {
                    'key': 'index',
                    'type': str,
                }
                choice_is_correct = {
                    'key': 'is_correct',
                    'type': bool,
                }

                dic_validetor(choice, required_items=[choice_html, choice_index, choice_is_correct], error_key='choice',
                              many=False, container_name='choice', raise_error_for_unexpected_key=True)
                _html_validator(choice["html"])
    return input
