/*
 * 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.stocktaking.invoice;

import com.arrebol.taxiservicios.beans.Datatable;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.admin.GenericValidationController;
import com.arrebol.taxiservicios.controller.stocktaking.InvoiceController;
import com.arrebol.taxiservicios.controller.stocktaking.TransferStocktakingController;
import com.arrebol.taxiservicios.controller.util.DateWrapper;
import com.arrebol.taxiservicios.model.stocktaking.ProductInvoiceBk;
import com.arrebol.taxiservicios.model.stocktaking.TransferStocktaking;
import com.arrebol.taxiservicios.model.stocktaking.view.StocktakingInvoiceView;
import java.io.Serializable;
import java.util.ArrayList;
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;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author oscarvargas
 */
@Named
@ViewScoped
public class TransferStocktakingListBean extends TaxiGenericBean implements Serializable, Datatable {

    public void fillDataTableBetweenDates() {
        try {
            if (getStartDate().after(getEndDate())) {
                showMessage(FacesMessage.SEVERITY_ERROR, getBundlePropertyFile().getString("generic.start.date"), getBundlePropertyFile().getString("generic.end.date.error"));
            } else {
                setTransfer(getController().fillDataTableBetweenDates(getStartDate(), getEndDate(), getLoggedUser().getLocation()));
            }
        } catch (Exception e) {
            logger.error("", e);
        }
    }

    /**
     * Edit invoice by selection datatable.
     *
     * @param outcome path for invoice update.
     * @return URL for updagte invoice.
     */
    public String editInvoice(String outcome) {
        return outcome;
    }

    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 addRow() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void onRowReorder(ReorderEvent event) {
        showMessage(FacesMessage.SEVERITY_INFO, "Registro Movido", "De columna: " + (event.getFromIndex() + 1) + " a columna: " + (event.getToIndex() + 1));
    }

    @Override
    public void deleteRow() {
        
    }

    private static final long serialVersionUID = -4475615978271025834L;

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

    TransferStocktakingController controller;
    GenericValidationController genericController;
    private List<TransferStocktaking> transfer;
    private TransferStocktaking selectedTransfer;
    private Date startDate;
    private Date endDate;

    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            loadBundlePropertyFile();
            initOneWeekBeforeToCurrdate();
            setController(new TransferStocktakingController());
            setGenericController(new GenericValidationController());
            setTransfer(getController().fillDataTableBetweenDates(getStartDate(), getEndDate(), getLoggedUser().getLocation()));

            logger.info("Invoices loaded");
        } catch (Exception e) {
            logger.error("init", e);
        }
    }

    private void initOneWeekBeforeToCurrdate() {
        try {
            Calendar start = Calendar.getInstance();

            start.set(Calendar.WEEK_OF_YEAR, start.get(Calendar.WEEK_OF_YEAR) - 1);
            start.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

            setStartDate(start.getTime());
            setEndDate(DateWrapper.getTodayMXTime());
        } catch (Exception e) {

        }
    }

    public TransferStocktakingController getController() {
        return controller;
    }

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

    public List<TransferStocktaking> getTransfer() {
        if (null == transfer) {
            transfer = new ArrayList<>();
        }
        return transfer;
    }

    public TransferStocktaking getSelectedTransfer() {
        return selectedTransfer;
    }

    public void setTransfer(List<TransferStocktaking> transfer) {
        this.transfer = transfer;
    }

    public void setSelectedTransfer(TransferStocktaking selectedTransfer) {
        this.selectedTransfer = selectedTransfer;
    }

    public GenericValidationController getGenericController() {
        return genericController;
    }

    public void setGenericController(GenericValidationController genericController) {
        this.genericController = genericController;
    }

    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;
    }

}
