from django.shortcuts import render
from django.http import HttpResponse, Http404
from .models import Image, Location, Category

# Create your views here.

def index(request):
    title = 'Home'
    images = Image.get_all_images()
    locations = Location.objects.all()
    return render(request, 'index.html', {'title':title, 'images':images, 'locations':locations})

def single_image(request, image_id):
    image = Image.get_image_by_id(image_id)
    return render(request, 'single_image.html', {"image":image})

# def single_image(request, category_name, image_id):
#      # print(image_category)
#     locations = Location.objects.all()

#     image = Image.get_image_by_id(image_id)
#     # Get category name
#     # print(category_name)
#     image_category = Image.objects.filter(category__photo_category = category_name)
#     title = f'{category_name}'
#     return render(request,'single_image.html',{'title':title, 'image':image, 'image_category':image_category, 'locations':locations})

def location_filter(request):
    # locations = Location.objects.all()
    images = Image.filter_by_location()
    # title = f'{location} Photos'
    return render(request, 'location.html', {"images":images,})

def get_category(request, category):
    image_category = Image.objects.filter(category = category)
    # image = Image.filter_category(category)
    return render(request, 'category.html', locals())

def search(request):

    if 'image' in request.GET and request.GET["image"]:
        search_term = request.GET.get("image")
        searched_images = Image.search_by_category(search_term)
        message = f"{search_term}"

        return render(request, 'search.html',{"message":message,"images": searched_images})

    else:
        message = "You haven't searched for any term"
        return render(request, 'search.html',{"message":message})

