package indicators;

import com.dukascopy.api.IIndicators;
import com.dukascopy.api.indicators.*;

public class testIndicator implements IIndicator 
{
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;

    private int optInput1;
    private int optInput2;

    private double[][] inputs = new double[1][];
    private double[][] outputs = new double[2][];
    

    public void onStart(IIndicatorContext context) 
    {
        inputParameterInfos = new InputParameterInfo[] 
        {
        	new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE) { { setAppliedPrice(IIndicators.AppliedPrice.CLOSE); } }
        };
        
        optInputParameterInfos = new OptInputParameterInfo[] 
        {                                                   
        	new OptInputParameterInfo("Opt Input 1", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(34, 5, 89, 1)),
        	new OptInputParameterInfo("Opt Input 2", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(5, 4, 6, 1))
        };
        
        outputParameterInfos = new OutputParameterInfo[] 
        {
        	new OutputParameterInfo("output 0", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE), 
            new OutputParameterInfo("output 1", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE)
        };

        indicatorInfo = new IndicatorInfo("test indicator", 
        								  "test indicator description", 
        								  "test indicators",
        								  true, 
        								  false, 
        								  false, 
        								  inputParameterInfos.length, 
        								  optInputParameterInfos.length, 
        								  outputParameterInfos.length);
    }

    public IndicatorResult calculate(int startIndex, int endIndex) 
    {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) 
        {
            startIndex -= startIndex - getLookback();
        }
        
        int i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) 
        {
            outputs[0][j] = inputs[0][i-getLookback()];
            outputs[1][j] = inputs[0][i-getLookback()];
        }
        
        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 optInput1 + optInput2;
    }

    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) 
    {
    	switch (index)
    	{
    	case 0:
    		optInput1 = (Integer) value;
    		break;
    	case 1:
    		optInput2 = (Integer) value;
    		break;
    	}
    }

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