# coding: utf-8
from __future__ import absolute_import

from spyne.model.complex import Array
from spyne.model.complex import ComplexModel
from spyne.model.primitive import Boolean
from spyne.model.primitive import Date
from spyne.model.primitive import DateTime
from spyne.model.primitive import Integer
from spyne.model.primitive import String
from spyne.model.primitive import Unicode

from .utils import create_attachment


class Item(ComplexModel):

    __type_name__ = 'item'

    count = Integer  # Кол-во входов за период

    teacher_id = Integer
    fullname = Unicode

    date = String
    times = String

    control_unit = Unicode
    control_unit_id = Integer

    school_id = Integer
    school_shortname = Unicode
    school_name = Unicode

    class Annotations(ComplexModel.Annotations):

        doc = u'Контейнер, содержащий данные сотрудника.'


class Root(ComplexModel):

    __type_name__ = 'root'

    employees = Array(Item)

    class Annotations(ComplexModel.Annotations):

        doc = u'Контейнер, содержащий список сотрудников.'


class Direction(ComplexModel):

    u"""Модель направления."""

    __type_name__ = 'item'

    ministry_id = Integer
    ministry_full_name = Unicode

    administration_id = Integer
    administration_full_name = Unicode

    unit_id = Integer
    unit_full_name = Unicode
    unit_short_name = Unicode

    id = Integer
    name = Unicode

    group_count = Integer
    pupil_count = Integer


class Mark(ComplexModel):

    u"""Модель оценки."""

    __type_name__ = 'item'

    pupil_id = Integer
    pupil_fio = Unicode
    date = DateTime


class Lesson(ComplexModel):

    u"""Модель занятия."""

    __type_name__ = 'item'

    unit_id = Integer
    unit_full_name = Unicode
    unit_short_name = Unicode
    direction_id = Integer
    direction_name = Unicode

    group_id = Integer
    group_pupil_count = Integer
    has_lessons = Boolean
    lesson_id = Integer
    lesson_date = Date
    lesson_held = Boolean
    teacher_id = Integer
    teacher_name = Unicode
    subject_id = Integer
    subject_name = Unicode
    hours_count = Integer
    mark_2_count = Integer
    mark_3_count = Integer
    mark_4_count = Integer
    mark_5_count = Integer
    pupils_visited = Integer

    marks = Array(Mark)

    homework = Boolean


class UnitsRoot(ComplexModel):

    __type_name__ = 'root'

    directions = Array(Direction)
    lessons = Array(Lesson)

    class Annotations(ComplexModel.Annotations):

        doc = u'Контейнер, содержащий информацию об учреждении.'


class GetIndexResponse(ComplexModel):

    __type_name__ = 'GetIndexResponse'

    _type_info = [
        ('IndexFilename', String),
        ('IndexBinaryData', Unicode),
    ]

    @classmethod
    def create(cls, file_name, content):
        content = create_attachment(file_name, content)
        return cls(
            IndexFilename=file_name, IndexBinaryData=content
        )

    class Annotations(ComplexModel.Annotations):

        doc = u'Ответ на запрос GetIndexRequest'


class IndexPeriod(ComplexModel):

    IndexPeriodType = Unicode(max_occurs=1)
    IndexPeriodDate = Unicode(max_occurs=1)
    IndexPeriodStartDate = Unicode(max_occurs=1)
    IndexPeriodEndDate = Unicode(max_occurs=1)

    class Annotations(ComplexModel.Annotations):

        doc = u'Данные периода выборки.'


class BuildIndexResponse(ComplexModel):

    __type_name__ = 'BuildIndexResponse'

    _type_info = [
        ('IndexIdent', String),
    ]

    @classmethod
    def create(cls, report_uid):
        return cls(
            IndexIdent=report_uid
        )

    class Annotations(ComplexModel.Annotations):

        doc = u'Ответ на запрос BuildIndexRequest'


class GetBuildedIndexResponse(ComplexModel):

    __type_name__ = 'GetBuildedIndexResponse'

    _type_info = [
        ('IndexFilename', String),
        ('IndexBinaryData', Unicode),
    ]

    @classmethod
    def create(cls, file_name, content):
        if file_name:
            content = create_attachment(file_name, content)
        return cls(
            IndexFilename=file_name or '',
            IndexBinaryData=content or '',
        )

    class Annotations(ComplexModel.Annotations):

        doc = u'Ответ на запрос GetBuildedIndexRequest'
