/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.game;

import java.util.ArrayList;

/**
 *
 * @author Dániel
 */
public class RecursiveNullFinder extends Thread
{
    private Controller controller;
    private int x;
    private int y;
    private ArrayList<int[]> nulls;
    private ArrayList<int[]> wasThere;
    private int depth = 0;
    
    public RecursiveNullFinder(int x, int y,ArrayList<int[]> nulls,ArrayList<int[]> wasThere,Controller controller)
    {
        this.x      =x;
        this.y      =y;
        this.nulls  = nulls;
        this.wasThere   = wasThere;
        this.controller = controller;
    }
    private ArrayList<int[]> getNullFieldAround()
    {
        if( wasThere == null )
            wasThere = new ArrayList<>();
        
        ArrayList<int[]> nullsAround;
        if( nulls != null )
            nullsAround = nulls;
        else
            nullsAround = new ArrayList<>();
        
        int pos[] = new int[2];
        pos[0] = x;
        pos[1] = y;
        String[] neighbours = controller.model.getNeighbours(pos);
        
        for (int i = 0; i < 9; i++) 
        {
            int[] posN = this.controller.model.getPosFromString(neighbours[i]);
            if( posN[0] == -1 || posN[1] == -1 )
            {
                int dummy = 10;
            }
            else if( this.controller.model.calculateMinesAround(posN[0], posN[1]) == 0 )
            {
                if( !this.isContains(wasThere, posN) )
                {
                    nullsAround.add(posN);
                    wasThere.add(posN);
                    
                    this.x = posN[0];
                    this.y = posN[1];
                    this.nulls = nullsAround;
                    
                    this.depth++;
                    System.out.println(this.depth);
                    ArrayList<int[]> newNulls = this.getNullFieldAround();
                    
                    
                   // nullsAround.addAll(newNulls);
                    for( int[] actNull : newNulls )
                    {
                        this.addUnique(nullsAround, actNull);
                    }
                } 
            }
        }
        return nullsAround;
    }
    public Boolean isContains(ArrayList<int[]> container, int[] position)
    {
        for( int[] act : container)
        {
            if( act[0] == position[0] && act[1] == position[1] )
                return true;
        }
        return false;
    }
    public ArrayList<int[]> addUnique(ArrayList<int[]> to, int[] add)
    {
        for (int[] actTo : to  ) 
        {
            if( actTo == add )
                return to;
        }
        to.add(add);
        return to;
    }
    @Override
    public void run()
    {
        if( controller.model.nulls == null )
            controller.model.nulls = new ArrayList<int[]>();
        controller.model.nulls = this.getNullFieldAround();
        controller.model.calculateDone = true;
    }
}