package jforex;

import com.dukascopy.api.indicators.*;
import com.dukascopy.api.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;

public class DrawBug implements IDrawingIndicator,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[2][];
    
    public void onStart(IIndicatorContext context) {
        indicatorInfo = new IndicatorInfo("EXAMPIND", "Sums previous values", "My indicators",
        		false, false, false, 1, 1, 2);
        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))};
        
        // set legend output
        OutputParameterInfo legendOutput =
                new OutputParameterInfo("", OutputParameterInfo.Type.DOUBLE, 
                                                   OutputParameterInfo.DrawingStyle.NONE);        
        legendOutput.setColor(new Color(0,0,0));
        legendOutput.setDrawnByIndicator(true);        
        
        outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.DrawingStyle.LINE), legendOutput};
    }

    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++) {
        	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 Point drawOutput(Graphics g, int outputIdx, Object values2, 
            Color color, Stroke s,IIndicatorDrawingSupport indicatorDrawingSupport, 
            java.util.List<Shape> shapes, Map<Color, java.util.List<Point>> handles) 
    {                    
         
        //int maxY = indicatorDrawingSupport.                      
                    
        // drawLegend    
        int offsetY = 50;
        for (int i=1;i<5;++i)                                    
        {
            Color c = new Color(0,0,0);
            String strColor = "RGB: " + c.getRed()+","+c.getGreen()+","+c.getBlue();
                        
            g.setColor(c);
            
            g.drawString(strColor,0,offsetY+i*15);
        }        
        
        return null;        
    }


    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;
    }
}