package uk.co.patrickhaston.mars;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;

import javax.swing.JOptionPane;

import oracle.jdeveloper.layout.XYConstraints;
import oracle.jdeveloper.layout.XYLayout;

public class MarsMapPanel extends MPanel 
{
  private MarsFrame theFrame = null;
  private MarsMap theMap = null;
  private MarsGlobePanel theGlobe = null;
  private MarsNavigator navigator = null;
  private MarsZoomControl zoom = null;
  private MPanel navBar = null;
  public int selectedWaypoint = 0;

  public MarsMapPanel(MarsFrame frame)
  {
    theFrame = frame;
    try
    {
      jbInit();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  private void jbInit()
    throws Exception
  {
    theMap = new MarsMap(this, theFrame);
    theMap.setPreferredSize(new Dimension(theFrame.getWidth()-200, theFrame.getHeight()-100));
    theMap.setName("Map");
    theMap.showFeatures(true);
    theMap.setScale(50000);
    theGlobe = new MarsGlobePanel(this, theFrame);
    navigator = new MarsNavigator(this, theFrame);
    zoom = new MarsZoomControl(this, theFrame);
    //navigator.setPreferredSize(new Dimension(48, 48));

    addMouseListener();
    
    this.setLayout(new BorderLayout());
    navBar = new MPanel();
    navBar.setLayout(new BorderLayout());
    navBar.add(theGlobe, BorderLayout.NORTH);
    navBar.add(zoom, BorderLayout.CENTER);
    navBar.add(navigator, BorderLayout.SOUTH);
    this.add(theMap, BorderLayout.CENTER);
    this.add(navBar, BorderLayout.EAST);
    theFrame.isMapVisible = true;
    //this.setLayout(new XYLayout());
    //this.add(navigator, new XYConstraints(532, 262, 48, 38));
  }

  private void addMouseListener()
  {
    // Add mouse listener for the map
    MouseListener mapMouseListener = new MouseAdapter() 
    {
      public void mouseClicked(MouseEvent e) 
      {
        if (e.getClickCount() == 1) 
        {
          LatLong loc = theMap.viewToWorld(e.getPoint());
          if (loc != null)
          {
            //theFrame.mars.systemMessage("Mouse click at (" + loc.east + ", " + loc.north + ")");
            //theFrame.mapPanel.remove(theFrame.map);
            //remove(theFrame.map);
            theFrame.showWaypoint(selectedWaypoint);
            /*
            theFrame.settlementView = new MarsView(theFrame, theFrame.selectedWayPoint);
            theFrame.settlementView.setPreferredSize(new Dimension(400, 400));

            add(theFrame.settlementView);
            theFrame.currentView = theFrame.SETTLEMENT_VIEW;
            theFrame.settlementView.validate();
            //validate();
            theFrame.validate();
            theFrame.repaint();

            //JOptionPane.showMessageDialog(theFrame, new MarsView(theFrame, theFrame.selectedWayPoint), "View", JOptionPane.PLAIN_MESSAGE);
            */
          }
        }
      }
    };
    theMap.addMouseListener(mapMouseListener);
    
    // Add mouse motion listener for the map
    MouseMotionListener mouseMotion = new MouseMotionAdapter()
    {
      public void mouseMoved(MouseEvent e)
      {
        Point p = e.getPoint();
        LatLong loc = theMap.viewToWorld(p);
        if (loc != null)
        {
          int wp = theFrame.mars.Waypoints.nearestWaypoint(loc);
          selectedWaypoint = 0;
          if (theFrame.mars.Waypoints.distanceTo(wp,loc) * theMap.radius / theMap.scale  < 7)
          {
            // highlight the waypoint
            selectedWaypoint = wp;
          }
          theFrame.repaint();
        }
      }
    };
    theMap.addMouseMotionListener(mouseMotion);
  }
  
  public void setName(String n)
  {
      theMap.setName(n);
  }
  
  public void zoomIn()
  {
    theMap.zoomIn();
    theFrame.repaint();
  }

  public void zoomOut()
  {
    theMap.zoomOut();
    theFrame.repaint();
  }
  
  public void pan(int direction)
  {
    if(direction == 1) 
    {
      theMap.panNorth(); theMap.panWest();
      theGlobe.panNorth(); theGlobe.panWest();
    }
    if(direction == 2) 
    {
      theMap.panNorth();
      theGlobe.panNorth();
    }
    if(direction == 3) 
    {
      theMap.panNorth(); theMap.panEast();
      theGlobe.panNorth(); theGlobe.panEast();
    }
    if(direction == 4) 
    {
      theMap.panWest();
      theGlobe.panWest();
    }
    if(direction == 6) 
    {
      theMap.panEast();   
      theGlobe.panEast();
    }
    if(direction == 7) 
    {
      theMap.panSouth(); theMap.panWest();
      theGlobe.panSouth(); theGlobe.panWest();
    }
    if(direction == 8) 
    {
      theMap.panSouth();
      theGlobe.panSouth();
    }
    if(direction == 9) 
    {
      theMap.panSouth(); theMap.panEast();
      theGlobe.panSouth(); theGlobe.panEast();
    }
    theFrame.repaint();
  }

  public void panNorth()
  {
    theMap.panNorth();
  }

  public void panSouth()
  {
    theMap.panSouth();
  }

  public void panEast()
  {
    theMap.panEast();
  }

  public void panWest()
  {
    theMap.panWest();
  }
  
  public void goTo(LatLong loc)
  {
    if(loc != null)
    {
      theMap.viewpoint = loc;
    }
  }
  
  public double getScale()
  {
    return theMap.scale;
  }

  public double getNorth()
  {
    return theMap.viewpoint.north;
  }

  public double getEast()
  {
    return theMap.viewpoint.east;
  }
  
  public void setPanelSize(int w, int h)
  {
    super.setPanelSize(w,h);
    theMap.setPreferredSize(new Dimension(w-100,h));
  }

}

