# -*- coding: UTF-8 -*-
# Copyright 2011-2018 Rumma & Ko Ltd
# License: BSD (see file COPYING for details)


from __future__ import unicode_literals
from builtins import str
import six

import datetime

from django.db import models
from django.db.models import Q
from django.conf import settings
from django.core.validators import MaxValueValidator
from django.core.validators import MinValueValidator
from django.core.exceptions import ValidationError
from django.utils import timezone

from lino import mixins
from lino.api import dd, rt, _, pgettext

from .choicelists import (
    DurationUnits, Recurrencies, Weekdays, AccessClasses, PlannerColumns)
from .utils import setkw, dt2kw, when_text

from lino.modlib.checkdata.choicelists import Checker
from lino.modlib.printing.mixins import TypedPrintable
from lino.modlib.printing.mixins import Printable
from lino.modlib.users.mixins import UserAuthored, Assignable
from lino_xl.lib.postings.mixins import Postable
from lino_xl.lib.outbox.mixins import MailableType, Mailable
from lino_xl.lib.contacts.mixins import ContactRelated
from lino.modlib.office.roles import OfficeStaff
from .workflows import (TaskStates, EntryStates, GuestStates)
from .actions import UpdateGuests
    
from .mixins import Component
from .mixins import EventGenerator, RecurrenceSet, Reservation
from .mixins import Ended
from .mixins import MoveEntryNext, UpdateEntries, UpdateEntriesByEvent
from .actions import ShowEntriesByDay

from .ui import ConflictingEvents

DEMO_START_YEAR = 2013


class CalendarType(object):

    def validate_calendar(self, cal):
        pass


class LocalCalendar(CalendarType):
    label = "Local Calendar"


class GoogleCalendar(CalendarType):
    label = "Google Calendar"

    def validate_calendar(self, cal):
        if not cal.url_template:
            cal.url_template = \
                "https://%(username)s:%(password)s@www.google.com/calendar/dav/%(username)s/"

CALENDAR_CHOICES = []
CALENDAR_DICT = {}


def register_calendartype(name, instance):
    CALENDAR_DICT[name] = instance
    CALENDAR_CHOICES.append((name, instance.label))

register_calendartype('local', LocalCalendar())
register_calendartype('google', GoogleCalendar())


class DailyPlannerRow(mixins.BabelDesignated, mixins.Sequenced):
    
    class Meta:
        app_label = 'cal'
        abstract = dd.is_abstract_model(__name__, 'PlannerRow')
        verbose_name = _("Planner row")
        verbose_name_plural = _("Planner rows")
        ordering = ['seqno']

    start_time = models.TimeField(
        blank=True, null=True,
        verbose_name=_("Start time"))
    end_time = models.TimeField(
        blank=True, null=True,
        verbose_name=_("End time"))


from lino.mixins.periods import ObservedDateRange
from etgen.html import E
from lino.utils import join_elems

class DailyPlannerRows(dd.Table):
    model = 'cal.DailyPlannerRow'
    column_names = "seqno designation start_time end_time"
    required_roles = dd.login_required(OfficeStaff)

class DailyPlanner(DailyPlannerRows):
    label = _("Daily planner")
    editable = False
    parameters = dict(
        date=models.DateField(
                _("Date"), help_text=_("Date to show")),
        user=dd.ForeignKey('users.User', null=True, blank=True))

    @classmethod
    def param_defaults(cls, ar, **kw):
        kw = super(DailyPlanner, cls).param_defaults(ar, **kw)
        kw.update(date=dd.today())