package calc;
import calc.cell.*;
import java.lang.*;
import java.util.*;
import java.io.*;

 public class Sheet {
	private Map<CellId, Cell> cells;
	public Sheet(Map<CellId, Cell> cells){
		this.cells=cells;
	}
	public Cell getCell(CellId which){
		if(which==null)return null;
		//System.out.println(cells.get(which).show());
		//System.out.println(which.hashCode());
		return cells.get(which);
	}

	private static String ids = "ABCDEFGHIJKLMNOPQRSTUVXYZ";

	public static Sheet loadFromFile(String fileName) throws IOException{
		Sheet sheet = new Sheet(new TreeMap<CellId, Cell>());
		if(!new File(fileName).exists()){return null;}
		//LineNumberReader in = new LineNumberReader(new FileReader(fileName));//3ICE: Végül nem használtam a sorszám lekérdező függvényt...
		BufferedReader r = new BufferedReader(new FileReader(new File(fileName)));
		CellId id;
		Cell cell;
		int betu, szam=1;
		for(String ss = r.readLine(); ss != null; ss = r.readLine()){
			//System.out.println("Processing "+ss);
			betu=0;
			String sss[] = ss.split(";");
			for(String s : sss){
				//System.out.println("Creating "+s+" "+ids.charAt(betu)+szam);
				id = new CellId(ids.charAt(betu),szam);
				betu++;
				//System.out.println(s);
				switch(s.charAt(0)){
					case '=':
					cell=new FormulaCell(sheet); break;
					default:
					cell=new IntegerCell(sheet);
				}
				cell.edit(s);
				//System.out.println(id.hashCode());
				//System.out.println(cell.show());
				sheet.cells.put(id, cell);
			}
			szam++;
		}
		return sheet;
	}
}