from datetime import datetime

from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.template.context import RequestContext
from django.views.generic import list_detail
from django.shortcuts import render_to_response, get_object_or_404

from directory.models import *


def index(request):
    towns = Town.objects.filter(in_coverage=True)
    places = Place.objects.all()[:5]
    return render_to_response(
        "directory/index.html", locals(), context_instance=RequestContext(request)
    )


def people(request):
    people = Person.objects.all()[:10]
    return render_to_response(
        "directory/people.html", locals(), context_instance=RequestContext(request)
    )


def person_detail(request, slug):
    person = get_object_or_404(Person, slug=slug)
    return render_to_response(
        "directory/person_detail.html",
        locals(),
        context_instance=RequestContext(request),
    )


def place_detail(request, slug):
    place = get_object_or_404(Place, slug=slug)
    return render_to_response(
        "directory/place_detail.html",
        locals(),
        context_instance=RequestContext(request),
    )


def town_index(request):
    towns = Town.objects.filter(in_coverage=True)
    return render_to_response(
        "directory/town_index.html", locals(), context_instance=RequestContext(request)
    )


def town_detail(request, slug):
    town = get_object_or_404(Town, slug=slug)
    return render_to_response(
        "directory/town_detail.html", locals(), context_instance=RequestContext(request)
    )


def town_info(request, slug):
    tinfo = get_object_or_404(TownInfo, town__slug=slug)
    area_miles = round((tinfo.total_area * 0.0015625), 2)
    return render_to_response(
        "directory/town_info.html", locals(), context_instance=RequestContext(request)
    )


def school_index(request):
    schools = School.objects.all()
    return render_to_response(
        "directory/school_index.html",
        locals(),
        context_instance=RequestContext(request),
    )


def school_detail(request, slug):
    school = get_object_or_404(School, slug=slug)
    return render_to_response(
        "directory/school_detail.html",
        locals(),
        context_instance=RequestContext(request),
    )


def phonebook_index(request):
    towns = Town.objects.filter(in_coverage=True)
    entry_list = PhoneBookEntry.objects.all().order_by("entry")
    paginator = Paginator(entry_list, 100)
    try:
        page = int(request.GET.get("page", "1"))
    except ValueError:
        page = 1

    try:
        entries = paginator.page(page)
    except (EmptyPage, InvalidPage):
        entries = paginator.page(paginator.num_pages)
    return render_to_response(
        "directory/phonebook_index.html",
        locals(),
        context_instance=RequestContext(request),
    )


def phonebook_town(request, town_slug):
    towns = Town.objects.filter(in_coverage=True)
    town = get_object_or_404(Town, slug=town_slug)
    entries = PhoneBookEntry.objects.filter(town=town).order_by("entry")

    return render_to_response(
        "directory/phonebook_town.html",
        locals(),
        context_instance=RequestContext(request),
    )
