from django.http import HttpResponse
from django.http import FileResponse
from django.shortcuts import render
from django.views import generic
from django.template import loader
from django.views.decorators.clickjacking import xframe_options_exempt
from django.contrib.auth.decorators import login_required

import caldav as cal

from datetime import datetime, timedelta, time

# Create your views here.

from mycaldav.models import *
from mycaldav.settings import *
from mycaldav.export_team_pdf import *


@xframe_options_exempt
def view_task_caldav(request):
    o_caldav = cls_caldav(url=caldav_cfg["task"])
    o_caldav.get_caldav_data()
    template = loader.get_template("task/task.html")
    context = {'latest_task_list':o_caldav.day, 'night_task_list': o_caldav.night}
    if (datetime.today().day == 1) or 2==1:
        myClient = cls_caldav_client()
        myClient.init_task_management()
    return HttpResponse(template.render(context,request))

@xframe_options_exempt
def view_task_edit_caldav(request, uuid):
    print(uuid)
    myClient = cls_caldav_client()
    myClient.mark_as_done_task(calandar=myClient.a_task,uuid=uuid)
    return view_task_caldav(request)

def view_op_edit_caldav(request, uuid):
    print(uuid)
    myClient = cls_caldav_client()
    myClient.mark_as_done_task(calandar=myClient.a_op,uuid=uuid)
    return view_op_caldav(request)

def view_vhc_edit_caldav(request, uuid):
    print(uuid)
    myClient = cls_caldav_client()
    myClient.mark_as_done_task(calandar=myClient.a_vhc,uuid=uuid)
    return view_vhc_caldav(request)

@xframe_options_exempt
def view_vhc_caldav(request):
    o_caldav = cls_caldav(url=caldav_cfg["vhc"])
    o_caldav.get_caldav_data(periode=2)
    template = loader.get_template("vhc/vhc_view.html")
    context = {'latest_task_list': o_caldav.items}
    return HttpResponse(template.render(context, request))

@xframe_options_exempt
def view_rh_caldav(request):
    o_caldav = cls_caldav(url=caldav_cfg["rh"])
    o_caldav.get_caldav_data(periode=2)
    template = loader.get_template("rh/rh_view.html")
    context = {'latest_task_list': o_caldav.items[::-1], 'today': datetime.today().strftime('%d.%m')}
    return HttpResponse(template.render(context, request))

@xframe_options_exempt
def view_road_caldav(request):
    o_caldav = cls_caldav(url=caldav_cfg["road"])
    o_caldav.get_caldav_data(periode=2)
    template = loader.get_template("road/road_view.html")
    context = {'latest_task_list': o_caldav.items[::-1]}
    return HttpResponse(template.render(context, request))
@xframe_options_exempt
def view_op_caldav(request):
    o_caldav = cls_caldav(url=caldav_cfg["op"])
    o_caldav.get_caldav_data(periode=1)
    template = loader.get_template("op/op_view.html")
    context = {'latest_task_list': o_caldav.items}
    return HttpResponse(template.render(context, request))

@xframe_options_exempt
def view_team_caldav(request):
    o_caldav = cls_caldav(url=caldav_cfg["team"])
    o_caldav.get_caldav_data(periode=3)
    template = loader.get_template("team/team_view.html")
    o_caldav.sort_array_by_key(True)

    context = {'latest_task_list': o_caldav.day, 'night_task_list': o_caldav.night, 'today': datetime.today().strftime('%d.%m'),'yesterday': (datetime.today() + timedelta(days=-1)).strftime('%d.%m'),'tomorow': (datetime.today() + timedelta(days=+1)).strftime('%d.%m')}
    return HttpResponse(template.render(context, request))

@xframe_options_exempt
def view_manif_caldav(request):
    o_caldav = cls_caldav(url=caldav_cfg["manif"])
    o_caldav.get_caldav_data(periode=1)
    template = loader.get_template("manif/manif_view.html")
    o_caldav.sort_array_by_key(True)
    context = {'latest_task_list': o_caldav.items, 'night_task_list': o_caldav.night, 'today': datetime.today().strftime('%d.%m')}
    return HttpResponse(template.render(context, request))

@xframe_options_exempt
def view_team_pdf_caldav(request):
    o_caldav = cls_caldav(url=caldav_cfg["team"])
    if not "dt" in request.GET.keys():
        start = datetime.today()
    else:
        start = datetime.strptime(request.GET['dt'], "%d.%m.%Y")

    myClient = cls_caldav_client()
    render_pdf(o_caldav, caldavClient=myClient, date=start)
    return  FileResponse(open('mycaldav/export.pdf', 'rb'), as_attachment=False, content_type='application/pdf')




@xframe_options_exempt
@login_required
def view_reskreen(request):
    template = loader.get_template("reskreen.html")
    #context = {'latest_task_list': o_caldav.day, 'night_task_list': o_caldav.night, 'today': datetime.today().strftime('%d.%m')}
    context = {}
    return HttpResponse(template.render(context, request))

from django_autologin.templatetags.django_autologin import automatic_login_token
@login_required
def view_generate_token(request):
    current_user = request.user
    auto_login = automatic_login_token(current_user)
    print(f"{current_user.username} autologin=> {auto_login}")
    return HttpResponse(f"{current_user.username}autologin=> {auto_login}")


