/*
 * PanLayer.java
 *
 * Created on Jul 13, 2009, 4:44:27 PM
 */
package usda.weru.gis.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import javax.swing.JComponent;

/**
 *
 * @author Joseph A. Levin <joelevin@weru.ksu.edu>
 */
public class MapPanLayer extends javax.swing.JPanel {

    private static final long serialVersionUID = 1L;

    private final MapController c_controller;

    /** Creates new form PanLayer
     * @param controller */
    public MapPanLayer(MapController controller) {
        c_controller = controller;
        //shape code
        int x = 0;
        int y = 0;
        int v = 10;
        int h = 20;

        int[] xp = new int[]{x, h, -h};
        int[] yp = new int[]{y, v, v};
        Shape north = new Polygon(xp, yp, 3);

        Shape south = new Polygon(xp, yp, 3);
        //end shape code
        initComponents();
        add(new PanArrow(north, Direction.North));
        add(new PanArrow(south, Direction.South));
        add(new PanArrow(north, Direction.East));
        add(new PanArrow(north, Direction.West));

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setOpaque(false);
        setLayout(null);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    private enum Direction {

        North,
        South,
        East,
        West
    }

    private class PanArrow extends JComponent {

        private static final long serialVersionUID = 1L;

        private Direction c_direction;
        int c_xHitPadding = 25;
        int c_yHitPadding = 8;
        int c_padding = 5;
        private boolean c_active;
        private final Shape c_shape;

        public PanArrow(Shape shape, Direction direction) {
            c_shape = shape;
            setDirection(direction);
            setActive(false);

            switch (c_direction) {
                case North:
                case South:
                    setSize(c_shape.getBounds().width + (c_xHitPadding * 2), c_shape.getBounds().height + (c_yHitPadding * 2));
                    break;
                case East:
                case West:
                    setSize(c_shape.getBounds().height + (c_yHitPadding * 2), c_shape.getBounds().width + (c_xHitPadding * 2));
                    break;
            }

            //arrow moves itself as the parent is resized.
            MapPanLayer.this.addComponentListener(new ComponentAdapter() {

                @Override
                public void componentResized(ComponentEvent e) {
                    PanArrow.this.setLocation(c_direction);
                }

            });

            addMouseListener(new MouseAdapter() {

                @Override
                public void mouseEntered(MouseEvent e) {
                    PanArrow.this.setActive(true);
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    PanArrow.this.setActive(false);
                }

                @Override
                public void mouseClicked(MouseEvent e) {
                    int x = 0;
                    int y = 0;
                    switch (c_direction) {
                        case North:
                            y = 1;
                            break;
                        case East:
                            x = -1;
                            break;
                        case South:
                            y = -1;
                            break;
                        case West:
                            x = 1;
                            break;
                    }

                    c_controller.pan(x * 150, y * 150);
                    e.consume();

                }

            });

        }

        public Direction getDirection() {
            return c_direction;
        }

        public void setDirection(Direction direction) {
            c_direction = direction;
            setLocation(direction);
            repaint();
        }

        public void setActive(boolean active) {
            c_active = active;
            repaint();
        }

        public boolean isActive() {
            return c_active;
        }

        public void setLocation(Direction direction) {
            switch (c_direction) {
                case North:
                    setLocation(MapPanLayer.this.getWidth() / 2 - getWidth() / 2, 0);
                    return;
                case South:
                    setLocation(MapPanLayer.this.getWidth() / 2 - getWidth() / 2, MapPanLayer.this.getHeight() - getHeight());
                    return;
                case West:
                    setLocation(0, MapPanLayer.this.getHeight() / 2 - getHeight() / 2);
                    return;
                case East:
                    setLocation(MapPanLayer.this.getWidth() - getWidth(), MapPanLayer.this.getHeight() / 2 - getHeight() / 2);

            }
        }

        @Override
        protected void paintComponent(Graphics g) {

            if (c_active) {
                Graphics2D g2d = (Graphics2D) g;
            //first get the north mode working.                                    

                //make a transform
                AffineTransform transform = g2d.getTransform();

                switch (c_direction) {
                    case North:
                        transform.translate(getWidth() / 2, c_padding);
                        break;
                    case South:
                        transform.translate(getWidth() / 2, getHeight() - c_padding);
                        transform.scale(1, -1);
                        break;
                    case West:
                        transform.translate(c_padding, getHeight() / 2);
                        transform.rotate(Math.toRadians(-90));

                        break;
                    case East:
                        transform.translate(getWidth() - c_padding, getHeight() / 2);
                        transform.rotate(Math.toRadians(90));
                        break;

                }

                g2d.setTransform(transform);

                g2d.setColor(Color.LIGHT_GRAY);
                g2d.fill(c_shape);
                g2d.setColor(Color.GRAY);
                g2d.draw(c_shape);

            }

        }

    }

}
