/*
 * 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.sp.report.ReportGoogleDriveController;
import com.arrebol.taxiservicios.model.sp.report.ReportGoogleDrive;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
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;

/**
 *
 * @author Janitzio Zavala López.
 */
@Named
@ViewScoped
public class ReportGoogleDriveBean extends TaxiGenericBean implements Serializable {

    public void updateReport() {
        LOG.info("updateReport");
        try {
            setReportGoogleDrive(
                    getController().callReportGoogleDriveQueryMySQL(DATE_FORMAT.format(getStartDate()), getLoggedUser().getLocation().getId())
            );
        } catch (Exception e) {
            LOG.error("updateReport", e);
        }
    }

    @PostConstruct
    public void init() {
        LOG.info("init");
        try {
            loadBundlePropertyFile();
            setController(new ReportGoogleDriveController());
            setReportGoogleDrive(
                    getController().callReportGoogleDriveQueryMySQL(DATE_FORMAT.format(getStartDate()), getLoggedUser().getLocation().getId())
            );
        } catch (Exception e) {
            LOG.error("init", e);
        }
    }

    private static final long serialVersionUID = 6394938203433032821L;
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    final Logger LOG = LogManager.getLogger(ReportGoogleDriveBean.class);

    private ReportGoogleDriveController controller;
    private List<ReportGoogleDrive> reportGoogleDrive;
    private Date startDate;
    private Date endDate;

    public ReportGoogleDriveBean() {
    }

    public ReportGoogleDriveController getController() {
        return controller;
    }

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

    public List<ReportGoogleDrive> getReportGoogleDrive() {
        return reportGoogleDrive;
    }

    public void setReportGoogleDrive(List<ReportGoogleDrive> reportGoogleDrive) {
        this.reportGoogleDrive = reportGoogleDrive;
    }

    public Date getStartDate() {
        if (null == startDate) {
            startDate = new Date();
        }

        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        if (null == endDate) {
            endDate = new Date();
        }
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

}
