package usda.weru.weps.webstart.CsipRunner;

import usda.weru.weps.webstart.Installer.wepsModelessDialog;
import usda.weru.util.ConfigData;
import weps.ws.Csip.utils.About;
import weps.ws.JettyWebstart.CsipInstance.CsipDataInstance;
import weps.ws.JettyWebstart.CsipInstance.CsipSciInstance;

import static usda.weru.weps.serverControl.ServerControlData.*;

/**
 *
 * @author mhaas
 */
public class WsCsipRunner {
    
    protected ConfigData cd;
    protected boolean dataRunning;
    protected boolean scienceRunning;
    protected String userLocalDirName;
    protected CsipSciInstance csipSciInstance;
    protected CsipDataInstance csipDataInstance;
    
    
    public WsCsipRunner () {
        
        dataRunning = false;
        scienceRunning = false;
        
        userLocalDirName = About.getUserWepsRoot().getAbsolutePath();
        csipDataInstance = null;
        csipSciInstance = null;
    }
    
    
    public void runDataEndpoint (String jnlpBaseUrl, String runnerEndpoint) {
        if (!dataRunning) {
            
            csipDataInstance = new CsipDataInstance(jnlpBaseUrl, runnerEndpoint, userLocalDirName);
            
            // Starting up the CSIP services takes a minute or three
            // So place the start up in its own thread...
            Runnable runnableStartSeq = new Runnable() {
                public void run() {

                    wepsModelessDialog dlg;
                    dlg = new wepsModelessDialog();
                    dlg.setMainText("Data server Runner");
                    dlg.setProgressIndeterminate(true);
                    dlg.appendText("Initializing and starting the local data server, this will take a minute or two...");
                    dlg.setAlwaysOnTop(true);
                    dlg.setLocation(dlg.getWidth(), dlg.getHeight());
                    dlg.setVisible(true);

                    dataRunning = true;
                    csipDataInstance.startService();
                    csipDataInstance.configureService();

                    dlg.dispose();
                }
            };
            new Thread(runnableStartSeq).start();
        }
    }
        
    public void runScienceEndpoint (String jnlpBaseUrl, String runnerEndpoint) {
        if (!scienceRunning) {
            
            csipSciInstance = new CsipSciInstance(jnlpBaseUrl, runnerEndpoint, userLocalDirName);
            
            // Starting up the CSIP services takes a minute or three
            // So place the start up in its own thread...
            Runnable runnableStartSeq = new Runnable() {
                public void run() {

                    wepsModelessDialog dlg;
                    dlg = new wepsModelessDialog();
                    dlg.setMainText("Service Runner");
                    dlg.setProgressIndeterminate(true);
                    dlg.appendText("Initializing and starting the local Science code runners, this will take a minute or two...");
                    dlg.setAlwaysOnTop(true);
                    dlg.setLocation(dlg.getWidth(), dlg.getHeight());
                    dlg.setVisible(true);

                    scienceRunning = true;
                    csipSciInstance.startService();
                    csipSciInstance.configureCombinedServices();

                    dlg.dispose();
                }
            };
            new Thread(runnableStartSeq).start();
        }
    }

    
    public void stopRunningAll () {
        stopRunningSci();
        stopRunningData();
    }

    
    public void stopRunningSci () {
        if (scienceRunning) {
            csipSciInstance.stopService();
            scienceRunning = false;
        }
    }

    
    public void stopRunningData () {
        if (dataRunning) {
            csipDataInstance.stopService();
            dataRunning = false;
        }
    }

    
    
    public void setCdAndRunIfNeeded (ConfigData newCd) {
    
        String runLocalJettyVal;
        boolean runJetty;
        
        cd = newCd;
        
        // for dataHandler
        runLocalJettyVal = cd.getData(cdKeyDataHandlerBase+cdKeyAddLocalJetty);
        runJetty = (runLocalJettyVal != null && runLocalJettyVal.contentEquals("1")) ? true : false;
        if (runJetty) {
            if (dataRunning) {
                if (!cd.getData(cdKeyDataHandlerBase+cdKeyAddJnlpUrlRef).contentEquals(csipDataInstance.getJnlp())) {
                    stopRunningData();
                }
            }
            if (!dataRunning) {
                runDataEndpoint(cd.getData(cdKeyDataHandlerBase+cdKeyAddJnlpUrlRef),
                                cd.getData(cdKeyDataHandlerBase+cdKeyAddEndpoint));
            }
        } else {
            stopRunningData();
        }
        
        // for science code combined runner
        runLocalJettyVal = cd.getData(cdKeySciCombinedBase+cdKeyAddLocalJetty);
        runJetty = (runLocalJettyVal != null && runLocalJettyVal.contentEquals("1")) ? true : false;
        if (runJetty) {
            if (scienceRunning) {
                if (!cd.getData(cdKeySciCombinedBase+cdKeyAddJnlpUrlRef).contentEquals(csipSciInstance.getJnlp())) {
                    stopRunningSci();
                }
            }
            if (!scienceRunning) {
                runScienceEndpoint(cd.getData(cdKeySciCombinedBase+cdKeyAddJnlpUrlRef),
                                   cd.getData(cdKeySciCombinedBase+cdKeyAddEndpoint));
            }
        } else {
            stopRunningSci();
        }
    }
}
