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

import json

from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseForbidden, HttpResponse
from django.shortcuts import render
from django.utils import timezone
from django.utils.translation import ugettext as _
from django.views.decorators.http import require_POST, require_GET
from pytz import timezone as timezone_pytz

from courses.models import Course
from groups.models import Group
from mail.common import EmailRenderer
from mail.models import Message
from users.model_user_status import get_statuses
from users.models import UserProfile

MONTH = {
    1: _(u"january"),
    2: _(u"february"),
    3: _(u"march"),
    4: _(u"april"),
    5: _(u"may"),
    6: _(u"june"),
    7: _(u"july"),
    8: _(u"august"),
    9: _(u"september"),
    10: _(u"october"),
    11: _(u"november"),
    12: _(u"december")
}


@require_GET
@login_required
def mail_page(request):
    user = request.user
    user_profile = user.profile

    users_from_staff_len = {}
    if user.is_staff and 'from_staff' in request.GET and 'user_ids_send_mail_counter' in request.session:
        key = 'user_ids_send_mail_' + request.GET['from_staff']
        if key in request.session:
            users_from_staff_len = {
                'index': request.GET['from_staff'],
                'length': len(request.session[key]),
            }

    if user.is_staff:
        courses_teacher = Course.objects.filter(is_active=True)
    else:
        courses_teacher = Course.objects.filter(teachers=user, is_active=True)

    context = {
        "user": user,
        "user_profile": user_profile,
        "courses_teacher": courses_teacher,
        'user_statuses': get_statuses(),
        "users_from_staff_len": users_from_staff_len,
        "snow_alert_message_fulltext": hasattr(settings, 'SEND_MESSAGE_FULLTEXT') and settings.SEND_MESSAGE_FULLTEXT,
    }

    return render(request, 'mail.html', context)


@require_GET
@login_required
def ajax_get_mailbox(request):
    response = dict()
    user = request.user
    user_profile = user.profile

    datatable_data = dict(request.GET)

    if "draw" not in datatable_data:
        return HttpResponseForbidden()

    if "make_read[]" in datatable_data:
        if datatable_data["make_read[]"][0] == "all":
            user_profile.unread_messages.clear()
            user_profile.send_notify_messages.clear()
        else:
            user_profile.unread_messages = list(
                user_profile.unread_messages
                .exclude(id__in=datatable_data["make_read[]"])
                .values_list("id", flat=True)
            )
            user_profile.send_notify_messages = list(
                user_profile.send_notify_messages
                .exclude(id__in=datatable_data["make_read[]"])
                .values_list("id", flat=True)
            )
    if "make_unread[]" in datatable_data:
        user_profile.unread_messages.add(*Message.objects.filter(id__in=datatable_data["make_unread[]"]))
    if "make_delete[]" in datatable_data:
        user_profile.deleted_messages.add(*Message.objects.filter(id__in=datatable_data["make_delete[]"]))
    if "make_undelete[]" in datatable_data:
        user_profile.deleted_messages = list(
            user_profile.deleted_messages
            .exclude(id__in=datatable_data["make_undelete[]"])
            .values_list("id", flat=True)
        )

    messages = Message.objects.none()