/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arrebol.taxiservicios.beans.report;

import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.admin.SaleController;
import com.arrebol.taxiservicios.controller.view.report.DailyMoneyController;
import com.arrebol.taxiservicios.model.view.report.DailyMoney;
import java.io.Serializable;
import java.math.BigInteger;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.primefaces.model.chart.Axis;
import org.primefaces.model.chart.AxisType;
import org.primefaces.model.chart.BarChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.PieChartModel;

/**
 *
 * @author Picasso
 */
@Named
@ViewScoped
public class DailyMoneyBean extends TaxiGenericBean implements Serializable {

    private void createBarModels() {
        createBarModel();
    }

    private void createBarModel() {
        barModel = initBarModel();

        barModel.setLegendPosition("ne");

        Axis xAxis = barModel.getAxis(AxisType.X);
        xAxis.setLabel(getBundlePropertyFile().getString("chart.bar.xAxis"));

        Axis yAxis = barModel.getAxis(AxisType.Y);
        yAxis.setLabel(getBundlePropertyFile().getString("chart.bar.yAxis"));

        BigInteger totalExpected = new BigInteger("200");

        if (null != getDailyMoney()) {
            totalExpected = new BigInteger(getDailyMoney().getExpectedDay().add(getDailyMoney().getExpectedSaving()).add(getDailyMoney().getExpectedAccident()).add(new BigInteger("100")).toString());
        }

        yAxis.setMin(0);
        yAxis.setMax(totalExpected);

        barModel.setExtender("skinBar");
    }

    private BarChartModel initBarModel() {
        BarChartModel model = new BarChartModel();

        ChartSeries current = new ChartSeries();
        ChartSeries expected = new ChartSeries();

        if (null != getDailyMoney()) {
            current.setLabel(getBundlePropertyFile().getString("chart.bar.current.label"));
            current.set(getBundlePropertyFile().getString("chart.bar.day"), getDailyMoney().getPayDay());
            current.set(getBundlePropertyFile().getString("chart.bar.save"), getDailyMoney().getPaySaving());
            current.set(getBundlePropertyFile().getString("chart.bar.accident"), getDailyMoney().getPayAccident());
            current.set(getBundlePropertyFile().getString("chart.bar.total"), getDailyMoney().getPayTotal());

            expected.setLabel(getBundlePropertyFile().getString("chart.bar.expected.label"));
            expected.set(getBundlePropertyFile().getString("chart.bar.day"), getDailyMoney().getExpectedDay());
            expected.set(getBundlePropertyFile().getString("chart.bar.save"), getDailyMoney().getExpectedSaving());
            expected.set(getBundlePropertyFile().getString("chart.bar.accident"), getDailyMoney().getExpectedAccident());

            BigInteger totalExpected = new BigInteger(getDailyMoney().getExpectedDay().add(getDailyMoney().getExpectedSaving()).add(getDailyMoney().getExpectedAccident()).toString());

            expected.set(getBundlePropertyFile().getString("chart.bar.total"), totalExpected);
        }

        model.addSeries(current);
        model.addSeries(expected);

        return model;
    }

    private void createPieModels() {
        createPieModel();
    }

    private void createPieModel() {
        pieModel = new PieChartModel();

        if (null != getDailyMoney()) {
            pieModel.set(getBundlePropertyFile().getString("chart.pie.sanction"), getDailyMoney().getSanctionToday());
            pieModel.set(getBundlePropertyFile().getString("chart.pie.disposition"), getDailyMoney().getDispositionToday());
            pieModel.set(getBundlePropertyFile().getString("chart.pie.accident"), getDailyMoney().getAccidentToday());
            pieModel.set(getBundlePropertyFile().getString("chart.pie.expense"), getDailyMoney().getExpenseToday());
        }

        pieModel.setLegendPosition("w");
        pieModel.setShadow(false);
        pieModel.setExtender("skinPie");
    }

    private static final long serialVersionUID = -3191424765157988910L;

    final Logger logger = LogManager.getLogger(DailyMoneyBean.class);

    private DailyMoneyController controller;
    private SaleController saleController;
    private DailyMoney dailyMoney;
    private BarChartModel barModel;
    private PieChartModel pieModel;
    private String todayAddStr;

    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            loadBundlePropertyFile();
            setController(new DailyMoneyController());
            setSaleController(new SaleController());
            setDailyMoney(getController().getReportDailyMoney());
            setTodayAddStr(getSaleController().getTotalAddForToday());
            createBarModels();
            createPieModels();
        } catch (Exception e) {
            logger.error("init", e);
        }
    }

    public DailyMoneyController getController() {
        return controller;
    }

    public void setController(DailyMoneyController controller) {
        this.controller = controller;
    }

    public SaleController getSaleController() {
        return saleController;
    }

    public void setSaleController(SaleController saleController) {
        this.saleController = saleController;
    }

    public DailyMoney getDailyMoney() {
        return dailyMoney;
    }

    public void setDailyMoney(DailyMoney dailyMoney) {
        this.dailyMoney = dailyMoney;
    }

    public BarChartModel getBarModel() {
        return barModel;
    }

    public PieChartModel getPieModel() {
        return pieModel;
    }

    public String getTodayAddStr() {
        return todayAddStr;
    }

    public void setTodayAddStr(String todayAddStr) {
        this.todayAddStr = todayAddStr;
    }

    public Double getTodayAdd() {
        Double todayAdd;
        try {
            todayAdd = new Double(getTodayAddStr());
        } catch (NumberFormatException e) {
            logger.error("Error conveerting value", e);
            todayAdd = new Double(0);
        }

        return todayAdd;
    }
}
