# Path: pytripgui/plan_executor/simulation_results.py
# class SimulationResults:
#     def __init__(self, patient, plan, name):
#         self.patient = patient
#         self.plan = plan
#         self.name = name
#         self.volume_histograms = {}
# 
#         self._compute_target_dvh()
#         self._compute_target_lvh()
# 
#     def get_doses(self):
#         return self.plan.dosecubes
# 
#     def get_dose(self, dose_type):
#         if dose_type not in DosCube.allowed_suffix:
#             raise ValueError("Wrong dose type")
# 
#         for dose in self.plan.dosecubes:
#             if dose.basename.endswith("." + dose_type):
#                 return dose
#         return None
# 
#     def get_lets(self):
#         return self.plan.letcubes
# 
#     def import_dos(self, dos_path):
#         logger.debug("Open DosCube {:s}".format(dos_path))
#         dos = DosCube()
#         dos.read(dos_path)
#         self.plan.dosecubes.append(dos)
# 
#     def import_let(self, let_path):
#         logger.debug("Open LETCube {:s}".format(let_path))
#         let = LETCube()
#         let.read(let_path)
#         self.plan.letcubes.append(let)
# 
#     def get_let(self, let_type):
#         if let_type not in LETCube.allowed_suffix:
#             raise ValueError("Wrong LET type")
# 
#         for let in self.plan.letcubes:
#             if let.basename.endswith("." + let_type):
#                 return let
#         return None
# 
#     def _compute_target_dvh(self):
#         for dose in self.plan.dosecubes:
#             dose_type = dose.basename.split(".")[-1]
#             target_name = self.plan.voi_target.name
#             dvh = {target_name: volhist.VolHist(dose, self.patient.vdx.get_voi_by_name(target_name))}
#             self.volume_histograms["DVH " + dose_type] = dvh
# 
#     def _compute_target_lvh(self):
#         for let in self.plan.letcubes:
#             let_type = let.basename.split(".")[-1]
#             target_name = self.plan.voi_target.name
#             lvh = {target_name: volhist.VolHist(let, self.patient.vdx.get_voi_by_name(target_name))}
#             self.volume_histograms["LVH " + let_type] = lvh
# 
#     def __str__(self):
#         return "Sim: " + self.name


import copy
import logging
import pytrip.tripexecuter as pte
import sys
from pytripgui.plan_executor.simulation_results import SimulationResults

logger = logging.getLogger(__name__)


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

# Path: pytripgui/plan_executor/simulation_results.py
# class SimulationResults:
#     def __init__(self, patient, plan, name):
#         self.patient = patient
#         self.plan = plan
#         self.name = name
#         self.volume_histograms = {}
# 
#         self._compute_target_dvh()
#         self._compute_target_lvh()
# 
#     def get_doses(self):
#         return self.plan.dosecubes
# 
#     def get_dose(self, dose_type):
#         if dose_type not in DosCube.allowed_suffix:
#             raise ValueError("Wrong dose type")
# 
#         for dose in self.plan.dosecubes:
#             if dose.basename.endswith("." + dose_type):
#                 return dose
#         return None
# 
#     def get_lets(self):
#         return self.plan.letcubes
# 
#     def import_dos(self, dos_path):
#         logger.debug("Open DosCube {:s}".format(dos_path))
#         dos = DosCube()
#         dos.read(dos_path)
#         self.plan.dosecubes.append(dos)
# 
#     def import_let(self, let_path):
#         logger.debug("Open LETCube {:s}".format(let_path))
#         let = LETCube()
#         let.read(let_path)
#         self.plan.letcubes.append(let)
# 
#     def get_let(self, let_type):
#         if let_type not in LETCube.allowed_suffix:
#             raise ValueError("Wrong LET type")
# 
#         for let in self.plan.letcubes:
#             if let.basename.endswith("." + let_type):
#                 return let
#         return None
# 
#     def _compute_target_dvh(self):
#         for dose in self.plan.dosecubes:
#             dose_type = dose.basename.split(".")[-1]
#             target_name = self.plan.voi_target.name
#             dvh = {target_name: volhist.VolHist(dose, self.patient.vdx.get_voi_by_name(target_name))}
#             self.volume_histograms["DVH " + dose_type] = dvh
# 
#     def _compute_target_lvh(self):
#         for let in self.plan.letcubes:
#             let_type = let.basename.split(".")[-1]
#             target_name = self.plan.voi_target.name
#             lvh = {target_name: volhist.VolHist(let, self.patient.vdx.get_voi_by_name(target_name))}
#             self.volume_histograms["LVH " + let_type] = lvh
# 
#     def __str__(self):
#         return "Sim: " + self.name


# Path: pytripgui/plan_executor/executor.py
import copy
import logging
import pytrip.tripexecuter as pte
import sys
from pytripgui.plan_executor.simulation_results import SimulationResults


logger = logging.getLogger(__name__)


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