import random
import json

from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django.views.generic.base import View, TemplateView
from django.http import HttpResponse
from django.db.models import Q
from django.contrib.sites.models import Site
from django.conf import settings

from booking.models import DEFAULT_PEOPLE__LIST, DEFAULT_CHILDREN_LIST, DEFAULT_ROOM_LIST
from .models import Property, PropertyWeb, PropertyWebPhoto, ExtraService, PropertyWebContact
from .models import PLACE_TYPES
from availability.models import Availability
from core.views import Search
from core.utils import generate_condition_message, generate_deposit_message, generate_children_message

class PropertyViewComentarios(Search, TemplateView):
    template_name = 'property/encuestas_property.html'

    def is_search(self, kwargs):
        if 'start' in kwargs and 'end' in kwargs:
            return True
        else:
            # data can go in kwargs or in request.GET
            g = self.request.GET
            return 'start' in g and 'end' in g

    def get_data(self):
        d = self.kwargs
        g = self.request.GET
        if 'start' not in d:
            d = g
        query = {
            'start': d.get('start', ''),
            'end': d.get('end', ''),
            'people': d.get('people', ''),
            'children': d.get('children'),
        }
        return query

    def query_avail(self, ctx, start, end=None, mode='range'):
        prop = ctx['prop'].id
        avail = []
        if mode == 'range':
            avail = Availability.objects.filter(date__range=[start, end],
                                                property_id=prop,
                                                rooms_to_shell__gt=0,
                                                availability__in=['0', '3'])
        elif mode == 'normal':
            avail = Availability.objects.filter(date=start,
                                                property_id=prop,
                                                rooms_to_shell__gt=0,
                                                availability__in=['0', '3'])
        elif mode == 'gte':
            avail = Availability.objects.filter(date__gte=start,
                                                property_id=prop,
                                                rooms_to_shell__gt=0,
                                                availability__in=['0', '3'])
        return avail

    def get_context_data(self, *args, **kwargs):
        from encuesta.models import Pregunta, Respuesta
        ctx = super(Search, self).get_context_data(*args, **kwargs)
        ctx['prop'] = get_object_or_404(Property, slug=kwargs['slug'])
        ctx['imgs'] = PropertyWebPhoto.objects.filter(Q(web__prop=ctx['prop']))
        ctx['extras'] = ExtraService.objects.filter(Q(prop=ctx['prop']))
        ctx['contact'] = PropertyWebContact.objects.filter(Q(web__prop=ctx['prop']))[:1]
        ctx['condition_message'] = generate_condition_message(property=ctx['prop'])
        ctx['deposit_message'] = generate_deposit_message(property=ctx['prop'])
        ctx['children_message'] = generate_children_message(property=ctx['prop'])
        ctx['gmaps_apikey'] = settings.GMAPS_APIKEY
        ctx['is_search'] = self.is_search(kwargs)
        ctx['pregunta'] = Pregunta.objects.all()[0]
        ctx['comentarios'] = Respuesta.objects.filter(pregunta__id=Pregunta.objects.all()[0].id, respuesta_customer_fecha_responde_date__isnull=False,
                                     respuesta_eliminada=False, prop__id=ctx['prop'].id, respuesta_comentario_text__isnull=False).order_by('-respuesta_customer_fecha_responde_date')
        if ctx['is_search']:
            ctx.update(self.search_availability(ctx))
        else:
            rooms = ctx['prop'].rooms.all()
            ctx['rooms'] = rooms

        ctx['people_list'] = DEFAULT_PEOPLE__LIST
        ctx['children_list'] = DEFAULT_CHILDREN_LIST
        ctx['rooms_list'] = DEFAULT_ROOM_LIST
        ctx['prop'] = ctx['prop']
        return ctx

