package  jforex.indicators.objects;

import java.awt.Color;
import java.awt.Font;
import java.util.UUID;
import com.dukascopy.api.*;
import com.dukascopy.api.drawings.*;
import com.dukascopy.api.drawings.IScreenLabelChartObject.Corner;
import com.dukascopy.api.feed.IFeedDescriptor;
import com.dukascopy.api.indicators.*;
import com.dukascopy.api.IBar;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.OfferSide;
import java.text.SimpleDateFormat;
public class BarTime 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 IIndicatorContext context;  
    private IConsole console;
    private IHistory history;
    private IFeedDescriptor feedDescriptor;
    private SimpleDateFormat sdf;
    public void onStart(IIndicatorContext context) {
        indicatorInfo = new IndicatorInfo("BarTime", "BarTime values", "Yils indicators",
                true, false, false, 1,0, 1);
         inputParameterInfos = new InputParameterInfo[] {
            new InputParameterInfo("Bars", InputParameterInfo.Type.BAR)};
         outputParameterInfos = new OutputParameterInfo[] {
                new OutputParameterInfo("K时间", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.NONE) {
                      {
                      this.setColor(Color.RED);
                      }
                  }
                };
        history=context.getHistory();
        feedDescriptor = context.getFeedDescriptor();
        this.context = context;
        this.console = context.getConsole();
        sdf = new SimpleDateFormat("YY HH:mm:ss");
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
       try {
               if (startIndex - getLookback() < 0) {
                    startIndex -= startIndex - getLookback();
                }
                if (startIndex > endIndex) {
                        return new IndicatorResult(0, 0);
                } 
               // long startTime= history.getStartTimeOfCurrentBar(feedDescriptor.getInstrument(), feedDescriptor.getPeriod());
               //  history.getBarStart(feedDescriptor.getPeriod(), arg1)
               //  history.getBarStart(arg0, arg1)
              
                int i, j;
                for (i = startIndex, j = 0; i <= endIndex; i++, j++) {            
                     outputs[0][j]=inputs[0][i].getOpen();
                }
               return new IndicatorResult(startIndex, j);
            } catch (Exception e) {
                 return new IndicatorResult(0,0);
            }
    }

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