import json
from django.shortcuts import render
from django.template import RequestContext
from django.http import Http404, HttpResponse
from django.core import serializers

from .models import LuxGallery, LuxImage
from locations.models import Country, Region

from utils.views import PaginatedListView
from django.views.generic import ListView
from django.views.generic.detail import DetailView


class PrivateGallery(DetailView):
    model = LuxGallery
    slug_field = "slug"
    template_name = "details/photo_gallery.html"


class PrivateGalleryList(PaginatedListView):
    template_name = 'archives/gallery_list.html'

    def get_queryset(self):
        return LuxGallery.objects.filter(is_public=False)

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(PrivateGalleryList, self).get_context_data(**kwargs)
        context['is_private'] = True
        return context


class Gallery(DetailView):
    model = LuxGallery
    slug_field = "slug"
    template_name = "details/photo_gallery.html"


class GalleryList(PaginatedListView):
    template_name = 'archives/gallery_list.html'

    def get_queryset(self):
        return LuxGallery.objects.filter(is_public=True)

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(GalleryList, self).get_context_data(**kwargs)
        context['is_private'] = False 
        return context


class OldGalleryList(PaginatedListView):
    template_name = 'archives/gallery_list.html'
    model = LuxGallery

    def get_queryset(self):
        return LuxGallery.objects.filter(is_public=True)

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(OldGalleryList, self).get_context_data(**kwargs)
        return context


class DailyPhotoList(PaginatedListView):
    template_name = 'archives/photo_daily_list.html'

    def get_queryset(self):
        return LuxImage.objects.filter(is_public=True, title__startswith="daily_")


def gallery_list(request, page):
    request.page_url = '/photos/%d/'
    request.page = int(page)
    context = {
        'object_list': LuxGallery.objects.all(),
        'page': page,
    }
    return render(request, "archives/photos.html", context)


def gallery(request, slug):
    context = {
        'object': LuxGallery.objects.get(set_slug=slug)
    }
    return render(request, 'details/photo_galleries.html', context)


def photo_json(request, slug):
    p = LuxGallery.objects.filter(set_slug=slug)
    return HttpResponse(serializers.serialize('json', p), mimetype='application/json')


def photo_preview_json(request, pk):
    p = LuxImage.objects.get(pk=pk)
    data = {}
    data['url'] = p.get_admin_image()
    data = json.dumps(data)
    return HttpResponse(data)


def thumb_preview_json(request, pk):
    p = LuxImage.objects.get(pk=pk)
    data = {}
    data['url'] = p.get_image_url_by_size('tn')
    data = json.dumps(data)
    return HttpResponse(data)


def gallery_list_by_area(request, slug, page):
    """Grabs entries by region or country"""
    request.page_url = '/photos/' + slug + '/%d/'
    request.page = int(page)
    try:
        region = Region.objects.get(slug__exact=slug)
        qs = LuxGallery.objects.filter(region=region).order_by('-id')
    except:
        region = Country.objects.get(slug__exact=slug)
        qs = LuxGallery.objects.filter(location__state__country=region).order_by('-id')
        if not region:
            raise Http404
    context = {
        'object_list': qs,
        'country_list': Country.objects.filter(visited=True),
        'region_list': Region.objects.all(),
        'region': region,
        'page': page
    }
    return render(request, "archives/photos.html", context)