# Path: jasper_erpnext_report/utils/jasper_iter_hooks.py
# class JasperHooks:
# 	def __init__(self, hook_name, docname=None, fallback=None):
# 		self.hook_name = hook_name
# 		self.current = 0
# 		self.methods = frappe.get_hooks().get(self.hook_name) or (fallback if fallback is not None else [])
# 		if isinstance(self.methods, dict):
# 			if docname in self.methods.keys():
# 				self.methods = self.methods[docname]
# 			else:
# 				self.methods = fallback if fallback is not None else []
# 		self.methods_len = len(self.methods)
# 
# 
# 	def __iter__(self):
# 		return self
# 
# 	def next(self):
# 		if self.current >= self.methods_len:
# 			raise StopIteration
# 		else:
# 			return self.get_next_jasper_hook_method()
# 
# 	def get_next_jasper_hook_method(self):
# 		if self.methods_len > 0:
# 			curr_method = frappe.get_attr(self.methods[self.current])
# 			self.current += 1
# 			return curr_method
# 		return None


import frappe
import re
import jasper_erpnext_report
	import os, sys
	import sys
	import jasper_erpnext_report as jr
	import semantic_version as sv
from frappe import _
from frappe.modules.import_file import import_doc
from ast import literal_eval
from . cache import *
from . jasper_document import *
from . jasper_email import set_jasper_email_doctype
from jasper_erpnext_report.utils.jasper_iter_hooks import JasperHooks
	from jasper_erpnext_report.jasper_reports.ScriptletDefault import JasperCustomScripletDefault
	from datetime import timedelta

def get_hook_module(hook_name, report_name=None):
	"""
		check if there is a scriptlet hook for this report in frappe-bench/sites/site_name/jasper_hooks folder.
		The folder have the following structure where jasper_hooks is the root(package):
			jasper_hooks/report name/hook name.py
			Example: jasper_hooks/Table 1 Custom/jasper_scriptlet.py -> where Table 1 Custom is the name of report and jasper_scriptlet.py
			is the name of the hook.
		Note: All the folders must have __init__.py to make it a package
		This strucutre is to help development. There is no need to make a frappe app only to control reports.
	"""

	make_jasper_hooks_path()

	jasper_absolute_path = os.path.realpath(frappe.local.site_path)

	if jasper_absolute_path not in sys.path:
		sys.path.insert(0, jasper_absolute_path)

	report_name = report_name + "." if report_name else ""

	module_path = "jasper_hooks_" + frappe.local.site.replace(".", "_") + "." + report_name + hook_name
	try:
		hook_module = frappe.get_module(module_path)
	except:
		hook_module = None

	return hook_module

def jasper_run_method(hook_name, *args, **kargs):

# Path: jasper_erpnext_report/utils/jasper_iter_hooks.py
# class JasperHooks:
# 	def __init__(self, hook_name, docname=None, fallback=None):
# 		self.hook_name = hook_name
# 		self.current = 0
# 		self.methods = frappe.get_hooks().get(self.hook_name) or (fallback if fallback is not None else [])
# 		if isinstance(self.methods, dict):
# 			if docname in self.methods.keys():
# 				self.methods = self.methods[docname]
# 			else:
# 				self.methods = fallback if fallback is not None else []
# 		self.methods_len = len(self.methods)
# 
# 
# 	def __iter__(self):
# 		return self
# 
# 	def next(self):
# 		if self.current >= self.methods_len:
# 			raise StopIteration
# 		else:
# 			return self.get_next_jasper_hook_method()
# 
# 	def get_next_jasper_hook_method(self):
# 		if self.methods_len > 0:
# 			curr_method = frappe.get_attr(self.methods[self.current])
# 			self.current += 1
# 			return curr_method
# 		return None


# Path: jasper_erpnext_report/utils/utils.py
import frappe
import re
import jasper_erpnext_report
	import os, sys
	import sys
	import jasper_erpnext_report as jr
	import semantic_version as sv
from frappe import _
from frappe.modules.import_file import import_doc
from ast import literal_eval
from . cache import *
from . jasper_document import *
from . jasper_email import set_jasper_email_doctype
from jasper_erpnext_report.utils.jasper_iter_hooks import JasperHooks
	from jasper_erpnext_report.jasper_reports.ScriptletDefault import JasperCustomScripletDefault
	from datetime import timedelta


def get_hook_module(hook_name, report_name=None):
	"""
		check if there is a scriptlet hook for this report in frappe-bench/sites/site_name/jasper_hooks folder.
		The folder have the following structure where jasper_hooks is the root(package):
			jasper_hooks/report name/hook name.py
			Example: jasper_hooks/Table 1 Custom/jasper_scriptlet.py -> where Table 1 Custom is the name of report and jasper_scriptlet.py
			is the name of the hook.
		Note: All the folders must have __init__.py to make it a package
		This strucutre is to help development. There is no need to make a frappe app only to control reports.
	"""

	make_jasper_hooks_path()

	jasper_absolute_path = os.path.realpath(frappe.local.site_path)

	if jasper_absolute_path not in sys.path:
		sys.path.insert(0, jasper_absolute_path)

	report_name = report_name + "." if report_name else ""

	module_path = "jasper_hooks_" + frappe.local.site.replace(".", "_") + "." + report_name + hook_name
	try:
		hook_module = frappe.get_module(module_path)
	except:
		hook_module = None

	return hook_module

def jasper_run_method(hook_name, *args, **kargs):

	mlist = JasperHooks(hook_name)