/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GhepTranh;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.Random;
import javax.swing.JButton;

/**
 *
 * @author Administrator
 */
public class MainControl {

    View v;
    int size, sizeOfCell = 100;
    JButton[][] btn;
    Random rd = new Random();

    public MainControl() {
    }

    public MainControl(View v) {
        this.v = v;

    }

    public void control() {
        v.setVisible(true);
        initThread();
        t.start();
        xulyBtnDraw();

    }

    void xulyBtnDraw() {
        v.getBtnDraw().addActionListener((evt) -> {
            v.getPnlDisplay().removeAll();
            v.getLblDisplay().setText("");
            v.getLblCount().setText("0");
            flag = true;
            try {
                size = v.getCbxSize().getSelectedIndex() + 3;
            } catch (Exception e) {
                size = 5;
            }
            btn = new JButton[size][size];
            v.getPnlDisplay().setLayout(new GridLayout(size, size, 0, 0));
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    btn[i][j] = new JButton((i == 0 && j == 0) ? "" : (i * size + j) + "");
                    btn[i][j].setPreferredSize(new Dimension(sizeOfCell, sizeOfCell));
                    btn[i][j].setMargin(new Insets(2, 2, 2, 2));
                }
            }
            for (int k = 0; k < 50; k++) {
                int ii = 0, jj = 0;
                for (int i = 0; i < size; i++) {
                    for (int j = 0; j < size; j++) {
                        if (btn[i][j].getText().equals("")) {
                            ii = i;
                            jj = j;
                            break;
                        }
                    }
                    if (btn[ii][jj].getText().equals("")) {
                        break;
                    }
                }
                int choice;
                if (ii == 0) {
                    if (jj == 0) {
                        choice = rd.nextInt(2) + 1;
                    } else if (jj == size - 1) {
                        choice = rd.nextInt(2) * 2 + 1;
                    } else {
                        choice = rd.nextInt(3) + 1;
                    }
                } else if (ii == size - 1) {
                    if (jj == 0) {
                        choice = rd.nextInt(2) * 2;
                    } else if (jj == size - 1) {
                        choice = rd.nextInt(2) * 3;
                    } else {
                        int [] option = {0,2,3};
                        choice = option[rd.nextInt(3)];
                    }
                } else {
                    if (jj == 0) {
                        choice = rd.nextInt(3);
                    } else if (jj == size - 1) {
                        int [] option = {0,1,3};
                        choice = option[rd.nextInt(3)];
                    } else {
                        choice = rd.nextInt(4);
                    }
                }
                switch (choice) {
                    case 0:
                        moveDown(btn, ii - 1, jj);
                        break;
                    case 1:
                        moveUp(btn, ii + 1, jj);
                        break;
                    case 2:
                        moveLeft(btn, ii, jj + 1);
                        break;
                    case 3:
                        moveRight(btn, ii, jj - 1);
                        break;
                }
            }
            addActions();
            v.pack();
        });
    }
	
	private void addActions(){
		for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    final int ii = i;
                    final int jj = j;
                    btn[ii][jj].addActionListener((e1) -> {
                        if (ii - 1 >= 0 && btn[ii - 1][jj].getText().equals("")) {
                            v.getLblCount().setText((Integer.parseInt(v.getLblCount().getText()) + 1) + "");
                            moveUp(btn, ii, jj);
                        }
                        if (ii + 1 < size && btn[ii + 1][jj].getText().equals("")) {
                            v.getLblCount().setText((Integer.parseInt(v.getLblCount().getText()) + 1) + "");
                            moveDown(btn, ii, jj);
                        }
                        if (jj - 1 >= 0 && btn[ii][jj - 1].getText().equals("")) {
                            v.getLblCount().setText((Integer.parseInt(v.getLblCount().getText()) + 1) + "");
                            moveLeft(btn, ii, jj);
                        }
                        if (jj + 1 < size && btn[ii][jj + 1].getText().equals("")) {
                            v.getLblCount().setText((Integer.parseInt(v.getLblCount().getText()) + 1) + "");
                            moveRight(btn, ii, jj);
                        }
                        if (checkWin(btn, size)) {
                            v.getLblDisplay().setText("You win!");
                            flag = false;
                            for (int m = 0; m < size; m++) {
                                for (int n = 0; n < size; n++) {
                                    btn[m][n].setEnabled(false);
                                }
                            }
                        }
                    });
                    v.getPnlDisplay().add(btn[i][j]);
                }
            }
	}

    public static void moveUp(JButton[][] btn, int ii, int jj) {
        btn[ii - 1][jj].setText(btn[ii][jj].getText());
        btn[ii][jj].setText("");
    }

    public static void moveDown(JButton[][] btn, int ii, int jj) {
        btn[ii + 1][jj].setText(btn[ii][jj].getText());
        btn[ii][jj].setText("");
    }

    public static void moveLeft(JButton[][] btn, int ii, int jj) {
        btn[ii][jj - 1].setText(btn[ii][jj].getText());
        btn[ii][jj].setText("");
    }

    public static void moveRight(JButton[][] btn, int ii, int jj) {
        btn[ii][jj + 1].setText(btn[ii][jj].getText());
        btn[ii][jj].setText("");
    }

    boolean checkWin(JButton[][] btn, int size) {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (!btn[i][j].getText().equals((i == 0 && j == 0) ? "" : (i * size + j) + "")) {
                    return false;
                }
            }
        }
        return true;
    }

    Thread t;
    boolean flag;

    private void initThread() {
        t = new Thread() {
            @Override
            public void run() {
                while (true) {
                    if (flag) {
                        int length = v.getLblTime().getText().length() - 4;
                        v.getLblTime().setText((Integer.parseInt(v.getLblTime().getText().substring(0, length)) + 1) + " sec");
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                    }
                }
            }
        };
    }

    public static void main(String[] args) {
        new MainControl(new View()).control();

    }
}