/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.arrebol.taxiservicios.beans.report;

import com.arrebol.taxiservicios.beans.Datatable;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.beans.TicketMulta;
import com.arrebol.taxiservicios.beans.TicketSale;
import com.arrebol.taxiservicios.beans.stocktaking.invoice.TransferStocktakingListBean;
import com.arrebol.taxiservicios.controller.admin.SaleController;
import com.arrebol.taxiservicios.controller.admin.SanctionController;
import com.arrebol.taxiservicios.controller.catalog.LocationController;
import com.arrebol.taxiservicios.controller.view.report.TicketViewController;
import com.arrebol.taxiservicios.model.catalog.Location;
import com.arrebol.taxiservicios.model.view.admin.TicketView;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
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 pc one
 */
@Named
@ViewScoped
public class TicketViewBean extends TaxiGenericBean implements Serializable, Datatable {

    private static final long serialVersionUID = -4475615978271025834L;
    final Logger logger = LogManager.getLogger(TransferStocktakingListBean.class);
    private TicketViewController ticketViewController;
    private SaleController saleController;
    private SanctionController sanctionController;
    private List<TicketView> ListTicketView;
    private TicketView selectedTicket;
    private LocationController locationController;
    private List<Location> locations;
    private String selectedLocation;
    private String rutaImg;
    private Date diaHoy;

    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            setDiaHoy(new Date());
            loadBundlePropertyFile();
            setLocationController(new LocationController());
            setLocations(getLocationController().fillLocationDatatable());
            setTicketViewController(new TicketViewController());
            if(getSelectedLocation()==null){
                setSelectedLocation(getLoggedUser().getLocation().getId());
            }
            setListTicketView(getTicketViewController().findAllTicket(getSelectedLocation()));
            logger.info("Ticket loaded");
        } catch (Exception e) {
            logger.error("init", e);
        }
    }
    
    public void updateTicketList(){
        setTicketViewController(new TicketViewController());
        setListTicketView(getTicketViewController().findAllTicket(getSelectedLocation()));
    }
    
    public void update(){

    }
    
    public void updateTicketStatus(){
        setSaleController(new SaleController());
        setSanctionController(new SanctionController());
         if (getSelectedTicket().getEconomicNumber() == 1) {
            try {                      
                getSaleController().updateTicketStatus(getSelectedTicket().getId(), getLoggedUser().getId());
                logger.info("updated and printed payment: " + getSelectedTicket().getId());
            } catch (Exception e) {
                logger.error("error printing the sales receipt: " + getSelectedTicket().getId());
                showMessage(FacesMessage.SEVERITY_ERROR,"Error al imprimir", "Ocurrió un error durante la impresión de los tickets");
            }
        } else {
            try {
                getSanctionController().updateTicketStatus(getSelectedTicket().getId(), getLoggedUser().getId());
                logger.info("updated and printed sanction: " + getSelectedTicket().getId());
            } catch (Exception e) {
                logger.error("error printing sanction receipt: " + getSelectedTicket().getId());
                showMessage(FacesMessage.SEVERITY_ERROR,"Error al imprimir", "Ocurrió un error durante la impresión de los tickets");
            }
        }
        setTicketViewController(new TicketViewController());
        if(getSelectedLocation()==null){
            setSelectedLocation(getLoggedUser().getLocation().getId());
        }
        setListTicketView(getTicketViewController().findAllTicket(getSelectedLocation()));
        logger.info("Ticket loaded");
    }
    
   /* public void imprimirTicket() {
        setSaleController(new SaleController());
        setSanctionController(new SanctionController());
        setListTicketView(getTicketViewController().findAllTicket(getSelectedLocation()));
        
        if(getListTicketView().size() <= 0){
            showMessage(FacesMessage.SEVERITY_ERROR,"Error al imprimir", "No ahí tickets pendientes");
        }else{
            for (TicketView ticketEnLista : getListTicketView()) {
                if (ticketEnLista.getEconomicNumber() == 1) {
                    try {
                        printTicketSale(ticketEnLista.getCompleteName(), ticketEnLista.getPayDay().toString(), ticketEnLista.getPaySaving().toString(), ticketEnLista.getAccident().toString());
                        getSaleController().updateTicketStatus(ticketEnLista.getId(), getLoggedUser().getId());
                        logger.info("updated and printed payment: " + ticketEnLista.getId());
                    } catch (Exception e) {
                        logger.error("error printing the sales receipt: " + ticketEnLista.getId());
                        showMessage(FacesMessage.SEVERITY_ERROR,"Error al imprimir", "Ocurrió un error durante la impresión de los tickets");
                    }
                } else {
                    try {
                        printTicketMulta(ticketEnLista.getCompleteName(), ticketEnLista.getPayDay().toString(), ticketEnLista.getEconomicNumber().intValue());
                        getSanctionController().updateTicketStatus(ticketEnLista.getId(), getLoggedUser().getId());
                        logger.info("updated and printed sanction: " + ticketEnLista.getId());
                    } catch (Exception e) {
                        logger.error("error printing sanction receipt: " + ticketEnLista.getId());
                        showMessage(FacesMessage.SEVERITY_ERROR,"Error al imprimir", "Ocurrió un error durante la impresión de los tickets");
                    }
                }
            }
            showMessage(FacesMessage.SEVERITY_INFO,"Impresión correcta", "Se imprimieron todos los tickets correctamente");
        }
    }

    public void printTicketSale(String nameDriver, String pagoDia, String ahorro, String mutual) {
        TicketSale ticket = new TicketSale(nameDriver, pagoDia, ahorro, mutual);
        ticket.printTest();
        ticket.print();
    }

    public void printTicketMulta(String nameDriver, String monto, Integer tipo) {
        TicketMulta ticket = new TicketMulta(nameDriver, monto, tipo);
        ticket.printTest();
        ticket.print();
    }*/

    @Override
    public void editRow(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

    @Override
    public void onRowCancel(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

    @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 addRow() {
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

    @Override
    public void deleteRow() {
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

    public TicketViewController getTicketViewController() {
        return ticketViewController;
    }

    public void setTicketViewController(TicketViewController ticketViewController) {
        this.ticketViewController = ticketViewController;
    }

    public List<TicketView> getListTicketView() {
        return ListTicketView;
    }

    public void setListTicketView(List<TicketView> ListTicketView) {
        this.ListTicketView = ListTicketView;
    }

    public TicketView getSelectedTicket() {
        return selectedTicket;
    }

    public void setSelectedTicket(TicketView selectedTicket) {
        this.selectedTicket = selectedTicket;
    }

    public SaleController getSaleController() {
        return saleController;
    }

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

    public SanctionController getSanctionController() {
        return sanctionController;
    }

    public void setSanctionController(SanctionController sanctionController) {
        this.sanctionController = sanctionController;
    }

    public LocationController getLocationController() {
        return locationController;
    }

    public void setLocationController(LocationController locationController) {
        this.locationController = locationController;
    }

    public List<Location> getLocations() {
        return locations;
    }

    public void setLocations(List<Location> locations) {
        this.locations = locations;
    }

    public String getSelectedLocation() {
        return selectedLocation;
    }

    public void setSelectedLocation(String selectedLocation) {
        this.selectedLocation = selectedLocation;
    }

    public String getRutaImg() {
        return rutaImg;
    }

    public void setRutaImg(String rutaImg) {
        this.rutaImg = rutaImg;
    }

    public Date getDiaHoy() {
        return diaHoy;
    }

    public void setDiaHoy(Date diaHoy) {
        this.diaHoy = diaHoy;
    }

}
