from flask import render_template, request, jsonify, send_from_directory
from www import app
import json, logging
from selection import sel
import urllib.parse
import urllib.request

LISTSERVS_URL = "http://127.0.0.1:5555"
lists_to_serve = []

@app.route('/')
def index():
	l = sel.lists()
	return render_template("index.html", lists=l, tags=sel.tags())

@app.route('/selection')
def selection():
	s = sel.format_selection()
	return render_template("selection.html", sel=s)


@app.route('/tags')
def tags():
	return jsonify(result=sel.tags())

@app.route('/report')
def report():
	return jsonify(report=sel.report())

# @app.route('/favicon.ico')
# def favicon():
#     return send_from_directory(os.path.join(app.root_path, 'static'),
#                                'favicon.ico', mimetype='image/vnd.microsoft.icon')	

@app.route('/collect')
def collect():
	url = request.args.get('url')
	tag = request.args.get('tags')
	li = request.args.get('list')

	msg = "Added " + url + " to " + tag
	commited = sel.commit_dump(li, url, tag)
	if not commited:
		msg = url + " already exists..."
		commited = []

	return jsonify({ "msg": msg, "report": sel.report(), "commited": commited, "tag": tag})


def filter_results_selection(res):
	h = sel.hashmap()
	for r in res:
		for rr in r['results']:
			for rrr in rr['hits']:
				if rrr['url'] in h:
					print(rrr['url'] + " in " + h[rrr['url']])
					rrr['sel'] = h[rrr['url']]

@app.route('/search')
def searh():

	global lists_to_serve
	
	if len(request.args) < 1:
		# q: list all table or keep predefined wconfig.lists_to_serve?
		# wconfig.lists_to_serve = archive.list_tables_db(config.db['database'], config.db['host'], config.db['user'], config.db['password'])
		r = urllib.request.urlopen(LISTSERVS_URL + '/lists')
		data = json.loads(r.read().decode('utf8'))
		lists_to_serve = data['lists']

		return render_template("search.html", archives=lists_to_serve, fields=['content', 'from'])

	k_arg = request.args.get('keyword')
	l_arg = request.args.get('list')
	f_arg = request.args.get('field')

	if k_arg is None or k_arg.strip() == '':
		return "no keyword..."
	
	if l_arg != "all" and l_arg not in lists_to_serve:
		return "list '" + l_arg + "' does not exist"

	if f_arg not in ['content', 'from']:
		return "field '" + f_arg + "' does not exist"

	data = {'keyword': k_arg, 'list': l_arg, 'field': f_arg}
	val = urllib.parse.urlencode(data)

	URL = LISTSERVS_URL + '/search?' + val
	
	logging.info("search keyword = " + k_arg)
	r = urllib.request.urlopen(LISTSERVS_URL + '/search?' + val)
	data = json.loads(r.read().decode('utf8'))

	filter_results_selection(data['result'])

	return jsonify({'result': data['result'], 'tags': sel.tags()})
	


