/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.taxiservicios.beans.system;

import com.arrebol.taxiservicios.beans.Datatable;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.admin.BitacoraController;
import com.arrebol.taxiservicios.model.admin.Bitacora;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
@Named
@ViewScoped
public class BitacoraBean extends TaxiGenericBean implements Serializable, Datatable {


    /**
     * Update bitacora list between start date and end date.
     */
    public void updateHistoryBitacoraList() {
        logger.info("updateHistoryBitacoraList");
        try {
            if (getStartDate().after(getEndDate())) {
                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("stable.small.box"),
                        getBundlePropertyFile().getString("start.date.after.error"));
            } else {
                setUpDates(false);
                setHistoryBitacora(
                        getController().findBitacoraListBetweenDates(
                                getStartDate(),
                                getEndDate(),
                                getLoggedUser().getLocation().getId()
                        )
                );
            }
        } catch (Exception e) {
            logger.error("updateHistoryBitacoraList", e);
        }
    }

    @Override
    public void editRow(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void onRowCancel(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void onRowReorder(ReorderEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addRow() {
        
    }

    @Override
    public void deleteRow() {
        
    }

    /**
     * Setup start date and end date.
     *
     * @param init True if comes from @PostConstruct otherwise false.
     */
    private void setUpDates(boolean init) {
        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();

        if (!init) {
            startCalendar.setTime(getStartDate());
            endCalendar.setTime(getEndDate());
        } else {
            startCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            endCalendar.setTime(new Date());
        }

        startCalendar.add(Calendar.HOUR_OF_DAY, 2);
        startCalendar.add(Calendar.MINUTE, 0);
        startCalendar.add(Calendar.SECOND, 0);
        startCalendar.add(Calendar.MILLISECOND, 0);

        endCalendar.add(Calendar.HOUR_OF_DAY, 22);
        endCalendar.add(Calendar.MINUTE, 59);
        endCalendar.add(Calendar.SECOND, 59);
        endCalendar.add(Calendar.MILLISECOND, 0);

        setStartDate(startCalendar.getTime());
        setEndDate(endCalendar.getTime());
    }

    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            loadBundlePropertyFile();
            setController(new BitacoraController());
            setCreatedOn(new Date());
            
            setUpDates(true);
            setHistoryBitacora(
                    getController().findBitacoraListBetweenDates(
                            getStartDate(),
                            getEndDate(),
                            getLoggedUser().getLocation().getId()
                        )
                );
        } catch (Exception e) {
            logger.error(e);
        }
    }

    private BitacoraController controller;
    private List<Bitacora> historyBitacora;
    private Bitacora selectedRow;
    private Date startDate;
    private Date endDate;
    private Date createdOn;

    public BitacoraController getController() {
        return controller;
    }

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

    public List<Bitacora> getHistoryBitacora() {
        return historyBitacora;
    }

    public void setHistoryBitacora(List<Bitacora> historyBitacora) {
        this.historyBitacora = historyBitacora;
    }

    public Bitacora getSelectedRow() {
        return selectedRow;
    }

    public void setSelectedRow(Bitacora selectedRow) {
        this.selectedRow = selectedRow;
    }

    public Date getStartDate() {
        return startDate;
    }

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

    public Date getEndDate() {
        return endDate;
    }

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

    public Date getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    private static final long serialVersionUID = 7328275333340376725L;
    final Logger logger = LogManager.getLogger(BitacoraBean.class);

}
