from Glocal import app
from flask import render_template, request, render_template_string
import collections
from Glocal.API import API

def setup_page_dict():
    """Make a dictionary of all the pages in the file for links at the top of
    the web page. Add the name and address of every new page here"""
    page_dict = collections.OrderedDict()
    page_dict['Home'] = '/'
    return page_dict

@app.route('/', methods=['GET', 'POST'])
def index_page():
    if request.method == 'GET':
        return render_template('home.html', title='Home',
                               page_dict=setup_page_dict(),
                               app_name=app.config['APP_NAME'])

    elif request.method == 'POST':
        st_address = request.form['st_address']
        city = request.form['city']
        state = request.form['state']
        miles = request.form['miles']
        latitude_form = request.form['latitude']
        longitude_form = request.form['longitude']
        if len(latitude_form) > 0 and len(longitude_form) > 0:
            user_query = API.GlocalAPI(st_address = st_address, city = city,
                                       state = state, miles = miles,
                                       latitude = latitude_form, longitude = longitude_form)
        else:
            user_query = API.GlocalAPI(st_address = st_address, city = city,
                                       state = state, miles = miles)
        lst_local_tweets, lst_trending_tweets = user_query.get_twitter()
        lst_local_insta = user_query.get_instagram()
        lst_four_square_trending, lst_four_square_explore = user_query.get_four_square()
        lst_events = user_query.get_events()
        actual_lat, actual_long = user_query.get_coordinates()
        return render_template('results.html', title='Home',
                               page_dict=setup_page_dict(),
                               app_name=app.config['APP_NAME'],
                               lst_local_tweets=lst_local_tweets,
                               lst_trending_tweets=lst_trending_tweets,
                               lst_local_insta=lst_local_insta,
                               lst_four_square_trending=lst_four_square_trending,
                               lst_four_square_explore=lst_four_square_explore,
                               lst_events=lst_events,
                               st_address=st_address,
                               city=city,
                               state=state,
                               latitude_form = latitude_form,
                               longitude_form = longitude_form,
                               actual_lat = actual_lat,
                               actual_long = actual_long)
