package com.adonis.ui.renta;

import at.downdrown.vaadinaddons.highchartsapi.Colors;
import at.downdrown.vaadinaddons.highchartsapi.HighChart;
import at.downdrown.vaadinaddons.highchartsapi.HighChartFactory;
import at.downdrown.vaadinaddons.highchartsapi.model.ChartConfiguration;
import at.downdrown.vaadinaddons.highchartsapi.model.ChartType;
import at.downdrown.vaadinaddons.highchartsapi.model.data.PieChartData;
import at.downdrown.vaadinaddons.highchartsapi.model.series.PieChartSeries;
import com.adonis.data.service.PersonService;
import com.adonis.data.service.RentaHistoryService;
import com.adonis.data.service.VehicleService;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;

/**
 * Created by oksdud on 12.04.2017.
 */
public class RentaChartView extends CustomComponent implements View {

    public static final String NAME =  "RENTA CHART VIEW";
    PersonService service;
    RentaHistoryService rentaHistoryService;
    VehicleService vehicleService;
    // The view root layout
    HorizontalLayout viewLayout = new HorizontalLayout();
    ChartConfiguration rentaConfiguration = new ChartConfiguration();

    public RentaChartView(PersonService personService, RentaHistoryService rentaHistoryService, VehicleService vehicleService){
        this.service = personService;
        this.rentaHistoryService = rentaHistoryService;
        this.vehicleService = vehicleService;
        setSizeFull();
        rentaConfiguration.setTitle("TestRenta");
        rentaConfiguration.setChartType(ChartType.PIE);
        rentaConfiguration.setBackgroundColor(Colors.WHITE);
        Double working = Double.valueOf(rentaHistoryService.findAllWorking().size());
        Double active = Double.valueOf(vehicleService.findAllActive().size());
        Double all = Double.valueOf(vehicleService.findAll().size());
        Double notActive = all - active;
        if(!all.equals(0.0)) {
            PieChartSeries pieVehicles = new PieChartSeries("Vehicles");
            PieChartData workingVehicles = new PieChartData("Working", Double.valueOf((working / all) * 100));
            PieChartData notWorkingVehicles = new PieChartData("Not working", Double.valueOf(((active - working) / all) * 100));
            PieChartData notActiveVehicles = new PieChartData("Not active", Double.valueOf((notActive / all) * 100));
            pieVehicles.getData().add(workingVehicles);
            pieVehicles.getData().add(notActiveVehicles);
            pieVehicles.getData().add(notWorkingVehicles);


            rentaConfiguration.getSeriesList().add(pieVehicles);
            try {
                HighChart pieChart = HighChartFactory.renderChart(rentaConfiguration);
                pieChart.setHeight(80, UNITS_PERCENTAGE);
                pieChart.setWidth(80, UNITS_PERCENTAGE);
                System.out.println("PieChart Script : " + rentaConfiguration.getHighChartValue());
                viewLayout.addComponent(pieChart);
                viewLayout.setComponentAlignment(pieChart, Alignment.MIDDLE_CENTER);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        viewLayout.setSizeFull();
        setCompositionRoot(viewLayout);

    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {

    }
}

@@ -738,94 +738,9 @@
 .ui.
-Alignment;%0Aimport com.vaadin.ui.CustomComponent;%0Aimport com.vaadin.ui.HorizontalLayout
+*
 ;%0A%0A/
@@ -1174,16 +1174,81 @@
 ation();
+%0A    PieChartSeries pieVehicles = new PieChartSeries(%22Vehicles%22);
 %0A%0A    pu
@@ -1501,32 +1501,33 @@
 vehicleService;%0A
+%0A
         setSizeF
@@ -1525,32 +1525,33 @@
  setSizeFull();%0A
+%0A
         rentaCon
@@ -1700,16 +1700,1258 @@
 .WHITE);
+%0A%0A        VerticalLayout verticalLayout = new VerticalLayout();%0A%0A        HighChart pieChart = initChart();%0A%0A        if(pieChart!=null)%7B%0A            verticalLayout.addComponent(pieChart);%0A            verticalLayout.setComponentAlignment(pieChart, Alignment.MIDDLE_CENTER);%0A        %7D%0A%0A        Button refresh = new Button(%22Refresh data%22);%0A        refresh.addClickListener(new Button.ClickListener() %7B%0A            @Override%0A            public void buttonClick(Button.ClickEvent event) %7B%0A                verticalLayout.removeComponent(pieChart);%0A                HighChart pieChart = initChart();%0A%0A                if(pieChart!=null)%7B%0A                    verticalLayout.addComponent(pieChart);%0A                    verticalLayout.setComponentAlignment(pieChart, Alignment.MIDDLE_CENTER);%0A                %7D%0A%0A            %7D%0A        %7D);%0A%0A        verticalLayout.addComponent(refresh);%0A        verticalLayout.setComponentAlignment(refresh, Alignment.BOTTOM_CENTER);%0A%0A        viewLayout.addComponent(verticalLayout);%0A        viewLayout.setComponentAlignment(verticalLayout, Alignment.MIDDLE_CENTER);%0A        viewLayout.setSizeFull();%0A        setCompositionRoot(viewLayout);%0A%0A    %7D%0A%0A    private HighChart initChart()%7B%0A        pieVehicles.getData().clear();%0A%0A
 %0A       
@@ -3254,81 +3254,9 @@
 ) %7B%0A
-            PieChartSeries pieVehicles = new PieChartSeries(%22Vehicles%22);
 %0A
+
     
@@ -3601,32 +3601,33 @@
 / all) * 100));%0A
+%0A
             pieV
@@ -3780,25 +3780,24 @@
 Vehicles);%0A%0A
-%0A
             
@@ -3841,32 +3841,33 @@
 d(pieVehicles);%0A
+%0A
             try 
@@ -4189,126 +4189,23 @@
     
-viewLayout.addComponent(pieChart);%0A                viewLayout.setComponentAlignment(pieChart, Alignment.MIDDLE_CENTER)
+return pieChart
 ;%0A  
@@ -4310,75 +4310,20 @@
     
- viewLayout.setSizeFull();%0A        setCompositionRoot(viewLayout);%0A
+return null;
 %0A   

update program