#-------------------------------------------------------------------------------
# Name:          Matlab Implementation
# Purpose:       Allows the communication with the Matlab script for optimal experimental design
# Authors:       Eric N
#
# Last Version:  15.09.2022
# Copyright:     (c) Institute of Technical Biocatalysis TUHH
# Licence:       GNU GPL-v3 
#-------------------------------------------------------------------------------
import matlab.engine

class Temp:

    def __init__(self) -> None:

        ### Starts the matlab engine
        ...

        ### Initialize the DOE
        fittingTool = 1
        oed_mode = 2
        self.DOE = self.eng.DesignOfExperiments(fittingTool,oed_mode)
        self.eng.setResultPath(self.DOE,self.paths['Results'],nargout=0)
        self.DOE.setMaxExperiments(self.DOE,self.AdminValues.MaxExperiments,nargout=0)
        self.DOE.activateDiary(self.DOE,nargout=0)

    def addToDoE(self,experiment):
        ''' helper function for adding an experiment to the matlab DOE '''
        s1 = experiment['TargetConcentrationS1']
        s2 = experiment['TargetConcentrationS2']
        tres = experiment['ResTimes']
        cNADH = experiment['average_final_cNADH']
        self.eng.addExperiments(self.DOE,s1,s2,tres,cNADH,nargout=0)

    def runBaseExperiments(self):
        '''
        TODO: purge nach jedem Durchlauf?
        Main Loop that iterates through the original experiments.
        '''

        for i in range(self.currentExperiment,len(self.ListExperiments)):
            
            experiment = self.ListExperiments[i]
            self.runExperiment(experiment)
            # nur die folgende Zeile sollte neu sein:
            self.addToDoE(experiment)
            self.currentExperiment += 1

    def run(self):
        '''
        Loop that creates new experiments while the (matlab) stop conditions havent been met.
        '''
        self.runBaseExperiments()
        self.eng.runDOE(self.DOE,nargout=0)
        continue_OED = self.eng.checkStopConditions(self.DOE)
        self.eng.outputJSON(continue_OED,nargout=0)
        while continue_OED:
            newExp = self.eng.calcNextExperiment(self.DOE)
            self.ExperimentManager.addNewExperiment(newExp[2],newExp[0],newExp[1])
            for i in range(self.currentExperiment,len(self.ListExperiments)):
                experiment = self.ListExperiments[i]
                self.runExperiment(experiment)
                self.addToDoE(experiment)
                self.currentExperiment += 1
            self.eng.runDOE(self.DOE,nargout=0)
            continue_OED = self.eng.checkStopConditions(self.DOE)
            self.eng.outputJSON(self.DOE,continue_OED,nargout=0)

        self.shutdown()

    def shutdown(self):
        '''
        Function to shut down the whole system.
        '''
        print('shutdown Measurement')
        self.MCQueue.put('3,0')
        self.PumpInterface.stopPumps()
        self.spec.close()
        # neu ab hier:
        self.eng.saveFigures(self.DOE,nargout=0)
        self.eng.deactivateDiary(self.DOE,nargout=0)
