import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.indicators.*;

public class Indicator implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private IBar[][] inputs = new IBar[1][];
    
    private int timePeriod = 2;
    private double[][] outputs = new double[1][];
    private Period period = Period.TEN_SECS;
    
    private IIndicatorContext context;
    private IConsole console;
    private IndicatorResult indiRes;

    public void onStart(IIndicatorContext context) {
        this.context = context;
        console = context.getConsole();
        indicatorInfo = new IndicatorInfo("EXAMPIND", "Sums previous values", "My indicators", false, false, false, 1,
                0, 1);
        inputParameterInfos = new InputParameterInfo[] { new InputParameterInfo("input", InputParameterInfo.Type.BAR) {
            {
                setOfferSide(OfferSide.ASK);
                setInstrument(Instrument.EURUSD);
            }
        } };
        outputParameterInfos = new OutputParameterInfo[] { new OutputParameterInfo("out",
                OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) };
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {

        // calculate the number of values that are to be calculated
        int length = endIndex - startIndex - getLookback();
        
        // the last output value should end at inProgressBarTime
        long inProgressBarTime = System.currentTimeMillis() - period.getInterval();
        int i, j;
        
        // find correct endIndex that is before inProgressBarTime
        while (endIndex >= startIndex){            
            if(inputs[0][endIndex].getTime() < inProgressBarTime) {
                break;
            }
            endIndex--;
        }
        // find startIndex so that required number of values is produced and the output ends at inProgressBarTime
        startIndex = Math.max(startIndex, endIndex - length);
        
        for(int k = 0; k <= endIndex - startIndex; k++){
            outputs[0][k] = 1; 
        }

        return new IndicatorResult(startIndex, endIndex - startIndex + 1);
    }

    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] = (IBar[]) array;
    }

    public void setOptInputParameter(int index, Object value) {
        timePeriod = (Integer) value;
    }

    public void setOutputParameter(int index, Object array) {
        outputs[index] = (double[]) array;

    }
}