/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ht.ui.energy.sum;

import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;

import com.ht.ui.chart.CustomBarChart;
import com.ht.ui.chart.CustomLineChart;
import com.ht.ui.chart.CustomPieChart;
import com.ht.ui.chart.ICmpChartWrapper;

/**
 *
 * @author Administrator
 */
public class SimpleChartViewer extends VBox {

//    private Chart chart;
//	public static final double WIDTH = (Screen.getPrimary().getVisualBounds().getHeight() - 150) / 2;
	
    private ICmpChartWrapper chartWrapper;
    private EventHandler<MouseEvent> eventHandler;
    private MainChartViewer mainViewer;
    

    public SimpleChartViewer(MainChartViewer mainView, ICmpChartWrapper chartWrapper) {
    	this.mainViewer = mainView;
    	
        getStyleClass().add("SimpleChartView");
//		getStyleClass().add("window");
		
//		Rectangle2D rect = Screen.getPrimary().getVisualBounds();
//        double width = (rect.getHeight() - 150) * 0.5;
//        setPrefSize(width, width);
//        setPrefSize(WIDTH, WIDTH);
        setPrefSize(300, 300);

        this.chartWrapper = chartWrapper;
        eventHandler = new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                ICmpChartWrapper mainChartWrapper = mainViewer.getChartWrapper();
                ICmpChartWrapper sampleChartWrapper = getChartWrapper();
                setChartWrapper(mainChartWrapper);
                mainViewer.setChartWrapper(sampleChartWrapper);
            }
        };
        this.chartWrapper.getChart().setOnMouseClicked(eventHandler);

        getChildren().add(chartWrapper.getChart());
        
        chartWrapper.showShortTitle();
        chartWrapper.getChart().setLegendVisible(false);
        if (chartWrapper instanceof CustomPieChart) {
        	((CustomPieChart)chartWrapper).setLabelsVisible(false);
        }
        if (chartWrapper instanceof CustomBarChart) {
        	((CustomBarChart)chartWrapper).getXAxis().setVisible(false);
        	((CustomBarChart)chartWrapper).getXAxis().setTickLabelsVisible(false);
        }
        if (chartWrapper instanceof CustomLineChart) {
        	((CustomLineChart)chartWrapper).getXAxis().setVisible(false);
        }
    }

    public ICmpChartWrapper getChartWrapper() {
        return chartWrapper;
    }

    public void setChartWrapper(ICmpChartWrapper chartWrapper) {

        if (chartWrapper != null) {
            if (this.chartWrapper != null) {
                getChildren().remove(this.chartWrapper.getChart());
            }
            
            chartWrapper.showShortTitle();
            chartWrapper.getChart().setLegendVisible(false);
            if (chartWrapper instanceof CustomPieChart) {
            	((CustomPieChart)chartWrapper).setLabelsVisible(false);
            }
            if (chartWrapper instanceof CustomBarChart) {
            	((CustomBarChart)chartWrapper).getXAxis().setTickLabelsVisible(false);
            }
            if (chartWrapper instanceof CustomLineChart) {
            	((CustomLineChart)chartWrapper).getXAxis().setTickLabelsVisible(false);
            	//((CustomLineChart)chartWrapper).setCreateSymbols(false);
            }
            getChildren().add(chartWrapper.getChart());
            
            this.chartWrapper = chartWrapper;
            this.chartWrapper.getChart().setOnMouseClicked(eventHandler);
            
        }
    }
}
