import logging
log = logging.getLogger(__name__)

import json
import requests
import requests.exceptions
import botologist.plugin

BASE_URL = 'https://qdb.lutro.me'


def _get_quote_url(quote):
	return BASE_URL + '/' + str(quote['id'])


def _get_qdb_data(url, query_params):
	return requests.get(url, query_params, headers={'accept': 'application/json'}).json()


def _search_for_quote(quote):
	search = False
	query_params = None
	if isinstance(quote, int):
		url = BASE_URL+'/'+str(quote)
		single_quote = True
	else:
		single_quote = False
		if quote == 'random':
			url = BASE_URL+'/random'
		elif quote == 'latest':
			url = BASE_URL
		else:
			search = str(quote)
			url = BASE_URL+'/random'
			query_params = {'s': search}

	try:
		data = _get_qdb_data(url, query_params=query_params)
	except requests.exceptions.RequestException:
		log.warning('QDB request caused an exception', exc_info=True)
		return 'HTTP error!'

	if single_quote:
		quote = data['quote']
	else:
		quotes = data['quotes']
		if 'items' in quotes:
			quotes = quotes['items']
		if len(quotes) < 1:
			return 'No quotes found!'
		quote = quotes[0]

	url = BASE_URL+'/'+str(quote['id'])

	if len(quote['body']) > 160:
		body = quote['body']
		if search:
			try:
				body_len = len(body)
				substr_pos = body.lower().index(search.lower())
				start = body.rfind('\n', 0, substr_pos) + 1
				while body_len - start < 100:
					substr_pos = body.rfind('\n', 0, start - 1) + 1
					if body_len - substr_pos < 100:
						start = substr_pos
					else:
						break
				end = start + 150 - len(search)
			except ValueError:
				start = 0
				end = 100
		else:
			start = 0
			end = 100

		body = body.replace('\r', '').replace('\n', ' ').replace('\t', ' ')
		excerpt = body[start:end]

		if start > 0:
			excerpt = '[...] ' + excerpt
		if end < len(quote['body']):
			excerpt = excerpt + ' [...]'
		body = excerpt
	else:
		body = quote['body'].replace('\r', '').replace('\n', ' ').replace('\t', ' ')

	return url + ' - ' + body


class QdbPlugin(botologist.plugin.Plugin):
	@botologist.plugin.command('qdb')
	def search(self, cmd):
		'''Search for a quote, or show a specific quote.

		Examples: !qdb search for this - !qdb #220
		'''
		if len(cmd.args) < 1:
			arg = 'random'
		elif cmd.args[0][0] == '#':
			try:
				arg = int(cmd.args[0][1:])
			except ValueError:
				arg = ' '.join(cmd.args)
		else:
			arg = ' '.join(cmd.args)

		return _search_for_quote(arg)

	@botologist.plugin.http_handler(method='POST', path='/qdb-update')
	def quote_updated(self, body, headers):
		data = json.loads(body)
		quote = data['quote']
		if quote['approved']:
			return 'New quote approved! ' + _get_quote_url(quote)
		else:
			return 'Quote pending approval!'

@@ -1186,10 +1186,10 @@
 ) %3E 
-16
+40
 0:%0A%09
@@ -1385,17 +1385,17 @@
 start %3C 
-1
+3
 00:%0A%09%09%09%09
@@ -1475,17 +1475,17 @@
 r_pos %3C 
-1
+3
 00:%0A%09%09%09%09
@@ -1546,17 +1546,17 @@
 start + 
-1
+3
 50 - len
@@ -1610,17 +1610,17 @@
 %09%09end = 
-1
+3
 00%0A%09%09els
@@ -1644,17 +1644,17 @@
 %09%09end = 
-1
+3
 00%0A%0A%09%09bo

increase qdb cutoff