package LevelEditor;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import PrintGame.*;
import PrintGame.Robot;

public class LabyrinthCanvas extends Canvas
 implements MouseListener
  
{
  private LabyrinthField labyrinth_field_;
  private Direction start_direction_;
  protected Image img_way_, img_wall_, img_robot_, img_diamond_;
  protected int draw_selector_;
  
  public final static int DRAW_WALL       = 1;
  public final static int DRAW_WAY        = 2;
  public final static int DRAW_DIAMOND    = 3;
  public final static int DRAW_STARTPOINT = 4;
  
  public LabyrinthCanvas(LabyrinthField labyrinth_field)
  {
    labyrinth_field_ = labyrinth_field;
    img_way_     = Toolkit.getDefaultToolkit().getImage(new File("PrintGame\\way_3.jpg").getAbsolutePath());
    img_wall_    = Toolkit.getDefaultToolkit().getImage(new File("PrintGame\\way_2.jpg").getAbsolutePath());
    img_diamond_ = Toolkit.getDefaultToolkit().getImage(new File("PrintGame\\diamond.gif").getAbsolutePath());     
    addMouseListener(this);
    draw_selector_ = DRAW_WAY;
    start_direction_ = new Direction();
 }
  
  public void paint(Graphics g)
  {
    int hposition, vposition, cols_counter, rows_counter;
    hposition=0;
    vposition=0;
    cols_counter=0;
    rows_counter=0;

    while(rows_counter<labyrinth_field_.getNumRows())
      {
        while(cols_counter<labyrinth_field_.getNumCols())
        {
          if(labyrinth_field_.isWay(rows_counter,cols_counter)==true)
          {
              g.drawImage(img_way_, hposition, vposition, this);
              if(labyrinth_field_.containsDiamond(rows_counter,cols_counter)==true)
                g.drawImage(img_diamond_, hposition, vposition, this);
              if(labyrinth_field_.isStartpoint(rows_counter,cols_counter)==true)
                g.drawString("S", hposition+5, vposition+PrintLabyrinth.CELL_HEIGHT-5);
          }
          else
            g.drawImage(img_wall_, hposition, vposition, this);

          cols_counter=cols_counter+1;
          hposition=hposition+PrintLabyrinth.CELL_WIDTH;
        }
        rows_counter=rows_counter+1;
        vposition=vposition+PrintLabyrinth.CELL_HEIGHT;
        hposition=0;
        cols_counter=0;
      }
  }
  
  public void setDrawSelector(int draw_selector)
  {
    draw_selector_ = draw_selector;
  }
  
  public Direction getStartDirection()
  {
    return start_direction_;
  }
  
  public void setStartDirection(Direction start_direction)
  {
    start_direction_ = start_direction;
  }
  
  public Dimension getMinimumSize()
  {
    return new Dimension(labyrinth_field_.getNumCols()*PrintLabyrinth.CELL_WIDTH, 
                         labyrinth_field_.getNumRows()*PrintLabyrinth.CELL_HEIGHT);
  }
  
  public Dimension getPreferredSize()
  {
    return getMinimumSize();
  }

  public void mouseClicked(MouseEvent e) 
  {
    int current_row = e.getY()/PrintLabyrinth.CELL_HEIGHT;
    int current_col = e.getX()/PrintLabyrinth.CELL_WIDTH;
    
    int cols = labyrinth_field_.getNumCols();
    int rows = labyrinth_field_.getNumRows();
    
    try
    {
      switch (draw_selector_)
      {
        case DRAW_WALL:
          if(!labyrinth_field_.isStartpoint(current_row, current_col))
            labyrinth_field_.convertToWall(current_row, current_col);
          break;
        case DRAW_WAY:
          labyrinth_field_.convertToWay(current_row, current_col);
          break;
        case DRAW_DIAMOND:   
          if (!labyrinth_field_.containsDiamond(current_row, current_col))
          {
            if(!labyrinth_field_.isStartpoint(current_row, current_col))
              labyrinth_field_.setDiamond(current_row, current_col);           
          }
 
          else
            labyrinth_field_.removeDiamond(current_row, current_col);
          break;
        case DRAW_STARTPOINT:
          if (current_row == 0 || current_row == rows-1 || current_col == 0 || current_col == cols-1)
          {
            if (!labyrinth_field_.isStartpoint(current_row, current_col))
            {
              if(!labyrinth_field_.containsDiamond(current_row, current_col))
              {
                labyrinth_field_.convertToStartpoint(current_row, current_col);
                if (current_row == 0)
                  start_direction_.setDirection(Direction.DIRECTION_DOWN);
                else if (current_row == rows-1)
                  start_direction_.setDirection(Direction.DIRECTION_UP);
                else if (current_col == 0)
                  start_direction_.setDirection(Direction.DIRECTION_RIGHT);
                else if (current_col == cols-1)
                  start_direction_.setDirection(Direction.DIRECTION_LEFT);
              }
            }
            else
            {
              labyrinth_field_.removeStartpoint(current_row, current_col);
              start_direction_ = new Direction();
            }
          }
          break;
        default:
          System.out.println("Illegal draw_selector");
      }
    }
    catch (IllegalArgumentException ex) {}
    catch (IllegalStateException ex) {}
    catch (IllegalDirectionException ex) { }
    repaint(current_col*PrintLabyrinth.CELL_WIDTH, current_row*PrintLabyrinth.CELL_HEIGHT, 
            PrintLabyrinth.CELL_WIDTH, PrintLabyrinth.CELL_HEIGHT);
  }
  
  public void mouseEntered(MouseEvent e) {}
  
  public void mouseExited(MouseEvent e) {}
  
  public void mousePressed(MouseEvent e) {}
  
  public void mouseReleased(MouseEvent e) {}
  
}