package jforex;

import com.dukascopy.api.indicators.*;
import com.dukascopy.api.IConsole;
import org.postgresql.Driver;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
@Library("postgresql-9.1-901.jdbc4.jar")

public class IndicatorSQLtest implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private double[][] inputs = new double[1][];
    private int timePeriod = 2;
    private double[][] outputs = new double[1][];
    
    private IConsole console;
    private Connection connection = null;
    
    public void onStart(IIndicatorContext context) {
        console = context.getConsole();
        indicatorInfo = new IndicatorInfo("EXAMPIND", "Sums previous values", "My indicators",
                false, false, false, 1, 1, 1);
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE)};
        optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("Time period", OptInputParameterInfo.Type.OTHER,
                new IntegerRangeDescription(2, 2, 100, 1))};
        outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.DrawingStyle.LINE)};
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (openSQLConnection()==0) {
            // BEGIN my SQL code
            // ...
            // END my SQL code 
            closeSQLConnection();
        }
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
        int i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
            double value = 0;
            for (int k = timePeriod; k > 0; k--) {
                value += inputs[0][i - k];
            }
            outputs[0][j] = value;
        }
        return new IndicatorResult(startIndex, j);
    }

    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

    public InputParameterInfo getInputParameterInfo(int index) {
        if (index <= inputParameterInfos.length) {
            return inputParameterInfos[index];
        }
        return null;
    }

    public int getLookback() {
        return timePeriod;
    }

    public int getLookforward() {
        return 0;
    }

    public OptInputParameterInfo getOptInputParameterInfo(int index) {
        if (index <= optInputParameterInfos.length) {
            return optInputParameterInfos[index];
        }
        return null;
    }

    public OutputParameterInfo getOutputParameterInfo(int index) {
        if (index <= outputParameterInfos.length) {
            return outputParameterInfos[index];
        }
        return null;
    }

    public void setInputParameter(int index, Object array) {
        inputs[index] = (double[]) array;
    }

    public void setOptInputParameter(int index, Object value) {
        timePeriod = (Integer) value;
    }

    public void setOutputParameter(int index, Object array) {
        outputs[index] = (double[]) array;
    }
    
    private int openSQLConnection() {
        console.getOut().println("Start connecting...");
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            console.getOut().println("Where is your PostgreSQL JDBC Driver? " + "Include in your library path!");
            e.printStackTrace();
            return 1;
        }
        //console.getOut().println("PostgreSQL JDBC Driver Registered!");
        try {
            connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/mydatabase", "postgres","postgres");
        } catch (SQLException e) {
            console.getOut().println("Connection Failed! Check output console");
            e.printStackTrace();
            return 2;
        }
        if (connection != null) {
            //console.getOut().println("You made it, take control your database now!");
             return 0;
        } else {
            console.getOut().println("Failed to make connection!");
            return 3;
        }
    }
    
    private void closeSQLConnection() {
        try {
            if (connection!=null && !connection.isClosed()) {
                connection.close();
                //console.getOut().println("Connection closed!");
            }
        } catch (SQLException ignored) {
            console.getErr().println(ignored);
        }
    }
}
