from datetime import datetime
from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse,HttpResponseRedirect
from django.views import generic
from django.utils.safestring import mark_safe
from datetime import timedelta,date
import calendar
from django.urls import reverse


from .models import *
from .utils import Calendar
from .forms import EventForm


def index(request):
    return render(request,'cal/index.html')

# def sign_up(request):
#     context = {}
#     form = UserCreationForm(request.POST or None)
#     if request.method == "POST":
#         if form.is_valid():
#             user = form.save()
#             login(request,user)
#             return HttpResponseRedirect(reverse('cal:calendar'))
#     context['form']=form
#     return render(request,'cal/signup.html',context)



class CalendarView(generic.ListView):
    model = Event
    template_name = 'cal/calendar.html'


    def get_event_for_user(self):
        
        return Event.objects.filter(user=self.request.user)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        # use today's date for the calendar
        d = get_date(self.request.GET.get('month', None))
        context['prev_month'] = prev_month(d)
        context['next_month'] = next_month(d)
        
       
        # Instantiate our calendar class with today's year and date
        cal = Calendar(d.year, d.month)
       
        # Call the formatmonth method, which returns our calendar as a table
        html_cal = cal.formatmonth(withyear=True)
      


        context['calendar'] = mark_safe(html_cal)
        context['object_list']=context['object_list'].filter(user=self.request.user)
        context['event_list']=context['event_list'].filter(user=self.request.user)

        
        return context

def get_date(req_day):
    if req_day:
        year, month = (int(x) for x in req_day.split('-'))
        print("jii",month)
        return datetime(year, month, day=1)
    return datetime.today()

def prev_month(d):
    first = d.replace(day=1)
    prev_month = first - timedelta(days=1)
    month = 'month=' + str(prev_month.year) + '-' + str(prev_month.month)
    return month

def next_month(d):
    days_in_month = calendar.monthrange(d.year, d.month)[1]
    last = d.replace(day=days_in_month)
    next_month = last + timedelta(days=1)
    month = 'month=' + str(next_month.year) + '-' + str(next_month.month)
    return month

def event(request, event_id=None):
    instance = Event()
  
    if event_id:
        instance = get_object_or_404(Event, pk=event_id)
    else:
        instance = Event()
  
    form = EventForm(request.POST or None,instance=instance)
    if request.POST and form.is_valid():
                
        event = form.save()
        event.user = User.objects.get(email=request.user.email)  
        
        event.save()
        return HttpResponseRedirect(reverse('cal:calendar'))
    return render(request, 'cal/event.html', {'form': form})


def deleteevent(request, event_id=None):
   Emp=Event()
   emp = get_object_or_404(Event, pk=event_id)
  
   emp.delete()
   return HttpResponseRedirect(reverse('cal:calendar'))


