package mapper;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;


public class MPController extends JPanel implements RubberBandObserver, MouseMotionListener, MouseListener
{
   MapperPanel mp;
   LatLonDisplay disp;
   int lastX, lastY;
   private RubberBandOverlay rbo = new RubberBandOverlay(this);

   public MPController(MapperPanel m)
   {
      setOpaque(true);
      setDoubleBuffered(false);

      this.mp = m;
      mp.addMouseMotionListener(this);
      mp.addMouseListener(this);

      FlowLayout fl = new FlowLayout(FlowLayout.LEFT, 2, 2);
      setLayout(fl);

      disp = new LatLonDisplay();
      add(disp);

      JButton b;

      b = new JButton("Center");
      b.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    mp.recentering(true);
                }
            });
      add(b);

      b = new JButton("Zoom In");
      b.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    mp.zoomIn(2);
                }
            });
      add(b);

      b = new JButton("Zoom Out");
      b.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    mp.zoomOut(2);
                }
            });
      add(b);

      b = new JButton("Grid");
      b.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    mp.toggleGridLines();
                }
            });
            /*
      b.addMouseListener(
            new MouseAdapter()
            {
               public void mousePressed(MouseEvent e)
               {
                  int mods = e.getModifiers();
                  if((mods & InputEvent.BUTTON3_MASK) != 0)
                  {
                     (new GridControls()).show();
                  }
               }
            });
            */
      add(b);

      b = new JButton("Scale");
      b.addActionListener(
            new ActionListener()
            {
               public void actionPerformed(ActionEvent e)
               {
                  mp.toggleScale();
               }
            });
      add(b);

      b = new JButton("RBZoom");
      b.addActionListener(
            new ActionListener()
            {
               public void actionPerformed(ActionEvent e)
               {
                  Dimension mpSize = mp.getSize();
                  rbo.setBounds(0, 0, mpSize.width, mpSize.height);
                  mp.add(rbo, -1);
               }
            });
      add(b);
   }

// Implement RubberBandObserver:
   public void rubberBandComplete(int x, int y, int width, int height)
   {
      mp.remove(rbo);
      mp.zoomToBox(x, y, width, height);
   }

// Implement MouseMotionListener:
   public void mouseDragged(MouseEvent e)
   {/*
      int currX = e.getX();   int currY = e.getY();
      float[] ll = mp.xy2LatLon(currX, currY);
      disp.display(ll[0], ll[1]);
      mp.translateCenter(currX - lastX, currY - lastY);
      lastX = currX;  lastY = currY;*/
   }

   public void mouseMoved(MouseEvent e)
   {
      float[] ll = mp.xy2LatLon(e.getX(), e.getY());
      disp.display(ll[0], ll[1]);
   }

   public void mouseClicked(MouseEvent e) { }
   public void mouseEntered(MouseEvent e) { }
   public void mouseExited(MouseEvent e) { }

   public void mousePressed(MouseEvent e)
   {
      int mods = e.getModifiers();
      if((mods & InputEvent.BUTTON1_MASK) != 0)
      {
         lastX = e.getX();  lastY = e.getY();
      }
   }

   public void mouseReleased(MouseEvent e) { }
}
