package com.taunova.ui.monitor.panel;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

import com.taunova.ui.light.component.LightLabel;
import com.taunova.ui.monitor.FlowMonitor;
import com.taunova.ui.light.PropertyListener;
import com.taunova.ui.monitor.series.SeriesMonitor;

public class PanelHistory extends FlowMonitor {

    private int mOffset;
    private int mZoom;
    private Display display = new Display();
    private SeriesMonitor monitor = null;

    public PanelHistory(SeriesMonitor seriesMonitor) {
        super(seriesMonitor.getSeriesContainer(), seriesMonitor.getRegionContainer());

        monitor = seriesMonitor;
        monitor.getRenderingOptions().addPropertyListener(new MonitorPropertyListener());

        display.setBackground(Color.PINK);

        renderingOptions.setGridAllowed(false);
        renderingOptions.setAllarmAllowed(false);

        mOffset = renderingOptions.getOffset();
        mZoom = renderingOptions.getZoom();

        addComponent(display, new Rectangle());

    }

    @Override
    public void paint(Graphics g) {
        drawDisplay();
        super.paint(g);
    }

    private void drawDisplay() {
        int _x = bounds.x + insets.left;
        int _width = bounds.width - insets.left - insets.right;

        int dWidth = (int) (_width / ((float) mZoom / renderingOptions.getZoom()));
        int dHeight = bounds.height - insets.top - insets.bottom;
        int dX = (int) (_x + (mOffset - renderingOptions.getOffset()) * renderingOptions.getZoom());

        int dY = Math.min(dX + dWidth, _x + _width);
        dX = Math.max(dX, _x);
        dWidth = dY - dX;

        display.setBounds(new Rectangle(dX, bounds.y + insets.top - 5, dWidth, dHeight + 10));
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);

        Point point = e.getPoint();
        if (getWorkBounds().contains(point) && isMouseOver()) {
            moveDisplay(point);
        }
    }

    private void moveDisplay(Point point) {
        Rectangle rectangle = getWorkBounds();

        int width = (int) (rectangle.width / ((float) mZoom / renderingOptions.getZoom()));
        int d = (point.x - rectangle.x - width / 2) / renderingOptions.getZoom();
        int offset = renderingOptions.getOffset() + d;

        monitor.getRenderingOptions().setOffset(offset);
    }

    private void moveDisplay(int delta) {

        int d = delta / renderingOptions.getZoom();
        int offset = monitor.getRenderingOptions().getOffset();
        monitor.getRenderingOptions().setOffset(offset + d);
    }

    class MonitorPropertyListener implements PropertyListener {

        public void propertyChanged() {
            int __offset = monitor.getRenderingOptions().getOffset();
            int __zoom = monitor.getRenderingOptions().getZoom();

            if (mOffset != __offset) {
                mOffset = __offset;
                repaint();
            }

            if (mZoom != __zoom) {
                mZoom = __zoom;
                repaint();
            }

        }
    }

    class Display extends LightLabel {

        private int _x = 0;
        private boolean _canDrag = false;

        public Display() {
            super("");
        }

        @Override
        public void mouseClicked(MouseEvent e) {

            super.mouseClicked(e);
            if (isMouseOver()) {
                moveDisplay(e.getPoint());
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            super.mouseReleased(e);
            _canDrag = false;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            super.mousePressed(e);
            _x = e.getX();
            _canDrag = true;
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            super.mouseDragged(e);

            if (!_canDrag) {
                return;
            }

            moveDisplay(e.getX() - _x);
            _x = e.getX();
        }
    }
}
