import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DataArray {
	
	protected ArrayList<int[]> dataarray = new ArrayList<int[]>();
	protected int dimension;

	public DataArray(){
		dataarray = new ArrayList<int[]>();
	}
	
	public DataArray(ArrayList<int[]> as){
		dataarray = as;
		dimension = as.get(0).length;
	}
	
	public void add(int[] add){
		this.dataarray.add(add);
	}
	
	public ArrayList<int[]> getDataArray(){
		return dataarray;
	}
	
	public void setDataArray(ArrayList<int[]> as){
		dataarray=as;
	}
	
	public void setDim(int dim){
		dimension = dim;
	}
	
	public void initialize(){
		dimension = dataarray.get(0).length;
	}
	
	public int getDim(){
		return dimension;
	}
	
	public void clear(){
		dataarray.clear();
	}
	
	public int size(){
		int si = this.getDataArray().size();
		return si;
	}
	
	public void data2file(String s) throws IOException{
		FileWriter fw = new FileWriter(s);
        BufferedWriter bw = new BufferedWriter(fw);
        ArrayList<int[]> here = this.getDataArray();
        int len = here.size();
        this.initialize();
        bw.write(""+len+","+this.getDim());
        bw.newLine();
        for(int i=0;i<len;i++){
        	String zu = here.get(i)[0]+","+here.get(i)[1]+","+here.get(i)[2]+","+here.get(i)[3]+","+here.get(i)[4];
        	bw.write(zu);
        	bw.newLine();
        }
        bw.close();
        fw.close();
	}
	
	public void file2data(String s) throws IOException{
		ArrayList<int[]> dadat = new ArrayList<int[]>();
		FileReader fr = new FileReader(s);
	    BufferedReader br = new BufferedReader(fr);
	    String input0 = br.readLine();
	    int len=0;
	    int dim=0;

        if(input0.toLowerCase().contains(",")){
        	String length = input0.substring(0,input0.indexOf(","));
        	length = length.replaceAll("[^\\d]", "");
        	len=Integer.parseInt(length);
        	String dime = input0.substring(input0.indexOf(",")+1,(input0.length()));
        	dime = dime.replaceAll("[^\\d]", "");
        	dim = Integer.parseInt(dime);
        } else {
        	System.out.println("Error! Error!");
        	System.exit(1);
        }
        
        ArrayList<Integer> zwischen = new ArrayList<>();
        int jo=0;
	    for(int i=0;i<len;i++){
	    	String input = br.readLine();
			while(input.contains(",")){
				jo=Integer.parseInt(input.substring(0,input.indexOf(",")));
				zwischen.add(jo);
				input = (String) input.subSequence(input.indexOf(",")+1, input.length());
			}
			jo=Integer.parseInt(input);
			zwischen.add(jo);
			int[] zw = new int[zwischen.size()];
			for(int j=0;j<zwischen.size();j++){
				zw[j]=zwischen.get(j);
			}
			dadat.add(zw);
			zwischen.clear();
	    }
	    
	    br.close();
        fr.close();
        
		this.clear();
		this.setDataArray(dadat);
	}
	
	public void print(){
		ArrayList<int[]> dea = this.getDataArray();
		int len = dea.size();
		int dim = dea.get(0).length;
		for(int i=0;i<len;i++){
			int[] now = dea.get(i);
			for(int j=0;j<(dim-1);j++){
				System.out.print(now[j]+",");
			}
			System.out.println(now[dim-1]);
		}
	}
	

	
	public Board data2board(int size1,int size2, int dim1, int dim2){
		Board test = new Board(size1,size2);
		ArrayList<int[]> dea = this.getDataArray();

		test.clear();
		for(int i=0;i<this.size();i++){
			int[] now = dea.get(i);
			int a = now[(dim1-1)];
			int b = now[(dim2-1)];
			int c = (test.getInput(a, b)+1);
			test.setInput(a, b, c);
		}
		test.input2board();
		return test;
	}
	
}
