/*
Same Game - My first application built in Kotlin
date: 19-III-2024
code: Przemyslaw R. Pietraszczyk
license: GPL v3
IDE: Android Studio Iguana
*/
package pietraszczyk.samegame;

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

public class Field {
    int index;
    int groupIndex;
    //String color;
    //int x, y;
    boolean visit;
    public java.util.List<String> groupList = new ArrayList<String>();
    public java.util.List<Integer> listIndex = new ArrayList<Integer>();

    public int value; // czy kolor (0) czy bialy (1)
    public int resource;



	JButton button;
    private Color colorHide, color;
    private int x,y;
    boolean found;
    //boolean reserve;
    boolean postReserve;


	public Field () {
        postReserve=false;
    }

    public Field(int i, int j) {

        visit = false;
        value = 0;

		button = new JButton("?");
        color=new Color(255,255,255);
        y = i;
        x = j;
        found=false;
       // reserve=false;
        postReserve=false;

    }
    
    public void reset() {
		visit = false;
        value = 0;
	}
	
    public void setIndex(int n) {
        int index = n;
    }

    public void setGroupIndex(int n) {
        groupIndex = n;
    }

    public void setColorName(Color c) {
        color = c;
    }

    public void setVelocity(int _x, int _y) {
        x = _x;
        y = _y;
    }

    public int getGroupIndex() {
        return groupIndex;
    }

    public int getVelocityY() {
        return y;
    }

    public int getVelocityX() {
        return x;
    }

	public Color getColorHide(){
        return colorHide;
    }
    public Color getColor() {
        return color;
    }
    public void setColorHide(int r, int g, int b){
        color=new Color(r,g,b);
    }
    public void setFound(boolean f){
        found=f;
    }
    public boolean getFound(){
        return found;
    }




	private String toRgb(Color arg){
		return String.format("%02X%02X%02X", arg.getRed(), arg.getGreen(), arg.getBlue());
	}





    public void like(Color color, Field [][] squares, int y, int x, int origy, int origx){

		Color white = new Color(255,255, 255);

     //   if ( squares[y][x].getColor() != white) {
       //     if (squares[y][x].getColor()  == color) {
       //System.out.println("Adr: "+squares[y][x].getColor());
       if (squares[y][x].visit == false) {
		 if ( (toRgb(squares[y][x].getColor()).equals(toRgb(white))) == false) {
            if (toRgb(squares[y][x].getColor()).equals(toRgb(color))) {
						
				//if (squares[y][x].visit == false) {
				//		squares[origy][origx].groupList.add(String.valueOf(squares[y][x].getGroupIndex()));
                //        squares[origy][origx].listIndex.add(squares[y][x].getGroupIndex());
				//}
						
						
                if (y >= 1) {
                    if (squares[y - 1][x].visit == false) {
                        squares[origy][origx].groupList.add(String.valueOf(squares[y][x].getGroupIndex()));
                        squares[origy][origx].listIndex.add(squares[y][x].getGroupIndex());

                        squares[y][x].visit = true;
                        like(color, squares, y - 1, x, origy, origx);
                    }
                }
                if (x >= 1) {
                    if (squares[y][x - 1].visit == false) {
                        squares[origy][origx].groupList.add(String.valueOf(squares[y][x].getGroupIndex()));
                        squares[origy][origx].listIndex.add(squares[y][x].getGroupIndex());

                        squares[y][x].visit = true;
                        like(color, squares, y, x - 1, origy, origx);
                    }
                }

                if (y < 8) {
                    if (squares[y + 1][x].visit == false) {
                        squares[origy][origx].groupList.add(String.valueOf(squares[y][x].getGroupIndex()));
                        squares[origy][origx].listIndex.add(squares[y][x].getGroupIndex());

                        squares[y][x].visit = true;
                        like(color, squares, y + 1, x, origy, origx);
                    }
                }

                if (x < 5) {
                    if (squares[y][x + 1].visit == false) {
                        squares[origy][origx].groupList.add(String.valueOf(squares[y][x].getGroupIndex()));
                        squares[origy][origx].listIndex.add(squares[y][x].getGroupIndex());

                        squares[y][x].visit = true;
                        like(color, squares, y, x + 1, origy, origx);
                    }
                }
                squares[y][x].visit = false;
            }

        }
	}
    }

    // Mozliwe ze wogole nie potrzebne
    public void blank() {
        this.setColorName(new Color(255, 255, 255));
        this.value = 1;

    }

    public void toUp(Field [][] squares, int y, int x) {

        int n = 9;
        Color temp; // String
        int v; // = new Box();
        for(int i=0; i < n; i++){
            for(int j=1; j < (n-i); j++){
                if(squares[j-1][x].value > squares[j][x].value) {
                    //swap elements
                    temp = squares[j-1][x].getColor();
                    v =  squares[j-1][x].value;
                    squares[j-1][x].setColorName(squares[j][x].getColor());
                    squares[j-1][x].value = squares[j][x].value;
                    squares[j][x].setColorName(temp);
                    squares[j][x].value = v;
                }

            }
        }
    }

    public void toLeft(Field [][] squares, int y, int x) {

        int n = 6;
        Color temp;  //String
        int v; // = new Box();
        for(int i=0; i < n; i++){
            for(int j=1; j < (n-i); j++){
                    if(squares[y][j-1].value > squares[y][j].value) {
                        //swap elements
                    temp = squares[y][j-1].getColor();
                    v =  squares[y][j-1].value;
                    squares[y][j-1].setColorName(squares[y][j].getColor());
                    squares[y][j-1].value = squares[y][j].value;
                    squares[y][j].setColorName(temp);
                    squares[y][j].value = v;
                }

            }
        }
    }

    public void removeDuplicates() {

        java.util.List<String> tmp = new ArrayList<String> ();

        for(String item:this.groupList){
            boolean find = false;
            for (String t:tmp) {
                if (t.equals(item)){
                    find = true;
                }
            }
            if (find == false) {
                tmp.add(item);
            }
        }
        this.groupList.clear();
        this.groupList = tmp;
    }

    public void removeDuplicatesIndex() {

        java.util.List<Integer> tmp = new ArrayList<Integer> ();

        for(Integer item:this.listIndex){
            boolean find = false;
            for (Integer t:tmp) {
                if (t.equals(item)){
                    find = true;
                }
            }
            if (find == false) {
                tmp.add(item);
            }
        }
        this.listIndex.clear();
        this.listIndex = tmp;
    }

    public void clearVisit(){
        this.visit = false;
    }

    public void sort() {

        // Zbyt nowe dopiero od API 24
        //String[] strings = b.groupList.toArray(String[]::new);
        String[] strings = new String[this.groupList.size()];
        for (int i = 0; i < this.groupList.size(); i++) {
            strings[i] = this.groupList.get(i);
        }

        Integer[] numbers = new Integer[strings.length];
        for(int i = 0;i < strings.length;i++)
        {
            try
            {
                numbers[i] = Integer.parseInt(strings[i]);
            }
            catch (NumberFormatException nfe)
            {
                numbers[i] = null;
            }
        }

        Arrays.sort(numbers, Collections.reverseOrder());

        this.groupList.clear();
        for(int i = 0;i < numbers.length;i++)
        {
            try
            {
                this.groupList.add(Integer.toString(numbers[i]));
            }
            catch (NumberFormatException nfe)
            {
                //numbers[i] = null;
            }
        }

    }

    public void sortReverseIndex() {
        // Zbyt nowe dopiero od API 24
        //String[] strings = b.groupList.toArray(String[]::new);
        Integer[] integers = new Integer[this.listIndex.size()];
        for (int i = 0; i < this.listIndex.size(); i++) {
            integers[i] = this.listIndex.get(i);
        }

        Arrays.sort(integers, Collections.reverseOrder());

        this.listIndex.clear();
        for(int i = 0;i < integers.length;i++)
        {
            try
            {
                this.listIndex.add(integers[i]);
            }
            catch (NumberFormatException nfe)
            {
            }
        }
    }
}
