from django.template import RequestContext, Context, loader
from django.http import HttpResponse
from django.db.models import Q
from django.shortcuts import render_to_response, render, get_object_or_404
from django.template.loader import render_to_string
from django.contrib.auth import login, logout, authenticate
from ox.django.shortcuts import render_to_json_response
import json
import re
from os.path import join
from place import *
import api_views
import datetime
import isodate
import settings
import instance_settings
from gazetteer.shortcuts import get_place_or_404
from django.views.decorators.csrf import csrf_exempt
from helpers import get_settings

def index(request):
    places_count = Place.objects.count("*")
    context = RequestContext(request, {
        'total_count': places_count
    return render_to_response("index.html", context)

def backbone(request):
    instance_name = instance_settings.SHORT_NAME
    instance_templates_base = join('instance_templates', instance_name)
    footer_template = join(instance_templates_base, 'footer.html')
        footer_content = render_to_string(footer_template)
        footer_content = ''
    context = {
        'site_title': instance_settings.SITE_TITLE,
        'app_base': instance_settings.APP_BASE,
        'footer': footer_content,
        'debug': settings.DEBUG
    return render(request, "backbone.html", context)

#FIXME: move to models
    ("0", 'None'),
    ("1", 'One Day'),
    ("7", 'One Week'),
    ("30", 'One Month'),
    ("365", 'One Year'),
    ("3650", 'One Decade'),

def detail(request, place_id):
    place = get_place_or_404(place_id)
    if place.updated:
        updated = isodate.isodates.parse_date(place.updated)
        updated = None
    geojson = json.dumps(place.to_geojson())

    similar_geojson = api_views.similar(request, place_id).content 

    similar_places = json.loads(similar_geojson)
    if place.feature_code:
            feature_code = FeatureCode.objects.get(typ=place.feature_code)
            feature_code = "Invalid"
        feature_code = "None"

    revisions_json = api_views.history(request, place_id).content
    revisions = json.loads(revisions_json)

    #FIXME: move get_wms_layers to a model method or so
    WARPER_URLS = ['']
    wms_layers = []
    for uri in place.uris:
        for warper_url in WARPER_URLS:
            if uri.startswith(warper_url):
                regex = re.compile(r'' + warper_url + '/warper/layers/([0-9]*)\..*$')
                if regex.match(uri):
                    warper_id = regex.findall(uri)[0]
                    wms_layer = warper_url + "/warper/layers/wms/" + warper_id

    if place.relationships is not None:
        place.relationships = [{'type': obj['type'], 'place': Place.objects.get(obj['id'])} for obj in place.relationships]

    context = RequestContext(request, {
        'place': place,
        'updated': updated,
        'place_geojson': geojson,
        'feature_code': feature_code,
        'similar_places': similar_places,
        'similar_geojson': similar_geojson,
        'revisions': revisions,
        'revisions_json': revisions_json,
        'wms_layers': json.dumps(wms_layers),
    return render_to_response("detail.html", context)

def login_json(request):
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    user = authenticate(username=username, password=password)
    if user is not None:
        login(request, user)
        user_json = {
            'username': user.username
        return render_to_json_response({'success': 'User logged in', 'user': user_json})
    return render_to_json_response({'error': 'Username / password do not match'})
def logout_json(request):
    return render_to_json_response({'success': 'User logged out'})    

def user_json(request):
    if request.user.is_authenticated():
        user_json = {
            'username': request.user.username
        user_json = {}   
    return render_to_json_response({'user': user_json, 'settings': get_settings()}) 

def edit_place(request, place_id):
    place = get_place_or_404(place_id)
    geojson = json.dumps(place.to_geojson())
    context = RequestContext(request, {
        'place': place,
        'place_geojson': geojson
    return render_to_response("edit_place.html", context)  

def new(request):
    place_obj  =  {
                "relationships": [],"admin": [], "name": "", 
                "geometry": {}, "is_primary": True,
                "uris": [], "feature_code": "ADM1",
                "centroid": [], "timeframe": {}, "is_composite": False
    place = Place(place_obj)
    geojson = json.dumps(place.to_geojson())
    context = RequestContext(request, {
        'place': place,
        'place_geojson': geojson
    return render_to_response("new.html", context)