/* XSSFCacheField.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.List;

import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCacheField;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDateTime;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDiscretePr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFieldGroup;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTGroupItems;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIndex;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumber;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSharedItems;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTString;
import org.zkoss.poi.ss.usermodel.PivotCache.CacheField;

public class XSSFCacheField implements CacheField, Serializable {
	private static final long serialVersionUID = -5976672310014964599L;
	
	private final CTCacheField _cacheField;
	
	/*package*/XSSFCacheField(CTCacheField cacheField) {
		_cacheField = cacheField;
		_cacheField.getDatabaseField();
	}
	
	public void setName(String name) {
		_cacheField.setName(name);
	}
	
	public String getName() {
		return _cacheField.getName();
	}

	private static void addDates(List<CTDateTime> from, List<Object> to) {
		if (from != null) {
			for (CTDateTime e : from) {
				to.add(e.getV());
			}
		}
	}
	
	private static void addNumbers(List<CTNumber> from, List<Object> to) {
		if (from != null) {
			for (CTNumber e : from) {
				to.add(e.getV());
			}
		}
	}
	
	private static void addStrings(List<CTString> from, List<Object> to) {
		if (from != null) {
			for (CTString e : from) {
				to.add(e.getV());
			}
		}
	}
	
	public List<Object> getSharedItems() {
		ArrayList<Object> list = new ArrayList<Object>();
		
		if (_cacheField.getDatabaseField()) {
			CTSharedItems sharedItems = _cacheField.getSharedItems();
			if (sharedItems != null) {
				//missing -> date -> number -> string
				if (sharedItems.getContainsBlank()) {
					list.add(null);
				}
				addDates(sharedItems.getDList(), list);
				addNumbers(sharedItems.getNList(), list);
				addStrings(sharedItems.getSList(), list);
				return list;
			}
		} else {
			CTFieldGroup fieldGroup = _cacheField.getFieldGroup();
			if (fieldGroup != null) {
				CTGroupItems groupItems = fieldGroup.getGroupItems();
				addDates(groupItems.getDList(), list);
				addNumbers(groupItems.getNList(), list);
				addStrings(groupItems.getSList(), list);
				return list;
			}	
		}
		return list;
	}

	public void setDatabaseField(boolean databaseField) {
		_cacheField.setDatabaseField(databaseField);
	}
	
	public boolean getDatabaseField() {
		return _cacheField.getDatabaseField();
	}

	public List<Integer> getGroupDiscrete() {
		CTFieldGroup fieldGroup = _cacheField.getFieldGroup();
		if (fieldGroup == null) {
			return null;
		}
		
		List<Integer> list = new ArrayList<Integer>();
		CTDiscretePr discretePr = fieldGroup.getDiscretePr();
		for (CTIndex i : discretePr.getXList()) {
			list.add(Integer.valueOf((int)i.getV()));
		}
		return list;
	}
	
	public int getFieldGroup() {
		CTFieldGroup fieldGroup = _cacheField.getFieldGroup();
		if (fieldGroup == null) {
			return -1;
		}
		
		return (int) fieldGroup.getPar();
	}

	public int getGroupBase() {
		CTFieldGroup fieldGroup = _cacheField.getFieldGroup();
		if (fieldGroup == null) {
			return -1;
		}
		
		return (int) fieldGroup.getBase();
	}

	public long getNumberFormatId() {
		return _cacheField.getNumFmtId();
	}
}