package com.henrypump.poc;

import java.time.ZonedDateTime;
import java.util.Scanner;

/**
 * Created by patrickjmcd on 2/8/17.
 */
class CLScanner implements Runnable {

    private final POC poc;
    CLScanner(POC poc){
        this.poc = poc;
    }

    private void startWell(){
        poc.thisWell.start("commandline");
    }

    private void stopWell(){
        poc.thisWell.stop("commandline");
    }

    private void pumpoffWell(){
        poc.thisWell.pumpOff("commandline");
    }

    private void restartWell(){
        poc.thisWell.restart("commandline");
    }

    private void exitPOC(){
        poc.thisWell.db.newRunStatus("Shutdown", "commandline");
        System.exit(99);
    }

    private void runStatus(){
        System.out.println("Run Status: " + poc.thisWell.getRunStatusString());
    }

    private void showTapers(){
        poc.thisWell.printTapers();
    }

    private void showTotals(){
        poc.thisWell.printTotals();
    }

    private void gaugeOff() {
        poc.thisWell.gaugeOff();
        poc.thisWell.printTotals();
    }

    private void help(){
        System.out.println("");
        System.out.println("== HELP MENU ==");
        System.out.println("");
        System.out.println("POSSIBLE COMMANDS");
        System.out.println("start -- Issues the start command");
        System.out.println("stop -- Issues the stop command");
        System.out.println("pumpoff -- Issues the pumpoff command");
        System.out.println("restart -- Issues the restart command");
        System.out.println("status -- Gets the current run status");
        System.out.println("showtapers -- Gets the current taper and well parameters");
        System.out.println("showtotals -- Gets the current totals and averages");
        System.out.println("exit -- Quits the program");
        System.out.println("");
        System.out.println("TO ADD A NEW WELL TEST:");
        System.out.println("welltest <timestamp> <test hours> <total fluid BBL> <oil BBL> <water BBL> <gas MCF>");
        System.out.println("");
        System.out.println("TO ADD A NEW FLUID SHOT:");
        System.out.println("fluidshot <fluidlevel>");
        System.out.println("");
    }



    public void run() {
        Scanner sc = new Scanner(System.in);
        String input = "";
        while (sc.hasNextLine()) {
            input = sc.nextLine();
            switch(input){
                case "start":
                    startWell();
                    break;
                case "stop":
                    stopWell();
                    break;
                case "pumpoff":
                    pumpoffWell();
                    break;
                case "restart":
                    restartWell();
                    break;
                case "exit":
                    exitPOC();
                    break;
                case "status":
                    runStatus();
                    break;
                case "showtotals":
                    showTotals();
                    break;
                case "showtapers":
                    showTapers();
                    break;
                case "gaugeoff":
                    gaugeOff();
                    break;
                case "help":
                    help();
                    break;
                default:
                    if (input.startsWith("welltest")){
                        String[] testParams = input.split(" ");
                        ZonedDateTime timestamp = ZonedDateTime.parse(testParams[1]);
                        poc.thisWell.wellTest = new WellTest(timestamp,
                                Double.parseDouble(testParams[2]), Double.parseDouble(testParams[3]),
                                Double.parseDouble(testParams[4]), Double.parseDouble(testParams[5]),
                                Double.parseDouble(testParams[6]), poc.thisWell.db.getPreviousDailyTotal(timestamp)
                        );
                        poc.thisWell.db.newWellTest(poc.thisWell.wellTest);
                        poc.thisWell.wellTest.print();
                    } else if(input.startsWith("fluidshot")){
                        String[] shotParams = input.split(" ");
                        FluidShot fs = new FluidShot(Double.parseDouble(shotParams[1]), poc.thisWell.getFluidGradient(),
                                poc.thisWell.cardStorage[0].getDownholeLoadSpan(), poc.thisWell.getRodDepthTotal(),
                                poc.thisWell.getPumpArea());
                        poc.thisWell.db.newFluidShot(fs);
                        poc.thisWell.setFrictionEstimate(fs.getFrictionEstimate());
                    } else {
                        help();
                    }
            }
        }
    }


}
