# Path: pytripgui/plan_executor/executor.py
# class PlanExecutor:
#     def __init__(self, trip_config, logger=None):
#         self.trip_config = trip_config
#         self.logger = logger
# 
#     def check_config(self):
#         # TODO replace with function that actually runs trip, and collect returned errors
#         if self.trip_config.wdir_path == "":
#             return -1
#         if self.trip_config.trip_path == "":
#             return -1
#         return 0
# 
#     def execute(self, patient, plan):
#         plan = copy.deepcopy(plan)
# 
#         plan.working_dir = self.trip_config.wdir_path
#         plan.dedx_path = self.trip_config.dedx_path
#         plan.hlut_path = self.trip_config.hlut_path
# 
#         te = pte.Execute(patient.ctx, patient.vdx)
#         if self.trip_config.remote_execution:
#             te.remote = True
#             te.servername = self.trip_config.host_name
#             te.username = self.trip_config.user_name
#             te.password = self.trip_config.password
#             te.pkey_path = self.trip_config.pkey_path
#             te.remote_base_dir = self.trip_config.wdir_remote_path + '/'
# 
#         te.trip_bin_path = self.trip_config.trip_path + '/' + 'TRiP98'
# 
#         if self.logger:
#             te.add_executor_logger(self.logger)
# 
#         try:
#             te.execute(plan, use_default_logger=False)
#         except BaseException as e:
#             logger.error(e.__str__())
#             sys.exit(-1)
# 
#         results = SimulationResults(patient, plan, plan.basename)
# 
#         return results
# 
# Path: pytripgui/plan_executor/html_executor_logger.py
# class HtmlExecutorLogger(ExecutorLogger):
#     def __init__(self):
#         self._queue = Queue()
# 
#         self._info_tag = "<b>{}</b>"  # bold
#         self._red_font = "<font color=\"Red\">{}</font>"
#         self._error_tag = self._red_font.format("<b>{}</b>")  # red and bold
# 
#     def info(self, text):
#         self._queue.put(self._info_tag.format(text))
# 
#     def log(self, text):
#         text = self._format_tags(text)
#         text = self._format_ansi(text)
#         text = self._format_colors(text)
#         self._queue.put(text)
# 
#     def error(self, text):
#         self._queue.put(self._error_tag.format(text))
# 
#     def empty(self):
#         return self._queue.empty()
# 
#     def get(self):
#         return self._queue.get(block=False)
# 
#     def _format_tags(self, text):
#         """ Changes '<' and '>' in tags to html entities
#         """
#         text = text.replace("<E>", "&lt;E&gt;")  # error
#         text = text.replace("<SYS>", "&lt;SYS&gt;")  # system
#         text = text.replace("<I>", "&lt;I&gt;")  # info
#         text = text.replace("<D>", "&lt;D&gt;")  # debug
#         return text
# 
#     def _format_colors(self, text):
#         """ Changes lines with <E> or <SYS> to red color
#         """
#         if "&lt;E&gt;" in text or "&lt;SYS&gt;" in text:
#             text = self._red_font.format(text)
#         return text
# 
#     def _format_ansi(self, text):
#         """ Remove ansi escape sequences
#         """
#         import re
#         return re.sub(r"\033\[[0-9]+m", "", text)


from threading import Thread
from queue import Queue
from pytripgui.plan_executor.executor import PlanExecutor
from pytripgui.plan_executor.html_executor_logger import HtmlExecutorLogger


class ThreadedExecutor(Thread):
    def __init__(self, plan, patient, trip_config):
        super().__init__()

        self.logger = HtmlExecutorLogger()
        self.item_queue = Queue()

        self.patient = patient
        self.plan = plan
        self.trip_config = trip_config

    def run(self):

# Path: pytripgui/plan_executor/executor.py
# class PlanExecutor:
#     def __init__(self, trip_config, logger=None):
#         self.trip_config = trip_config
#         self.logger = logger
# 
#     def check_config(self):
#         # TODO replace with function that actually runs trip, and collect returned errors
#         if self.trip_config.wdir_path == "":
#             return -1
#         if self.trip_config.trip_path == "":
#             return -1
#         return 0
# 
#     def execute(self, patient, plan):
#         plan = copy.deepcopy(plan)
# 
#         plan.working_dir = self.trip_config.wdir_path
#         plan.dedx_path = self.trip_config.dedx_path
#         plan.hlut_path = self.trip_config.hlut_path
# 
#         te = pte.Execute(patient.ctx, patient.vdx)
#         if self.trip_config.remote_execution:
#             te.remote = True
#             te.servername = self.trip_config.host_name
#             te.username = self.trip_config.user_name
#             te.password = self.trip_config.password
#             te.pkey_path = self.trip_config.pkey_path
#             te.remote_base_dir = self.trip_config.wdir_remote_path + '/'
# 
#         te.trip_bin_path = self.trip_config.trip_path + '/' + 'TRiP98'
# 
#         if self.logger:
#             te.add_executor_logger(self.logger)
# 
#         try:
#             te.execute(plan, use_default_logger=False)
#         except BaseException as e:
#             logger.error(e.__str__())
#             sys.exit(-1)
# 
#         results = SimulationResults(patient, plan, plan.basename)
# 
#         return results
# 
# Path: pytripgui/plan_executor/html_executor_logger.py
# class HtmlExecutorLogger(ExecutorLogger):
#     def __init__(self):
#         self._queue = Queue()
# 
#         self._info_tag = "<b>{}</b>"  # bold
#         self._red_font = "<font color=\"Red\">{}</font>"
#         self._error_tag = self._red_font.format("<b>{}</b>")  # red and bold
# 
#     def info(self, text):
#         self._queue.put(self._info_tag.format(text))
# 
#     def log(self, text):
#         text = self._format_tags(text)
#         text = self._format_ansi(text)
#         text = self._format_colors(text)
#         self._queue.put(text)
# 
#     def error(self, text):
#         self._queue.put(self._error_tag.format(text))
# 
#     def empty(self):
#         return self._queue.empty()
# 
#     def get(self):
#         return self._queue.get(block=False)
# 
#     def _format_tags(self, text):
#         """ Changes '<' and '>' in tags to html entities
#         """
#         text = text.replace("<E>", "&lt;E&gt;")  # error
#         text = text.replace("<SYS>", "&lt;SYS&gt;")  # system
#         text = text.replace("<I>", "&lt;I&gt;")  # info
#         text = text.replace("<D>", "&lt;D&gt;")  # debug
#         return text
# 
#     def _format_colors(self, text):
#         """ Changes lines with <E> or <SYS> to red color
#         """
#         if "&lt;E&gt;" in text or "&lt;SYS&gt;" in text:
#             text = self._red_font.format(text)
#         return text
# 
#     def _format_ansi(self, text):
#         """ Remove ansi escape sequences
#         """
#         import re
#         return re.sub(r"\033\[[0-9]+m", "", text)


# Path: pytripgui/plan_executor/threaded_executor.py
from threading import Thread
from queue import Queue
from pytripgui.plan_executor.executor import PlanExecutor
from pytripgui.plan_executor.html_executor_logger import HtmlExecutorLogger



class ThreadedExecutor(Thread):
    def __init__(self, plan, patient, trip_config):
        super().__init__()

        self.logger = HtmlExecutorLogger()
        self.item_queue = Queue()

        self.patient = patient
        self.plan = plan
        self.trip_config = trip_config

    def run(self):

        self.on_execute_selected_plan_threaded()