from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.http import HttpResponseNotFound, HttpResponse
from django.shortcuts import redirect
from django.utils.decorators import method_decorator
from django.views.generic.base import View, TemplateResponseMixin
from ldt.ldt_utils.models import Project, Content
#import logging
#import re

class MetadataComposerContextView(View):
    
    branding = "iri"
    
    def get_context_dict(self, request):
        context = {}
        context["branding"] = self.branding
        context["top_header_partial"] = "partial/%s_composer_header.html" % self.branding
        return context



class MetadataComposerHome(TemplateResponseMixin, MetadataComposerContextView):
    
    def get_template_names(self):
        return "metadatacomposer_home.html"
    
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(MetadataComposerHome, self).dispatch(*args, **kwargs)
    
    def get(self, request, branding="iri", **kwargs):
        self.branding = branding
        
        projects = Project.safe_objects.filter(owner=request.user).order_by('-modification_date')[:6]
        contents = Content.safe_objects.order_by('-update_date')[:6]
        
        context = self.get_context_dict(request)
        context.update({"projects":projects, "contents": contents})
        return self.render_to_response(context)



class MetadataComposerProjectList(TemplateResponseMixin, MetadataComposerContextView):
    
    def get_template_names(self):
        return "metadatacomposer_project_list.html"
    
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(MetadataComposerProjectList, self).dispatch(*args, **kwargs)
    
    def get(self, request, branding="iri", **kwargs):
        self.branding = branding
        page = request.GET.get("page") or 1
        
        projects = Project.safe_objects.prefetch_related("contents").order_by('-modification_date').filter(owner=request.user)
        nb = getattr(settings, 'METADATACOMPOSER_ELEMENTS_PER_PAGE', 9)
        if page=="x":
            nb = projects.count()
        
        paginator = Paginator(projects, nb)
        try:
            results = paginator.page(page)
        except (EmptyPage, InvalidPage):
            results = paginator.page(paginator.num_pages)
        
        context = self.get_context_dict(request)
        context.update({"results":results})
        return self.render_to_response(context)



class MetadataComposerResourceList(TemplateResponseMixin, MetadataComposerContextView):
    
    def get_template_names(self):
        return "metadatacomposer_resource_list.html"
    
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(MetadataComposerResourceList, self).dispatch(*args, **kwargs)
    
    def get(self, request, branding="iri", **kwargs):
        self.branding = branding
        
        # We get the first page of contents
        content_results = get_contents_and_projects(1, request.user)
        # We get the first page of images
        
        context = self.get_context_dict(request)
        context.update({"content_results":content_results})
        return self.render_to_response(context)



class MetadataComposerContentPagination(TemplateResponseMixin, MetadataComposerContextView):
    
    def get_template_names(self):
        return "partial/resource_content_list.html"
    
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(MetadataComposerContentPagination, self).dispatch(*args, **kwargs)
    
    def get(self, request, branding="iri", **kwargs):
        self.branding = branding
        page = request.GET.get("page") or 1
        
        content_results = get_contents_and_projects(page, request.user)
        
        context = self.get_context_dict(request)
        context.update({"content_results":content_results})
        return self.render_to_response(context)



def get_contents_and_projects(page, user):
    
    # We get the current's page contents
    contents = Content.safe_objects.order_by('-update_date')
    
    nb = getattr(settings, 'METADATACOMPOSER_ELEMENTS_PER_PAGE', 9)
    if page=="x":
        nb = contents.count()
    
    paginator = Paginator(contents, nb)
    try:
        results = paginator.page(page)
    except (EmptyPage, InvalidPage):
        results = paginator.page(paginator.num_pages)
    
    # We add the user's projects for each content
    results_object_list = []
    for content in results.object_list:
        projects = content.project_set.all().filter(owner=user)
        results_object_list.append({"content":content, "projects":projects})
    results.object_list = results_object_list
    
    return results
    