package com.henrypump.poc;

import java.time.ZonedDateTime;

/**
 * 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 wellSetupJsonFile, String dbHostname){
        ioEnabled = true;
        thisWell = new Well(dbHostname, 99, 99, 7);
        thisWell.parseJSONFile(wellSetupJsonFile);
    }

    POC(String wellSetupJsonFile, String simFileName, boolean ioEnabled, String dbHostname){
        this.ioEnabled = ioEnabled;
        if (this.ioEnabled) {
            thisWell = new Well(dbHostname, simFileName,99, 99, 7);
        } else {
            thisWell = new Well(dbHostname, simFileName,99, 99, 99);
        }
        thisWell.parseJSONFile(wellSetupJsonFile);
    }


    public void run(){
        new Thread(new CLScanner(this)).start();
        if(ioEnabled){
            new Thread(new IOControl(this)).start();
        }
        long sleepMilliseconds = (long) (thisWell.getDt() * 1000);
        thisWell.setupFluidRatio(0.50, 0.50, 1.12);
        thisWell.checkSafeties();
        while (true) {
//            while (thisWell.getRunStatus() == Well.RUNSTATUS_RUNNING || thisWell.getRunStatus() == Well.RUNSTATUS_STARTING) {
                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) {
        String dbHostname = "localhost";
        if (args.length > 3){
            dbHostname = args[3];
        }
        final POC thisPOC = new POC(args[0], args[1], args[2].equals("true"), dbHostname);
        thisPOC.start();
    }
}




