package uk.co.patrickhaston.mars;

import uk.co.patrickhaston.pdhsprites.PdhBitmapSprite;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MarsControl
  extends MPanel
{
  public MPanel myParent = null;
  // The parent view that this navigator will manipulate
  
  public MarsFrame theFrame = null;
  
  private PdhBitmapSprite image = null;
  // The composite image used to display the button depending on what has been clicked
  
  private Dimension sz = null;

  public int direction = 5;
  public static int NORTH_WEST = 1;
  public static int NORTH      = 2;
  public static int NORTH_EAST = 3;
  public static int WEST       = 4;
  public static int AT_REST    = 5;
  public static int EAST       = 6;
  public static int SOUTH_WEST = 7;
  public static int SOUTH      = 8;
  public static int SOUTH_EAST = 9;

  public MarsControl(MPanel parent, MarsFrame frame)
  {
    myParent = parent;
    theFrame = frame;
    
    try
    {
      addMouseListener();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  private void addMouseListener()
  {
    // Add mouse listener for the map
    MouseListener mapMouseListener = new MouseAdapter() 
    {
      public void mousePressed(MouseEvent e) 
      {
        handleMousePressed(e);
      }
      public void mouseReleased(MouseEvent e)
      {
        handleMouseReleased(e);
      }
      public void mouseClicked(MouseEvent e)
      {
        handleMouseClicked(e);
      }
    };
    addMouseListener(mapMouseListener);
  }
  
  public void handleMousePressed(MouseEvent e)
  {
    // to be over-written
  }

  public void handleMouseReleased(MouseEvent e)
  {
    // to be over-written
  }

  public void handleMouseClicked(MouseEvent e)
  {
    // to be over-written
  }
  
  public void setImage(PdhBitmapSprite img)
  {
    image = img;
  }
  
  public void setControlSize(Dimension s)
  {
    sz = s;
    this.setPreferredSize(s);
    this.setMaximumSize(s);
    this.setMinimumSize(s);
  }

  public int getDirection(Point p)
  {
    // work out which button has been clicked
    // default to centre
    return 5;
  }
  
  public void paintComponent(Graphics g)
  {
    Point topLeft = new Point(0,0); 
    
    switch (direction)
    {
      case 0: topLeft.x = 48; topLeft.y = 48; break; // shouldn't happen
      case 1: topLeft.x = 0;  topLeft.y = 0;  break; // NW
      case 2: topLeft.x = 48; topLeft.y = 0;  break; // N
      case 3: topLeft.x = 96; topLeft.y = 0;  break; // NE
      case 4: topLeft.x = 0;  topLeft.y = 48; break; // W
      case 5: topLeft.x = 48; topLeft.y = 48; break; // No button pressed
      case 6: topLeft.x = 96; topLeft.y = 48; break; // E
      case 7: topLeft.x = 0;  topLeft.y = 96; break; // SW
      case 8: topLeft.x = 48; topLeft.y = 96; break; // S
      case 9: topLeft.x = 96; topLeft.y = 96; break; // SE
    }
    image.paintAt(g, 0, 0, sz.width, sz.height, 
      topLeft.x, topLeft.y, topLeft.x + sz.width, topLeft.y + sz.height);
  }
}

