/*
 * Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
 */
package ch.psi.plot.jlchart;

import ch.psi.plot.LinePlotBase;
import ch.psi.plot.LinePlotSeries;
import ch.psi.plot.utils.MonitoredPanel;
import fr.esrf.tangoatk.widget.util.chart.DataList;
import fr.esrf.tangoatk.widget.util.chart.JLAxis;
import fr.esrf.tangoatk.widget.util.chart.JLChart;
import fr.esrf.tangoatk.widget.util.chart.JLDataView;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

/**
 *
 */
public class LinePlot extends LinePlotBase {

    final JLChart plot;

    final ArrayList<JLDataView> views = new ArrayList<>();

    public LinePlot() {
        super();
        plot = new JLChart();
        plot.getXAxis().setAnnotation(JLAxis.VALUE_ANNO);
        //plot.getXAxis().setLabelFormat(JLAxis.SCIENTIFIC_FORMAT);
        plot.getY1Axis().setAutoScale(true);
        plot.getY2Axis().setAutoScale(true);
        plot.getXAxis().setAutoScale(true);
        plot.getY1Axis().setGridVisible(true);
        plot.getXAxis().setGridVisible(true);

        /*
         plot.getY1Axis().setAutoScale(false);
         plot.getY2Axis().setAutoScale(false);
         plot.getXAxis().setAutoScale(false);        
         plot.getY1Axis().setMinimum(0);
         plot.getY1Axis().setMaximum(10);
         plot.getY2Axis().setMinimum(0);
         plot.getY2Axis().setMaximum(10);        
         plot.getXAxis().setMinimum(0);
         plot.getXAxis().setMaximum(10);
         */
        plot.setLabelVisible(false);
        plot.setBackground(new Color(240, 240, 240));
        setRequireUpdateOnAppend(false);
    }

    @Override
    protected void onAxisRangeChanged(AxisId axis_id) {
        JLAxis axis = null;
        switch (axis_id) {
            case X:
                axis = plot.getXAxis();
                break;
            case Y:
                axis = plot.getY1Axis();
                break;
            default:
            return;
        }
        axis.setAutoScale(getAxis(axis_id).isAutoRange());
        axis.setMinimum(getAxis(axis_id).getMin());
        axis.setMaximum(getAxis(axis_id).getMax());
        update(true);
    }

    @Override
    protected Object onAddedSeries(LinePlotSeries series) {
        JLDataView view = new JLDataView();

        view.setColor(series.getColor());
        //view.setLineWidth(1);

        int markerSize = 4;
        view.setName(series.getName());
        view.setMarkerSize(markerSize);
        view.setMarkerColor(series.getColor());

        view.setViewType(JLDataView.TYPE_LINE);

        if (markerSize > 0) {
            view.setMarker(JLDataView.MARKER_DOT);
        } else {
            view.setMarker(JLDataView.MARKER_NONE);
        }

        if (series.getAxisY() == 2) {
            plot.getY2Axis().addDataView(view);
        } else {
            plot.getY1Axis().addDataView(view);
        }
        return view;
    }

    JLDataView getDataView(LinePlotSeries series) {
        return (JLDataView) (series.getToken());
    }

    @Override
    protected void onRemovedSeries(LinePlotSeries series) {
        JLDataView view = getDataView(series);
        for (int i = 0; i < plot.getY1Axis().getViewNumber(); i++) {
            if (view.getName().equals(plot.getY1Axis().getDataView(i).getName())) {
                plot.getY1Axis().removeDataView(view);
            }

        }
        for (int i = 0; i < plot.getY2Axis().getViewNumber(); i++) {
            if (view.getName().equals(plot.getY2Axis().getDataView(i).getName())) {
                plot.getY2Axis().removeDataView(view);
            }
        }
    }

    @Override
    protected void onAppendData(LinePlotSeries series, double x, double y) {
        JLDataView view = getDataView(series);
        if (view != null) //view.add(x, y);
        {
            plot.addData(view, x, y);
        }
    }

    @Override
    protected void onSetData(LinePlotSeries series, double[] x, double[] y) {
        JLDataView view = getDataView(series);
        if (view != null) {
            view.setData(x, y);
        }
    }

    @Override
    public double[][] getSeriesData(LinePlotSeries series) {
        JLDataView view = getDataView(series);
        double[] x = new double[view.getDataLength()];
        double[] y = new double[x.length];
        int index = 0;
        DataList data = view.getData();
        while (data != null) {
            if (index >= x.length) {
                break;
            }
            x[index] = data.x;
            y[index] = data.y;
            index++;
            data = data.next;
        }
        return new double[][]{x, y};
    }

    JPanel chartPanel;

    @Override
    protected JPanel createChartPanel() {
        JPanel chartPanel = new MonitoredPanel() {
            protected void onShown() {
                addKeyBindings();
            }
        };

        chartPanel.setLayout(new BorderLayout());
        chartPanel.add(plot);
        chartPanel.setPreferredSize(new Dimension(480, 240));

        plot.setHeader(getTitle());
        plot.getY1Axis().setName(getAxis(AxisId.Y).getLabel());
        plot.getXAxis().setName(getAxis(AxisId.Y).getLabel());
        return chartPanel;
    }

    @Override
    public void updateSeries(LinePlotSeries series) {
        doUpdate();//TODO
    }

    @Override
    public void doUpdate() {
        plot.repaint();
    }

    private void addKeyBindings() {
        //The zoom method  ois not doing what  I expecte, and no documentation...
        /*
         plot.setFocusable( true ); 
         plot.requestFocusInWindow();
         plot.addKeyListener(new KeyListener() {

         @Override
         public void keyTyped(KeyEvent e) {                
         }

         @Override
         public void keyPressed(KeyEvent e) {
         doUpdate();
         System.out.println("Total " + plot.getXAxis().getMinimum() + "->" + plot.getXAxis().getMaximum());
         System.out.println("Zoom  " + plot.getXAxis().getMin() + "->" + plot.getXAxis().getMax());
         if (plot.isZoomed()){
         if (e.getKeyCode()==KeyEvent.VK_RIGHT){                        
         double w = plot.getXAxis().getMax()-plot.getXAxis().getMin();
         double r = plot.getXAxis().getMax() + w/10;
         //r = Math.min(r,plot.getXAxis().getMaximum());
         plot.getXAxis().zoom((int)(r-w),(int)r);
         System.out.println("Zooming to " + ((int)(r-w)) + "-" + ((int)r));
         requestUpdate();                       

         }
         else if (e.getKeyCode()==KeyEvent.VK_LEFT){
         double w = plot.getXAxis().getMax()-plot.getXAxis().getMin();
         double l = plot.getXAxis().getMin() - w/10;
         //l = Math.max(l,plot.getXAxis().getMinimum());
         plot.getXAxis().zoom((int)l,(int)(l+w));
         System.out.println("Zooming to " + ((int)l) + "-" + ((int)(l+w)));
         requestUpdate();
         }
         }
         }

         @Override
         public void keyReleased(KeyEvent e) {
                
         }
         });
         */
    }

    protected void addPopupMenuItem(final JMenuItem item) {
        if (item == null) {
            plot.addSeparator();
        } else {
            plot.addMenuItem(item);
        }
    }

}
