package viewer;

import maze.Cell;
import maze.Maze;
import maze.Wall;

import javax.swing.*;
import java.awt.*;
import java.util.List;


/**
 * @author Nick Hirakawa
 */
public class MazePanel extends JPanel {

    private Maze maze;
    private int cellSize;
    private int gapSize;

    public MazePanel(Maze maze, int cellSize, int gapSize){
        this.maze = maze;
        this.cellSize = cellSize;
        this.gapSize = gapSize;
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);

        drawBorders(this.cellSize, this.gapSize, g);

        for(List<Cell> row : this.maze.getCells()){
            for(Cell cell : row){
                drawCell(cell, this.cellSize, this.gapSize, g);
            }
        }

        for(Wall wall : this.maze.getWalls()){
            drawWall(wall, this.cellSize, this.gapSize, g);
        }
    }

    private void drawCell(Cell cell, int cellSize, int gapSize, Graphics g){
        int x1 = cell.getX() * (cellSize + gapSize) + gapSize;
        int y1 = cell.getY() * (cellSize + gapSize) + gapSize;
        int width = cellSize;
        int height = cellSize;
        g.setColor(Color.RED);
        g.fillRect(x1, y1, width, height);
    }

    private void drawWall(Wall wall, int cellSize, int gapSize, Graphics g){
        Cell cell1 = wall.getCell1();
        Cell cell2 = wall.getCell2();
        if(cell1.getX() == cell2.getX()){
            if(cell1.getY() > cell2.getY()){
                drawBottomWall(cell1.getX(), cell1.getY(), cellSize, gapSize, g);
            } else {
                drawTopWall(cell1.getX(), cell1.getY(), cellSize, gapSize, g);
            }
        } else {
            if(cell1.getX() < cell2.getX()){
                drawRightWall(cell1.getX(), cell1.getY(), cellSize, gapSize, g);
            }else{
                drawLeftWall(cell1.getX(), cell1.getY(), cellSize, gapSize, g);
            }
        }
    }

    private void drawTopWall(int x, int y, int cellSize, int gapSize, Graphics g){
        g.setColor(Color.BLACK);
        int x1 = x * (cellSize + gapSize) + gapSize;
        int y1 = y * (cellSize + gapSize) + gapSize + cellSize;
        int width = cellSize;
        int height = gapSize;
        g.fillRect(x1, y1, width, height);
    }

    private void drawBottomWall(int x, int y, int cellSize, int gapSize, Graphics g){
        g.setColor(Color.BLACK);
        int x1 = x * (cellSize + gapSize) + gapSize;
        int y1 = y * (cellSize + gapSize);
        int width = cellSize;
        int height = gapSize;
        g.fillRect(x1, y1, width, height);
    }

    private void drawLeftWall(int x, int y, int cellSize, int gapSize, Graphics g){
        g.setColor(Color.BLACK);
        int x1 = x * (cellSize + gapSize);
        int y1 = y * (cellSize + gapSize) + gapSize;
        int width = gapSize;
        int height = cellSize;
        g.fillRect(x1, y1, width, height);
    }

    private void drawRightWall(int x, int y, int cellSize, int gapSize, Graphics g){
        g.setColor(Color.BLACK);
        int x1 = x * (cellSize + gapSize) + gapSize + cellSize;
        int y1 = y * (cellSize + gapSize) + gapSize;
        int width = gapSize;
        int height = cellSize;
        g.fillRect(x1, y1, width, height);
    }

    private void drawBorders(int cellSize, int gapSize, Graphics g){
        g.setColor(Color.BLACK);
        int x1 = 0;
        int y1 = 0;
        int width = maze.getWidth() * (gapSize + cellSize) + gapSize;
        int height = gapSize;
        g.fillRect(x1, y1, width, height);

        width = gapSize;
        height = maze.getHeight() * (gapSize + cellSize) + gapSize;
        g.fillRect(x1, y1, width, height);

        x1 = 0;
        y1 = maze.getHeight() * (gapSize + cellSize);
        width = maze.getWidth() * (cellSize + gapSize) + gapSize;
        height = gapSize;
        g.fillRect(x1, y1, width, height);

        x1 = maze.getWidth() * (gapSize + cellSize);
        y1 = 0;
        width = gapSize;
        height = maze.getHeight() * (cellSize + gapSize) + gapSize;
        g.fillRect(x1, y1, width, height);
    }
}

@@ -428,24 +428,65 @@
  = gapSize;%0A
+        this.setBackground(Color.WHITE);%0A
     %7D%0A%0A    p
@@ -1204,13 +1204,199 @@
 lor.
-RED);
+WHITE);%0A        if(cell.equals(this.maze.getStart()))%7B%0A            g.setColor(Color.GREEN);%0A        %7D%0A        if(cell.equals(this.maze.getEnd()))%7B%0A            g.setColor(Color.RED);%0A        %7D
 %0A   

Set cell backgrounds