
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.Timer;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

class MainFrameTetries extends JPanel implements ActionListener {

    private int x, y;
    private int currentRow = 0, currentColumn = 4;
    private Timer timer;
    private int rows = 20, columns = 10;
    private JPanel[][] panel;
    private Random random;
    private Color randomColor;
    private MyKeyListener myKeyListener = new MyKeyListener();
    private List<Point[]> arrayList = new ArrayList<Point[]>();

    public MainFrameTetries(MainGame mainGame) {
        addKeyListener(myKeyListener);
        setFocusable(true);
        requestFocusInWindow();
        random = new Random();
        createMainPanel(mainGame);
        creatwTetrisPanel();
        randomColor = randomColor();
        timer = new Timer(700, this);
        timer.start();
    }

    private void createMainPanel(MainGame mainGame) {
        setPreferredSize(new Dimension(200, 400));
        setSize(getPreferredSize());
        setLayout(new GridLayout(rows, columns));
        System.out.println(getPreferredSize().height);
        x = mainGame.getWidth() / 2 - getWidth() / 2;
        y = mainGame.getHeight() / 2 - getHeight() / 2;
        setLocation(x, y);
    }

    private void creatwTetrisPanel() {
        panel = new JPanel[rows][columns];

        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < columns; ++j) {
                panel[i][j] = new JPanel();
                panel[i][j].setBackground(Color.WHITE);
                panel[i][j].setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
                add(panel[i][j]);
            }
        }
    }

    public void actionPerformed(ActionEvent e) {
        move_Square_Vertical();
        drawPreviousSquares();
    }
    
    private void drawPreviousSquares() {
    	for (int i = 0; i < arrayList.size(); i++) {
    		Point[] p = arrayList.get(i);
    		panel[(int)p[0].getX()][(int)p[0].getY()].setBackground(Color.RED);
    		panel[(int)p[1].getX()][(int)p[1].getY()].setBackground(Color.RED);
    		panel[(int)p[2].getX()][(int)p[2].getY()].setBackground(Color.RED);
    		panel[(int)p[3].getX()][(int)p[3].getY()].setBackground(Color.RED);
    	}
    }

    private Color randomColor() {
        return new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
    }

    public void clearPanel() {
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < columns; ++j) {
                panel[i][j].setBackground(Color.WHITE);
            }
        }
    }

    private void move_Square_Vertical() {
        clearPanel();
        panel[currentRow][currentColumn].setBackground(randomColor);
        panel[currentRow][currentColumn + 1].setBackground(randomColor);
        panel[currentRow + 1][currentColumn].setBackground(randomColor);
        panel[currentRow + 1][currentColumn + 1].setBackground(randomColor);
        if (currentRow != rows - 2) {
            currentRow++;
        } else {
            Point[] p = {new Point(currentRow, currentColumn), new Point(currentRow, currentColumn + 1), 
            			new Point(currentRow + 1, currentColumn), new Point(currentRow +1, currentColumn + 1)};
            arrayList.add(p);
            currentRow = 0;  // أقصدبذلك حينما تصل للصف الإخير أبدأ مرة أخرى من قيمة ال row =0 , ال col=4
            currentColumn = 4;
        }
    }

    private void move_Square_Right_Horizontal() {
        if (currentRow == rows - 2) {
            currentColumn = currentColumn - 1;
            //    move_Square_Vertical(0, 4);
        }
        clearPanel();
        currentColumn++;
        if (currentColumn + 1 > columns - 1) {
            currentColumn--;
        }
        panel[currentRow][currentColumn].setBackground(randomColor);
        panel[currentRow][currentColumn + 1].setBackground(randomColor);
        panel[currentRow + 1][currentColumn].setBackground(randomColor);
        panel[currentRow + 1][currentColumn + 1].setBackground(randomColor);
    }

    private void move_Square_Left_Horizontal() {
        if (currentRow == rows - 2) {
            currentColumn = currentColumn + 1;
            //      move_Square_Vertical(0,4);
        }
        clearPanel();
        currentColumn--;
        if (currentColumn < 0) {
            currentColumn++;
        }
        panel[currentRow][currentColumn].setBackground(randomColor);
        panel[currentRow][currentColumn + 1].setBackground(randomColor);
        panel[currentRow + 1][currentColumn].setBackground(randomColor);
        panel[currentRow + 1][currentColumn + 1].setBackground(randomColor);
    }

    private class MyKeyListener extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            int location = e.getKeyCode();
            if (location == KeyEvent.VK_RIGHT) {
                move_Square_Right_Horizontal();
            } else if (location == KeyEvent.VK_LEFT) {
                move_Square_Left_Horizontal();

            }
        }
    }
}
