__author__ = 'luissaguas'


#import json
from jnius import PythonJavaClass, java_method
import frappe, re, os


class FrappeTask(PythonJavaClass):
	__javainterfaces__ = ['IFrappeTask']

	def read_config(self):
		config = frappe.get_conf() or {}
		curr_site = os.path.join("currentsite.txt")
		config.default_site = frappe.read_file(curr_site) or frappe.local.site

		return config

	def conf(self):
		conf = self.read_config()
		return conf

	def __init__(self, task_id, result):
		super(FrappeTask, self).__init__()
		self.task_id = "Local-" + task_id
		self.result = result

	def get_hostname(self, url):
		if (not url): return None;
		if (url.index("://") > -1):
			url = url.split('/')[2]
			return url[0:url.index(":")] if (re.search(":", url)) else url

	def get_site_name(self):
		if (frappe.get_request_header('x-frappe-site-name')):
			return self.get_hostname(frappe.get_request_header('x-frappe-site-name'))

		conf = self.conf()
		if (frappe.get_request_header('host') in ['localhost', '127.0.0.1'] and conf.default_site):
			return conf.default_site

		if (frappe.get_request_header('origin')):
			return self.get_hostname(frappe.get_request_header('origin'))

		return self.get_hostname(frappe.get_request_header('host'))

	def setResult(self, result):
		self.result = result

	@java_method('()V')
	def setReadyTask(self):
		from frappe.async import emit_via_redis
		import frappe

		response = {}
		response.update({
			"status": "Success",
			"task_id": self.task_id,
			"result": self.result
		})
		sitename = self.get_site_name() or frappe.local.site
		emit_via_redis("task_status_change", response, sitename + ":task_progress:" + self.task_id)


"""

redis_server = None

def get_redis_server():
	global redis_server
	if not redis_server:
		from redis import Redis
		redis_server = Redis.from_url(conf.get("async_redis_server") or "redis://localhost:12311")
	return redis_server


def emit_via_redis(event, message, room):

	r = get_redis_server()

	try:
		r.publish('events', frappe.as_json({'event': event, 'message': message, 'room': room}))
	except redis.exceptions.ConnectionError:
		# print frappe.get_traceback()
		pass



"""
@@ -629,16 +629,45 @@
 n None;%0A
+%09%09print %22url %7B%7D%22.format(url)%0A
 %09%09if (ur
@@ -668,21 +668,20 @@
 if (url.
+f
 ind
-ex
 (%22://%22) 
@@ -716,17 +716,16 @@
 ')%5B2%5D%0A%09%09
-%09
 return u
@@ -737,13 +737,12 @@
 url.
+f
 ind
-ex
 (%22:%22
@@ -805,16 +805,133 @@
 (self):%0A
+%09%09print %22frappe.get_request_header('x-frappe-site-name') %7B%7D%22.format(frappe.get_request_header('x-frappe-site-name'))%0A
 %09%09if (fr

fix hostname bug