/* XSSFCacheRecord.java

{{IS_NOTE
	Purpose:
		
	Description:
		
	History:
		Apr 25, 2012 6:44:57 PM , Created by Sam
}}IS_NOTE

Copyright (C) 2012 Potix Corporation. All Rights Reserved.

{{IS_RIGHT
}}IS_RIGHT
*/
package org.zkoss.poi.xssf.usermodel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBoolean;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCacheField;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDateTime;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIndex;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumber;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRecord;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSharedItems;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTString;
import org.w3c.dom.Node;
import org.zkoss.poi.ss.usermodel.PivotCache.CacheRecord;

public class XSSFCacheRecord implements CacheRecord, Serializable {
	private static final long serialVersionUID = 6090905779116889504L;
	private CTRecord _record;
	private List<CTCacheField> _fields;
	private List<Object> _data;
	
	/*package*/XSSFCacheRecord(CTRecord record, List<CTCacheField> fields) {
		_record = record;
		_fields = fields;
	}
	
	public List<Object> getData() {
		if (_data == null) {
			initData();
		}
		return _data;
	}
	
	private void initData() {
		_data = new ArrayList<Object>();
		
		HashMap<Integer, Object> dataMap = new HashMap<Integer, Object>();
		IndexMapper indexMapper = new IndexMapper(_record.getDomNode());
		
		List<CTIndex> xList = _record.getXList();
		if (xList != null) {
			for (CTIndex e : xList) {
				int index  = indexMapper.get(e.getDomNode());
				CTSharedItems sharedItems = _fields.get(index).getSharedItems();
				
				//TODO: sharedItems could contains number, string, date
				List<CTString> sList = sharedItems.getSList();
				String s = sList.get((int)e.getV()).getV();
				dataMap.put(index, s);
			}
		}
		
		List<CTNumber> nList = _record.getNList();
		if (nList != null) {
			for (CTNumber n : nList) {
				int index  = indexMapper.get(n.getDomNode());
				dataMap.put(index, n.getV());
			}
		}
		
		List<CTString> sList = _record.getSList();
		if (sList != null) {
			for (CTString s : sList) {
				int index  = indexMapper.get(s.getDomNode());
				dataMap.put(index, s.getV());
			}
		}
		
		List<CTDateTime> dList = _record.getDList();
		if (dList != null) {
			for (CTDateTime d : dList) {
				int index  = indexMapper.get(d.getDomNode());
				dataMap.put(index, d.getV());
			}
		}
		
		List<CTBoolean> bList = _record.getBList();
		if (bList != null) {
			for (CTBoolean b : bList) {
				int index  = indexMapper.get(b.getDomNode());
				dataMap.put(index, b.getV());
			}
		}
		
		for (int i = 0; i < dataMap.size(); i++) {
			_data.add(dataMap.get(i));
		}
	}
	
	private class IndexMapper extends HashMap<Node, Integer> {
		
		IndexMapper(Node parent) {
			int i = 0;
			for (Node chd = parent.getFirstChild(); chd != null; chd = chd.getNextSibling()) {
				put(chd, i++);
			}
		}
	}

}
