/* XSSFTableStyle.java

	Purpose:
		
	Description:
		
	History:
		Jan 27, 2016 3:57:58 PM, Created by henrichen

	Copyright (C) 2016 Potix Corporation. All Rights Reserved.
*/

package org.zkoss.poi.xssf.usermodel;

import java.util.Map;
import java.util.LinkedHashMap;

import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyle;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleElement;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STTableStyleType;
import org.zkoss.poi.ss.usermodel.DxfCellStyle;
import org.zkoss.poi.ss.usermodel.TableStyle;
import org.zkoss.poi.ss.usermodel.TableStyleElem;
import org.zkoss.poi.xssf.model.StylesTable;
import org.zkoss.poi.xssf.model.ThemesTable;
import org.zkoss.poi.xssf.usermodel.extensions.XSSFCellAlignment;

//ZSS-992
/**
 * @author henri
 * @sicne 3.9.10
 */
public class XSSFTableStyle implements TableStyle {
    private CTTableStyle _ctTbStyle;
    private Map<String, CTTableStyleElement> _ctTbStyleElems;
    private XSSFWorkbook _book;

    public XSSFTableStyle(int styleId, StylesTable stylesSource, XSSFWorkbook book) {
    	this._ctTbStyle = stylesSource.getTableStyles().get(styleId);
    	this._book = book;
    	this._ctTbStyle.setPivot(false); //ZSS-1188
    }

    //ZSS-1188
    public XSSFTableStyle(String name, StylesTable styleSource, XSSFWorkbook book) {
    	this._ctTbStyle = CTTableStyle.Factory.newInstance();
    	this._book = book;
    	this._ctTbStyle.setName(name);
    	this._ctTbStyle.setPivot(false);
    }

	@Override
	public String getName() {
		return _ctTbStyle.getName();
	}

	@Override
	public DxfCellStyle getDxfCellStyle(String name) {
		CTTableStyleElement elem = _getTableStyleElem(name);
		return elem != null ? _book.createDxfCellStyle((int)elem.getDxfId()) : null;
	}
	
	private CTTableStyleElement _getTableStyleElem(String name) {
		if (_ctTbStyleElems == null) {
			_ctTbStyleElems = new LinkedHashMap<String, CTTableStyleElement>();
			for (CTTableStyleElement tbStyleElem : _ctTbStyle.getTableStyleElementArray()) {
				_ctTbStyleElems.put(tbStyleElem.getType().toString(), tbStyleElem);
			}
		}
		return _ctTbStyleElems.get(name);
	}

	//ZSS-1188
	@Override
	public void addTableStyleElement(int dxfId, String type, int stripeSize) {
		CTTableStyleElement elem = addTableStyleElement0(dxfId, type);
		if (stripeSize > 1) {
			elem.setSize(stripeSize);
		}
		_ctTbStyleElems = null;
	}

	//ZSS-1188
	@Override
	public void addTableStyleElement(int dxfId, String type) {
		addTableStyleElement0(dxfId, type);
	}
	
	//ZSS-1188
	private CTTableStyleElement addTableStyleElement0(int dxfId, String type) {
		CTTableStyleElement elem = _ctTbStyle.addNewTableStyleElement();
		elem.setDxfId(dxfId);
		elem.setType(STTableStyleType.Enum.forString(type));
		_ctTbStyleElems = null;
		_ctTbStyle.setCount(_ctTbStyle.getTableStyleElementList().size());
		return elem;
	}
	
	//ZSS-1188
	public CTTableStyle getCTTableStyle() {
		return _ctTbStyle;
	}
}
