from __future__ import unicode_literals
from django.contrib import admin
from .models import Schedule
from .models import Customer
from .models import Couch
from .models import Cost
import datetime
import calendar
from django.urls import reverse
from calendar import HTMLCalendar
from django.utils.safestring import mark_safe
from .utils import EventCalendar


# Register your models here.

class ScheduleAdmin(admin.ModelAdmin):
    list_display = ['id', 'day', 'start', 'end', 'counter', 'duty', 'category']
    change_list_template = 'admin/schedule/change_list.html'

    def changelist_view(self, request, extra_context=None):
        after_day = request.GET.get('day__gte', None)
        extra_context = extra_context or {}

        if not after_day:
            d = datetime.date.today()
        else:
            try:
                split_after_day = after_day.split('-')
                d = datetime.date(year=int(split_after_day[0]), month=int(split_after_day[1]), day=1)
            except:
                d = datetime.date.today()

        previous_month = datetime.date(year=d.year, month=d.month, day=1)  # find first day of current month
        previous_month = previous_month - datetime.timedelta(days=1)  # backs up a single day
        previous_month = datetime.date(year=previous_month.year, month=previous_month.month,
                                       day=1)  # find first day of previous month

        last_day = calendar.monthrange(d.year, d.month)
        next_month = datetime.date(year=d.year, month=d.month, day=last_day[1])  # find last day of current month
        next_month = next_month + datetime.timedelta(days=1)  # forward a single day
        next_month = datetime.date(year=next_month.year, month=next_month.month,
                                   day=1)  # find first day of next month

        #extra_context['previous_month'] = reverse('admin:schedule_schedule_changelist') + '?day__gte=' + str(
        #    previous_month)
        #extra_context['next_month'] = reverse('admin:schedule_schedule_changelist') + '?day__gte=' + str(next_month)

        cal = EventCalendar()
        html_calendar = cal.formatmonth(d.year, d.month, withyear=True)
        html_calendar = html_calendar.replace('<td ', '<td  width="150" height="150"')
        extra_context['calendar'] = mark_safe(html_calendar)
        return super(ScheduleAdmin, self).changelist_view(request, extra_context)


class CustomerAdmin(admin.ModelAdmin):
    list_display = ['custID', 'name', 'birthday', 'address', 'doctorCert', 'certExpiration', 'category']


class CouchAdmin(admin.ModelAdmin):
    list_display = ['couchID', 'name']


class CostAdmin(admin.ModelAdmin):
    list_display = ['costID', 'category', 'money']


admin.site.register(Schedule, ScheduleAdmin)
admin.site.register(Customer, CustomerAdmin)
admin.site.register(Couch, CouchAdmin)
admin.site.register(Cost, CostAdmin)
