import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.util.ArrayList;


public class TextFilter extends KeyAdapter {

    private JTextField Text;
    private int GRID_SIZE;
    private JTextField[][] grid;
    private int row;
    private int col;
    private ArrayList<Integer> bad_numbers = new ArrayList<Integer>();


    public TextFilter (JTextField t,int GRID_SIZE, JTextField[][] grid, int i, int j) {

	this.Text = t;
	this.GRID_SIZE = GRID_SIZE;
	this.grid = grid;
	this.row = i;
	this.col = j;
    }
    

    @Override

    public void keyTyped(KeyEvent e) {
	
	char chiffre = e.getKeyChar();
	int taille = this.Text.getText().length();

	if ( ((chiffre < '0') || (chiffre > '9')) && (chiffre != KeyEvent.VK_BACK_SPACE)) {

	    e.consume(); // ignorer l'événement
	}

	if ( ((chiffre >= '0') && (chiffre <= '9')) && (chiffre != KeyEvent.VK_ENTER)) {

	    GrilleValide(chiffre);
	}

	if ((!bad_numbers.isEmpty()) && (chiffre == KeyEvent.VK_BACK_SPACE)) {

	    Text.setBackground(Color.white);

	    while(!bad_numbers.isEmpty()) {

		grid[bad_numbers.removeFirst()][bad_numbers.removeFirst()].setBackground(Color.white);
	    }
	}

	if ( (bad_numbers.isEmpty()) && (chiffre == KeyEvent.VK_BACK_SPACE)) {

	    Coloriage();
	}

	if ( taille >= 1 )  {

	    e.consume();
	}
    }
    

    public void GrilleValide(char chiffre) {
        
	int evaluant = Integer.parseInt(Character.toString(chiffre));

	// Test de validité sur les lignes
	for (int row2 = row + 1; row2 < this.GRID_SIZE; row2++) {
	    if (!grid[row2][col].getText().isEmpty()) {
		int comparateur = Integer.parseInt(grid[row2][col].getText());

		if (evaluant == comparateur) {
		    this.grid[row2][col].setBackground(Color.red);
		    this.grid[row][col].setBackground(Color.red);
		    this.bad_numbers.add(row2);
		    this.bad_numbers.add(col);
		    
		}
	    }
	}
	for (int row2 = row - 1; row2 >= 0; row2--) {
	    if (!grid[row2][col].getText().isEmpty()) {
		int comparateur = Integer.parseInt(grid[row2][col].getText());
			    
		if (evaluant == comparateur) {
		    this.grid[row2][col].setBackground(Color.red);
		    this.grid[row][col].setBackground(Color.red);
		    this.bad_numbers.add(row2);
		    this.bad_numbers.add(col);
		}
	    }
	}
		    
	// Test de validité sur les colonnes
	for (int col2 = col + 1; col2 < this.GRID_SIZE; col2++) {
	    if (!grid[row][col2].getText().isEmpty()) {
		int comparateur = Integer.parseInt(grid[row][col2].getText());

		if (evaluant == comparateur) {
		    this.grid[row][col2].setBackground(Color.red);
		    this.grid[row][col].setBackground(Color.red);
		    this.bad_numbers.add(row);
		    this.bad_numbers.add(col2);
		}
	    }

	}
	for (int col2 = col - 1; col2 >= 0; col2--) {
	    if (!grid[row][col2].getText().isEmpty()) {
		int comparateur = Integer.parseInt(grid[row][col2].getText());
		
		if (evaluant == comparateur) {
		    this.grid[row][col2].setBackground(Color.red);
		    this.grid[row][col].setBackground(Color.red);
		    this.bad_numbers.add(row);
		    this.bad_numbers.add(col2);
		}
	    }
	}
		    
	// Vérifier la validité dans les régions
	int rowregion = this.row/3*3;
	int colregion = this.col/3*3;
	for (int row2 = rowregion; row2 < rowregion + 3; row2++) {
	    for (int col2 = colregion; col2 < colregion + 3; col2++) {
			  
                           
		if (row2 != row && col2 != col ) {
                                
		    if (!grid[row2][col2].getText().isEmpty()) {
			int comparateur = Integer.parseInt(grid[row2][col2].getText());
				    
			if (evaluant == comparateur) {
			    this.grid[row2][col2].setBackground(Color.red);
			    this.grid[row][col].setBackground(Color.red);
			    this.bad_numbers.add(row2);
			    this.bad_numbers.add(col2);
			}
		    }
		}
	    }
	}
    }

    public void Coloriage() {

	//coloriage sur les lignes
	for (int row2 = 0; row2 < this.GRID_SIZE; row2++) {

	    if (this.grid[row2][this.col].getBackground() == Color.red)  {

		this.grid[row2][this.col].setBackground(Color.white);
	    }
	}

	//coloriage sur les colonnes
	for (int col2 = 0; col2 < this.GRID_SIZE; col2++) {

	    if (this.grid[this.row][col2].getBackground() == Color.red)  {

		this.grid[this.row][col2].setBackground(Color.white);
	    }
	}

	//coloriage des régions
	int rowregion = this.row/3*3;
	int colregion = this.col/3*3;
	for (int row2 = rowregion; row2 < rowregion + 3; row2++) {
	    for (int col2 = colregion; col2 < colregion + 3; col2++) {

		if (this.grid[row2][col2].getBackground() == Color.red)  {

		    this.grid[row2][col2].setBackground(Color.white);
		}
	    }
	}
    }		    		 
}