from django.shortcuts import render
from .models import Adress, Publication, Ruta, SignUp
from django.contrib.gis.geos import Point, LineString #para obtener las coordenadas
from .forms import SignUpForm, RutaForm

# Create your views here.
def homepage(request):
    return render(request, "homepage.html")

def content(request):
    return render(request, "content.html")

def signup(request):
	title = 'Hi there!'
	form = SignUpForm(request.POST or None)
	context = {
	'title': title,
	'form': form,
	}

	if form.is_valid():
		instance = form.save(commit=False)
		instance.save()
		
		context = {
		'title': 'Great!',
		}

	return render(request, "signup.html", context)

def addroute(request):
	form = RutaForm()
	context = {
	'form': form,
	}
	return render(request, "addroute.html", context)

def publications(request):
	pub_list = Publication.objects.all()
	if pub_list.count() == 0:
		message = 'Join Mapdex'
		context = {
		'message': message
		}
		return render(request, "publications.html", context)
	else:
		print(str(len(pub_list))+' objects in pub_list')
		context = {
		'pub_list': pub_list,
		}
		return render(request, "publications.html", context)

def after_logout(request):
		return render(request, "logout.html")

def profile(request):
	log=request.user.id
	pub_list = Publication.objects.filter(id=log)
	if pub_list.count() == 0:
		message = 'Go ahead and create some routes!'
		context = {
		'message': message
		}
		return render(request, "profile_pubs.html", context)
	else:
		print(str(len(pub_list))+' objects in pub_list')
		context = {
		'pub_list': pub_list,
		}
		return render(request, "profile_pubs.html", context)	

def map(request):
	log=request.user
	context = {}
	
	if Adress.objects.all().count()==0:
		adress_empty = 'No adresses'
		print(adress_empty)
		context.update({'adress_empty': adress_empty})
	else:
		#Gets the coordinates to put a marker on the map	
		coordenadas = Adress.objects.filter(id=1).values('punto')[0].get('punto')
		lon = Point.get_x(coordenadas)
		print(lon)
		lat = Point.get_y(coordenadas)
		print(lat)
		#Gets the reverted coordinates of one route
		context.update({'lon': lon})
		context.update({'lat': lat})
	
	if Ruta.objects.all().count()==0:
		ruta_empty = 'No routes'
		print(ruta_empty)
		context.update({'ruta_empty': ruta_empty})
	else:
		coordenadas_ruta = Ruta.objects.filter(id=1).values('ruta')[0].get('ruta')
		lista = []
		for item in coordenadas_ruta.coords:
			longitud = item[0]
			latitud = item[1]
			formato = str('new L.LatLng'+'('+str(latitud)+','+str(longitud)+'),')
			lista.append(formato)
		#Creates a list of all route-coordinates in database
		route_list = []
		for i in Ruta.objects.all():
			value = len(route_list)
			route_list.append(Ruta.objects.all().values('ruta')[value].get('ruta'))
		print(str(len(route_list))+' objects in route_list')
		
		#Reverted list of all route-coordinates in database
		reverted_all_list_coords = []
		for item in route_list:
			one_list_coords = []
			for item in item:	
				longitud = item[0]
				#print(longitud)
				latitud = item[1]
				#print(latitud)
				formato = str('new L.LatLng'+'('+str(latitud)+','+str(longitud)+'),')
				one_list_coords.append(formato)
				#print('nuevo')
			#print('-----------')
			reverted_all_list_coords.append(one_list_coords)
		print(str(len(reverted_all_list_coords))+' objects in reverted_all_list_coords')
		context.update({'lista': lista})
		context.update({'route_list': route_list})
		context.update({'reverted_all_list_coords': reverted_all_list_coords})
	#print(context)
	return render(request, "map.html", context)


