# coding: utf-8

'''フロントコントローラを提供する
'''

from math import ceil
import os

from flask import json
from flask import Flask
from flask import request
from flask import send_from_directory
from flask import render_template

# from json_loader import load_locations
# from json_loader import prepare_locations
from models import Location


# ページ毎のロケ地表示する
LOCATION_ITEMS_PER_PAGE = 20


app = Flask(__name__)
app.config['GOOGLE_API_KEY'] = os.environ['GOOGLE_API_KEY']
app.config['ROOT'] = (app.config['APPLICATION_ROOT']
                      if app.config['APPLICATION_ROOT'] else '')


@app.route('/static/<path:path>')
def send_js(path):
    return send_from_directory('static', path)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/location')
def location():
    req_title = request.args.get('title', None)
    try:
        req_page = int(request.args.get('page', 1))
    except ValueError as e:
        req_page = 1

    query = Location.selectbase()

    if req_title:
        query = query.where(Location.title ** '%{}%'.format(req_title))

    total_items = query.count()
    total_pages = ceil(total_items / LOCATION_ITEMS_PER_PAGE)

    current_page = req_page if req_page <= total_pages else total_pages

    query = query.paginate(current_page, LOCATION_ITEMS_PER_PAGE)

    locations = [l.as_dict() for l in query]

    return json.jsonify({
        'meta': {
            'pager_data': {
                'totalItems':  total_items,
                'totalPages':  total_pages,
                'currentItems': len(locations),
                'currentPage':  current_page,
                'itemsPerPage': LOCATION_ITEMS_PER_PAGE,
            },
        },
        'entities': {
            'locations': locations,
        },
    })


@app.route('/movie')
def movie():
    req_title = request.args.get('title', None)

    if not req_title:
        return json.jsonify([])

    query = (Location.select(Location.title)
                     .distinct()
                     .where(Location.title ** '%{}%'.format(req_title)))

    movies = [{'id': index, 'title': l.title} for index, l in enumerate(query)]

    return json.jsonify(movies)

[(24, 'flask.Flask', 'Flask', False, 'from flask import Flask\n'), (32, 'flask.send_from_directory', 'send_from_directory', False, 'from flask import send_from_directory\n'), (37, 'flask.render_template', 'render_template', False, 'from flask import render_template\n'), (42, 'flask.request.args.get', 'request.args.get', False, 'from flask import request\n'), (48, 'models.Location.selectbase', 'Location.selectbase', False, 'from models import Location\n'), (54, 'math.ceil', 'ceil', False, 'from math import ceil\n'), (80, 'flask.request.args.get', 'request.args.get', False, 'from flask import request\n'), (91, 'flask.json.jsonify', 'json.jsonify', False, 'from flask import json\n'), (83, 'flask.json.jsonify', 'json.jsonify', False, 'from flask import json\n'), (44, 'flask.request.args.get', 'request.args.get', False, 'from flask import request\n'), (85, 'models.Location.select', 'Location.select', False, 'from models import Location\n')]