# -*- coding: utf-8 -*-

# pylint: disable=E1101

from datetime import date
from datetime import datetime

from marshmallow import pre_load
from marshmallow import post_load
from marshmallow import post_dump

from marshmallow.fields import Integer
from marshmallow.fields import Float
from marshmallow.fields import String
from marshmallow.fields import DateTime
from marshmallow.fields import Email
from marshmallow.validate import Length
from marshmallow.validate import Range

from amnesia.modules.content.validation import ContentSchema


class EventSchema(ContentSchema):
    ''' Schema for the Event model '''

    content_id = Integer()
    body = String()
    location = String(allow_none=True)
    address = String(allow_none=True)
    address_latitude = Float(allow_none=True)
    address_longitude = Float(allow_none=True)
    attendees = String(allow_none=True)
    contact_name = String(allow_none=True)
    contact_email = Email(allow_none=True)
    contact_phone = String(allow_none=True)
    country_iso = String(allow_none=True, validate=Length(equal=2))

    starts = DateTime(required=True, dump_only=True)
    ends = DateTime(required=True, dump_only=True)

    starts_year = Integer(load_only=True)
    starts_month = Integer(load_only=True, validate=Range(min=1, max=12))
    starts_day = Integer(load_only=True, validate=Range(min=1, max=31))
    starts_hour = Integer(load_only=True, allow_none=True,
                          validate=Range(min=0, max=23))
    starts_minute = Integer(load_only=True, allow_none=True,
                            validate=Range(min=0, max=55))

    ends_year = Integer(load_only=True)
    ends_month = Integer(load_only=True, validate=Range(min=1, max=12))
    ends_day = Integer(load_only=True, validate=Range(min=1, max=31))
    ends_hour = Integer(load_only=True, allow_none=True,
                        validate=Range(min=0, max=23))
    ends_minute = Integer(load_only=True, allow_none=True,
                          validate=Range(min=0, max=55))

    ########
    # LOAD #
    ########

    @post_load
    def load_event_dates(self, data):
        # Starts / Ends
        for part in ('starts', 'ends'):
            date_col = (part + '_year', part + '_month', part + '_day')
            time_col = (part + '_hour', part + '_minute')

            if all((data.get(i) for i in date_col)):
                col = [int(data[i]) for i in date_col]
                if all((data.get(i) for i in time_col)):
                    col.extend([int(data[i]) for i in time_col])

                data[part] = datetime(*col).isoformat()

        return data

    ########
    # DUMP #
    ########

    @post_dump(pass_original=True)
    def split_event_dates(self, data, orig):
        # Effective / Expiration dates
        date_col = ('year', 'month', 'day')
        datetime_col = ('year', 'month', 'day', 'hour', 'minute')

        for col in ('starts', 'ends'):
            value = getattr(orig, col)
            if isinstance(value, datetime):
                for i in datetime_col:
                    data['{}_{}'.format(col, i)] = getattr(value, i)
            elif isinstance(value, date):
                for i in date_col:
                    data['{}_{}'.format(col, i)] = getattr(value, i)

        return data

@@ -47,166 +47,8 @@
 01%0A%0A
-from datetime import date%0Afrom datetime import datetime%0A%0Afrom marshmallow import pre_load%0Afrom marshmallow import post_load%0Afrom marshmallow import post_dump%0A
 %0Afro
@@ -278,47 +278,8 @@
 ngth
-%0Afrom marshmallow.validate import Range
 %0A%0Afr
@@ -904,32 +904,16 @@
 red=True
-, dump_only=True
 )%0A    en
@@ -943,2123 +943,6 @@
 True
-, dump_only=True)%0A%0A    starts_year = Integer(load_only=True)%0A    starts_month = Integer(load_only=True, validate=Range(min=1, max=12))%0A    starts_day = Integer(load_only=True, validate=Range(min=1, max=31))%0A    starts_hour = Integer(load_only=True, allow_none=True,%0A                          validate=Range(min=0, max=23))%0A    starts_minute = Integer(load_only=True, allow_none=True,%0A                            validate=Range(min=0, max=55))%0A%0A    ends_year = Integer(load_only=True)%0A    ends_month = Integer(load_only=True, validate=Range(min=1, max=12))%0A    ends_day = Integer(load_only=True, validate=Range(min=1, max=31))%0A    ends_hour = Integer(load_only=True, allow_none=True,%0A                        validate=Range(min=0, max=23))%0A    ends_minute = Integer(load_only=True, allow_none=True,%0A                          validate=Range(min=0, max=55))%0A%0A    ########%0A    # LOAD #%0A    ########%0A%0A    @post_load%0A    def load_event_dates(self, data):%0A        # Starts / Ends%0A        for part in ('starts', 'ends'):%0A            date_col = (part + '_year', part + '_month', part + '_day')%0A            time_col = (part + '_hour', part + '_minute')%0A%0A            if all((data.get(i) for i in date_col)):%0A                col = %5Bint(data%5Bi%5D) for i in date_col%5D%0A                if all((data.get(i) for i in time_col)):%0A                    col.extend(%5Bint(data%5Bi%5D) for i in time_col%5D)%0A%0A                data%5Bpart%5D = datetime(*col).isoformat()%0A%0A        return data%0A%0A    ########%0A    # DUMP #%0A    ########%0A%0A    @post_dump(pass_original=True)%0A    def split_event_dates(self, data, orig):%0A        # Effective / Expiration dates%0A        date_col = ('year', 'month', 'day')%0A        datetime_col = ('year', 'month', 'day', 'hour', 'minute')%0A%0A        for col in ('starts', 'ends'):%0A            value = getattr(orig, col)%0A            if isinstance(value, datetime):%0A                for i in datetime_col:%0A                    data%5B'%7B%7D_%7B%7D'.format(col, i)%5D = getattr(value, i)%0A            elif isinstance(value, date):%0A                for i in date_col:%0A                    data%5B'%7B%7D_%7B%7D'.format(col, i)%5D = getattr(value, i)%0A%0A        return data
+)
 %0A

remove shit