from django.utils import timezone
from django.shortcuts import render, redirect
from django.contrib import messages
from django.views.generic import View
from review.models import Review, Reply
from review.forms import ReplyForm
from gauth.models import Location
from review.review_utils import sync_all_review, reply_review
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


class ReviewListByLocationView(View):

    def get(self, request, location_id, *args, **kwargs):
        reviews = Review.objects.filter(location_id=location_id).order_by('-update_time')
        locations = Location.objects.all()
        page = request.GET.get('page', 1)
        paginator = Paginator(reviews, 50)
        try:
            reviews = paginator.page(page)
        except PageNotAnInteger:
            reviews = paginator.page(1)
        except EmptyPage:
            reviews = paginator.page(paginator.num_pages)
        context = {'reviews': reviews, 'locations': locations}

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


class ReviewListView(View):

    def get(self, request, *args, **kwargs):
        reviews = Review.objects.all().order_by('-update_time')
        locations = Location.objects.all()
        page = request.GET.get('page', 1)
        paginator = Paginator(reviews, 50)
        try:
            reviews = paginator.page(page)
        except PageNotAnInteger:
            reviews = paginator.page(1)
        except EmptyPage:
            reviews = paginator.page(paginator.num_pages)
        context = {'reviews': reviews, 'locations': locations}

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


class UnRepliedReviewList(View):

    def post(self, request, *args, **kwargs):
        form = ReplyForm(self.request.POST)
        if form.is_valid():
            reply = form.cleaned_data.get('reply')
        review_id = self.request.POST['review_id']
        review = Review.objects.filter(pk=review_id).first()
        now = timezone.now()
        if review:
            response = reply_review(review=review, replied_text=reply)
            if response.status_code == 200:
                review_reply = Reply(replied_text=reply, create_time=now)
                review_reply.save()
                review.reply = review_reply
                review.save()
                messages.success(request, f'Your reply has been posted!')
        return redirect('un-replied-review')

    def get(self, request, *args, **kwargs):
        now = timezone.now()
        form = ReplyForm()
        date = now - timezone.timedelta(days=30)
        reviews = Review.objects.filter(reply=None, update_time__gte=date).exclude(comment=None).order_by('update_time')
        return render(request, 'dashboard.html', {'reviews': reviews, 'form': form})


class ReportView(View):

    def get(self, request, *args, **kwargs):
        locations = Location.objects.all()
        return render(request, 'report.html', {'location_report': locations})


def sync_location(request, location_id):
    sync_all_review(location_id)
    return redirect('review-report')
