package com.henrypump.poc;

/**
 * Created by patrickjmcd on 2/1/17.
 * POC Class
 *
 */
import java.awt.*;
import java.awt.event.*;
import java.time.Instant;
import java.util.Date;

public class POC implements Runnable{
    protected Well thisWell;
    private int simLoops;
    private Thread t;
    private DigitalIn startBtn, stopBtn;
    private DigitalOut led2, led3, led4, led5;
    private DigitalOut runIndicator;

    private boolean ioEnabled;
    private boolean guiEnabled;



    POC(String wellName, String wellSetupJsonFile, int simLoops){
        ioEnabled = true;
        guiEnabled = false;
        thisWell = new Well(wellName, 99, 99, 7);
        thisWell.parseJSONFile(wellSetupJsonFile);
        thisWell.printTapers();
        this.simLoops = simLoops;

        // IO
        startBtn = new DigitalIn(8);
        stopBtn = new DigitalIn(9);
        led2 = new DigitalOut(2, 0);
        led3 = new DigitalOut(3, 0);
        led4 = new DigitalOut(4, 0);
        led5 = new DigitalOut(5, 0);
        runIndicator = new DigitalOut(6,0);


        String headlessProp = !guiEnabled ? "true" : "false";
        System.setProperty("java.awt.headless", headlessProp);
        System.out.println(java.awt.GraphicsEnvironment.isHeadless());
    }

    POC(String wellName, String wellSetupJsonFile, String simFileName, boolean ioEnabled, int simLoops){
        this.ioEnabled = ioEnabled;
        if (this.ioEnabled) {
            guiEnabled = false;
            thisWell = new Well(wellName, simFileName,99, 99, 7);

            // IO
            startBtn = new DigitalIn(8);
            stopBtn = new DigitalIn(9);
            led2 = new DigitalOut(2, 0);
            led3 = new DigitalOut(3, 0);
            led4 = new DigitalOut(4, 0);
            led5 = new DigitalOut(5, 0);
            runIndicator = new DigitalOut(6,0);

        } else {
            thisWell = new Well(wellName, simFileName,99, 99, 99);
            guiEnabled = true;
            // IO
            startBtn = new DigitalIn(99);
            stopBtn = new DigitalIn(99);
            led2 = new DigitalOut(99, 0);
            led3 = new DigitalOut(99, 0);
            led4 = new DigitalOut(99, 0);
            led5 = new DigitalOut(99, 0);
            runIndicator = new DigitalOut(99,0);
        }

        String headlessProp = !guiEnabled ? "true" : "false";
        System.setProperty("java.awt.headless", headlessProp);
        System.out.println(java.awt.GraphicsEnvironment.isHeadless());

        thisWell.parseJSONFile(wellSetupJsonFile);
        thisWell.printTapers();
        this.simLoops = simLoops;
    }

    public void allOutputsOff(){
        led2.write(0,true);
        led3.write(0,true);
        led4.write(0,true);
        led5.write(0,true);
    }

    public void run(){
        int loopCounter = 0, loopLimit = simLoops, led2out, led3out, led4out,led5out;
        double pos;
        boolean newWellTest = true;
        long sleepMilliseconds = (long) (thisWell.getDt() * 1000);
        thisWell.setupFluidRatio(0.50, 0.50, 1.12);
        thisWell.checkSafeties();
        while (true) {
            while (loopCounter < loopLimit && (thisWell.getRunStatus() == Well.RUNSTATUS_RUNNING || thisWell.getRunStatus() == Well.RUNSTATUS_STARTING)) {
                for (int i = 0; i <= thisWell.sim.getLastFilledIndex(); i++) {
                    if (startBtn.read() == 1) thisWell.start("startbutton");
                    if (stopBtn.read() == 1) thisWell.stop("stopbutton");
                    if (startBtn.read() == 1 && stopBtn.read() == 1) {
                        System.exit(0);
                    }


                    thisWell.eval(i);

                    pos = thisWell.getCurrentSurfacePosition();
                    led2.write(pos > 20.0 ? 1 : 0);
                    led3.write(pos > 40.0 ? 1 : 0);
                    led4.write(pos > 60.0 ? 1 : 0);
                    led5.write(pos > 80.0 ? 1 : 0);

                    runIndicator.write(thisWell.getRunStatus()==Well.RUNSTATUS_RUNNING ? 1 : 0);

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

            if (newWellTest){
                System.out.println("Previous kFactor = " + thisWell.db.getLatestKFactor());
                Date nowDate = Date.from(Instant.now());
                double lastProductionMeasured = thisWell.db.getPreviousDailyTotal(nowDate);
                thisWell.wellTest = new WellTest(nowDate, 24.0, .35, .20, .15, 1.25, lastProductionMeasured);
                thisWell.db.newWellTest(thisWell.wellTest);
                System.out.println("Well Test @ " + nowDate.toString());
                System.out.println("kFactor: " + thisWell.wellTest.getkFactor());
                System.out.println("oilRatio: " + thisWell.wellTest.getOilRatio());
                System.out.println("waterRatio: " + thisWell.wellTest.getWaterRatio());
                System.out.println("gasRatio: " + thisWell.wellTest.getGasMCFRatio());
                newWellTest = false;
                System.out.println("Last kFactor = " + thisWell.db.getLatestKFactor());
            }


            if (startBtn.read() == 1) thisWell.start("startbutton");
            if (stopBtn.read() == 1) thisWell.stop("stopbutton");
            if (startBtn.read() == 1 && stopBtn.read() == 1) {
                System.exit(0);
            }
            led2.write(0);
            led3.write(0);
            led4.write(0);
            led5.write(0);
        }

    }

    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) {
        final POC thisPOC = new POC("Barney", args[0], args[1], true, 100);
        thisPOC.start();


    }
}


