package jp.satomichan.nucalgen;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.CellReference;

public class NamedAreaStore {

	private static NamedAreaStore instance = new NamedAreaStore();
	
	private Map<String, String> namedAreaMap = new HashMap<String, String>();
	
	private NamedAreaStore() {
		//
	}
	
	static NamedAreaStore getInstance() {
		return NamedAreaStore.instance;
	}
	
	static String getAreaString(int row1, int col1, int row2, int col2) {
		String ret = new CellReference(row1, col1, true, true).formatAsString();
		
		if(row1 != row2 || col1 != col2) {
			ret = ret + ":" + new CellReference(row2, col2, true, true).formatAsString();
		}
		
		return ret;
	}
	
	String save(String areaname, int row1, int col1, int row2, int col2) {
		String area = getAreaString(row1, col1, row2, col2);
		areaname = Matcher.quoteReplacement(areaname);
		this.namedAreaMap.put(areaname, area);
		
		return area;
	}
	
	
	String save(String areaname, int row, int col) {
		return this.save(areaname, row, col, row, col);
	}
	
	
	String save(String areaname, Cell cell) {
		return this.save(areaname, cell.getRowIndex(), cell.getColumnIndex());
	}
	
	
	String load(String areaName) {
		areaName = Matcher.quoteReplacement(areaName);
		return this.namedAreaMap.get(areaName);
	}
	
	
	Set<Entry<String, String>> entrySet(){
		return this.namedAreaMap.entrySet();
	}
	
}
