package com.henrypump.poc;

/**
 * Created by patrickjmcd on 2/1/17.
 * POC Class
 *
 */

public class POC implements Runnable{
    protected Well thisWell;
    private Thread t;
    private boolean ioEnabled;

    POC(String dbHostname){
        ioEnabled = true;
        thisWell = new Well(dbHostname, 1, 2);
        thisWell.getWellSetup();
    }

    POC(String simFileName, boolean ioEnabled, String dbHostname){
        this.ioEnabled = ioEnabled;
        if (this.ioEnabled) {
            thisWell = new Well(dbHostname, simFileName,1, 2);
        } else {
            thisWell = new Well(dbHostname, simFileName,99, 99);
        }
        thisWell.getWellSetup();
    }


    public void run(){
        new Thread(new CLScanner(this)).start();
        long sleepMilliseconds = (long) (thisWell.getDt() * 1000);
        thisWell.checkSafeties();
        if(ioEnabled){
            new Thread(new IOControl(this)).start();
            while (true) {
                thisWell.eval();
                try {
                    Thread.sleep(sleepMilliseconds);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } else {
            while (true) {
                for (int i = 0; i <= thisWell.sim.getLastFilledIndex(); i++) {
                    thisWell.eval(i);

                    try {
                        Thread.sleep(sleepMilliseconds);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }

    public void start () {
        System.out.println("Starting POC Thread");
        if (t == null) {
            t = new Thread (this, "POC-Thread");
            t.start ();
        }
    }


    public static void main(String[] args) {
        if (args[0].equals("help")){
            System.out.println("Pass command line parameters...");
            System.out.println("=============");
            System.out.println("");
            System.out.println("Simulation Mode:");
            System.out.println("-------------");
            System.out.println("<String: simulation filename> <boolean: use IO> <String: database hostname/address (optional)>");
            System.out.println("");
            System.out.println("IO Mode:");
            System.out.println("-------------");
            System.out.println("<String: database hostname/address (optional)>");
            System.out.println("");
            System.out.println("If updating config via json file, place a config file named 'wellSetup.json' in the project directory.");
            System.exit(2);
        }


        String dbHostname = "localhost";
        if (args.length > 2){
            dbHostname = args[2];
        }

        if (args.length == 1){
            dbHostname = args[0];
        }

        if (args.length < 2){
            final POC realPOC = new POC(dbHostname);
            try {
                WebServer httpServer = new WebServer(8000, realPOC);
            } catch (Exception e){
                System.out.println("problem starting web server");
                System.out.println(e);
            }
            realPOC.start();
        } else {
            final POC simPOC = new POC(args[0], args[1].equals("true"), dbHostname);
            try {
                WebServer httpServer = new WebServer(8000, simPOC);
            } catch (Exception e){
                System.out.println("problem starting web server");
                System.out.println(e);
            }
            simPOC.start();
        }


    }
}




