/* ChartWidget.java

	Purpose:
		
	Description:
		
	History:
		Oct 18, 2010 2:27:40 PM     2010, Created by henrichen

Copyright (C) 2010 Potix Corporation. All Rights Reserved.
 */
package org.zkoss.zssex.ui.widget;

import org.zkoss.zk.ui.Component;
import org.zkoss.zss.model.SChart;
import org.zkoss.zss.model.SSheet;
import org.zkoss.zssex.ui.ZssCharts;
import org.zkoss.zssex.util.HighchartsHelper;

/**
 * A ChartWiget to show chart in spreadsheet.
 * The implementation of this widget is {@link Chart}, 
 * you can get {@link Chart} component by call {@link #getComponent()}
 * 
 * @author henrichen@zkoss.org
 * @author dennis , refine to new chart model
 */
public class ChartsWidget extends BaseWidget{
	private static final long serialVersionUID = 7841619576255947718L;
	
	private SSheet _sheet; //chart showing sheet
	private int _zindex;
	private SChart _chartInfo; //XSSFChartX and HSSFChartX
	private ZssCharts _chartComp; //ZSS-1094: ZK Charts must be 2.0.1+
	
	private ZssCharts inner() {
		return _chartComp;
	}
	
	public ChartsWidget(SSheet sheet, SChart chartInfo, String panel,int zindex) {
		super(panel);
		_sheet = sheet;
		_chartInfo = chartInfo;
		_zindex = zindex;
		_chartComp = newChart0();
		if (_chartComp != null) {
			initChart();
		}
		
		setId(_chartInfo.getId());
		setSizable(true);
		setMovable(true);
		setCtrlKeys("#del");
	}

	protected Component getInnerComponent() {
		return inner();
	}
	
	void setInClient(boolean inClient) {
		super.setInClient(inClient);
		//ZSS491-attach to ui when set to client
		if(inClient){
			_chartComp.setParent(getCtrl());
		}else{
			_chartComp.setParent(null);
		}
	}
	
	public String getType() {
		return inner().getType();
	}
	
	public void setType(String type) {
		inner().setType(type);
	}

	public String getTitle() {
		return inner().getTitle().getText();
	}
	
	public void setTitle(String title) {
		inner().setTitle(title);
	}
	
	public boolean isThreeD() {
		return inner().isThreeD();
	}
	public void setThreeD(boolean enabled) {
		inner().setThreeD(enabled);
	}
	
	public void setWidth(Number width){
		inner().setWidth(width);
	}
	
	public Number getWidth(){
		return inner().getWidth();
	}
	
	public void setHeight(Number height){
		inner().setHeight(height);
	}
	
	public Number getHeight(){
		return inner().getHeight();
	}
	
	private ZssCharts newChart0() {
		final ZssCharts chart = createChart(_chartInfo);
		//ZSS491, append widget untile mark it on client
//		chart.setParent(getCtrl());
		return chart;
	}
	
	@Override
	public void invalidate() {
		if (_chartComp != null) {
			initChart(); //reinit
		}
	}
	
	private void initChart() {
		HighchartsHelper.drawCharts(_chartComp, _chartInfo);
		_chartComp.setSclass("zswidget-chart");
	}
	
	//override this method to provide different Chart
	public ZssCharts createChart(SChart chartInfo) {
		return HighchartsHelper.createCharts(chartInfo);
	}
	
	@Override
	public String getWidgetType() {
		return "chart";
	}
}
