package view.priceHistory;

import java.util.Date;

import javafx.collections.ObservableList;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import priceHistory.ListedStockTO;
import priceHistory.PriceHistoryController;
import view.BaseMainView;

public class PriceHistoryMainView extends BaseMainView {
	
	private StocklistView stocklistView;
	
	private BorderPane vbox;
	public TechnicalAnalysisSelectionView techAnalysisSelectionListView;
	
	private GridPane chartAreaPane;
	private StockPriceChart stockPriceChart;
	private StockVolumeChart stockVolumeChart;
	
	private PriceHistoryController controller;
	
	public PriceHistoryMainView() {
		super();
		controller = new PriceHistoryController( this );
		
		createComponents();
		layoutComponents();
		
		initialiseComponents();
	}
	
	private void createComponents() {
		createVBox();
		createChartAreaPane();
	}
	
	private void createVBox() {
		vbox = new BorderPane();
		stocklistView = new StocklistView( controller );
		vbox.setTop( stocklistView );
		
		techAnalysisSelectionListView = new TechnicalAnalysisSelectionView( controller );
		vbox.setBottom( techAnalysisSelectionListView );
	}
	
	private void initialiseComponents() {
		ObservableList<ListedStockTO> observableList = controller.getListedStocks();
		populateStockListView( observableList );
	}
	
	private void layoutComponents() {
		super.setLeft( vbox );
		super.setCenter( chartAreaPane );
		
		layoutChartAreaPane();
	}
	
	private void createChartAreaPane() {
		chartAreaPane = new GridPane();
		chartAreaPane.setGridLinesVisible( false );
		
		stockPriceChart = new StockPriceChart();
		stockVolumeChart = new StockVolumeChart();
	}
	
	private void layoutChartAreaPane() {
		chartAreaPane.add( stockPriceChart, 0, 0 );
		chartAreaPane.add( stockVolumeChart, 0, 1 );
		
		ColumnConstraints col0Constraint = new ColumnConstraints();
		col0Constraint.setHgrow(Priority.ALWAYS);
		chartAreaPane.getColumnConstraints().add( 0, col0Constraint );
		
		RowConstraints row0Constraint = new RowConstraints();
		row0Constraint.setPercentHeight(70);
		chartAreaPane.getRowConstraints().add( 0, row0Constraint );
		
		RowConstraints row1Constraint = new RowConstraints();
		row1Constraint.setPercentHeight(30);
		chartAreaPane.getRowConstraints().add( 1, row1Constraint );
	}
	
	private void populateStockListView( ObservableList<ListedStockTO> p_observableList ) {
		stocklistView.populateStockList( p_observableList );
	}
	
	public void populateStockPriceChart( XYChart.Series<Date, Number> p_closePriceSeries ){
		stockPriceChart.getData().clear();
		stockPriceChart.getData().add( p_closePriceSeries );
	}
	
	public void populateStockVolumeChart( XYChart.Series<String, Number> p_closePriceSeries ) {
		stockVolumeChart.getData().clear();
		stockVolumeChart.getData().add( p_closePriceSeries );
	}
	
	public void populateStockPriceAux( XYChart.Series<Date, Number> p_auxSeries ) {
		stockPriceChart.getData().add( p_auxSeries );
	}
	
	public void removeStockPriceAux( XYChart.Series<Date, Number> p_auxSeries ) {
		stockPriceChart.getData().remove( p_auxSeries );
	}

}

@@ -561,23 +561,25 @@
 ate 
-BorderPane vbox
+GridPane leftPane
 ;%0D%0A%09
@@ -1054,20 +1054,24 @@
 %09%09create
-VBox
+LeftPane
 ();%0D%0A%09%09c
@@ -1124,37 +1124,43 @@
 eate
-VBox() %7B%0D%0A%09%09vbox
+LeftPane() %7B%0D%0A%09%09leftPane
  = new 
-Border
+Grid
 Pane
@@ -1160,24 +1160,28 @@
 ridPane();%0D%0A
+%09%09%0D%0A
 %09%09stocklistV
@@ -1222,43 +1222,8 @@
 r );
-%0D%0A%09%09vbox.setTop( stocklistView );%0D%0A
 %09%09%0D%0A
@@ -1313,53 +1313,419 @@
 %0D%0A%09%09
-vbox.setBottom( techAnalysisSelectionListView
+%0D%0A%09%09leftPane.add( stocklistView, 0, 0 );%0D%0A%09%09leftPane.add( techAnalysisSelectionListView, 0, 1 );%0D%0A%09%09%0D%0A%09%09RowConstraints row0Constraint = new RowConstraints();%0D%0A%09%09row0Constraint.setPercentHeight(60);%0D%0A%09%09leftPane.getRowConstraints().add( 0, row0Constraint );%0D%0A%09%09%0D%0A%09%09RowConstraints row1Constraint = new RowConstraints();%0D%0A%09%09row1Constraint.setPercentHeight(40);%0D%0A%09%09leftPane.getRowConstraints().add( 1, row1Constraint
  );%0D
@@ -1960,12 +1960,16 @@
 ft( 
-vbox
+leftPane
  );%0D

Make panes scale properly