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

import com.arrebol.taxiservicios.beans.report.*;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.sp.report.ReportCustomerPayController;
import com.arrebol.taxiservicios.model.admin.Expense;
import com.arrebol.taxiservicios.model.admin.FinancingCustomer;
import com.arrebol.taxiservicios.model.admin.ReleaseResource;
import com.arrebol.taxiservicios.model.admin.Sale;
import com.arrebol.taxiservicios.model.admin.Taxi;
import com.arrebol.taxiservicios.model.enums.ActionStatus;
import com.arrebol.taxiservicios.model.enums.BasicType;
import com.arrebol.taxiservicios.model.sp.report.ReportCustomerPay;
import com.arrebol.taxiservicios.model.stocktaking.Invoice;
import com.arrebol.taxiservicios.model.stocktaking.InvoiceHasProduct;
import com.arrebol.taxiservicios.model.stocktaking.ServiceOrder;
import com.arrebol.taxiservicios.model.stocktaking.ServiceOrderHasProduct;
import com.arrebol.taxiservicios.model.stocktaking.ServiceOrderHasProductId;
import com.arrebol.taxiservicios.model.stocktaking.view.KardexOutStocktakingView;
import com.arrebol.taxiservicios.model.view.report.PaymentClient;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import static java.util.Calendar.HOUR_OF_DAY;
import static java.util.Calendar.MILLISECOND;
import static java.util.Calendar.MINUTE;
import static java.util.Calendar.SECOND;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
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.SelectEvent;
import org.primefaces.event.UnselectEvent;

/**
 *
 * @author Janitzio Zavala López.
 */
@Named("paymentClientDetailBean")
@ViewScoped
public class PaymentClientDetailBean extends TaxiGenericBean implements Serializable {

   
    public void addRow() {
        
    }

    public void updateReport() {
        LOG.info("updateReport");
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();

        start.setTime(getPaymentClient().getInitDate());
        start.set(Calendar.HOUR_OF_DAY, 7);
        start.set(Calendar.MINUTE, 30);
        start.set(Calendar.SECOND, 0);

        end.setTime(null == getPaymentClient().getEndDate() ? getToday() : getPaymentClient().getEndDate());
        end.set(Calendar.HOUR_OF_DAY, 23);
        end.set(Calendar.MINUTE, 59);
        end.set(Calendar.SECOND, 59);
        try {
            setReportCustomerPays(
                    getController().callReportCustomerPayToMySQLQuery(
                            DATE_FORMAT.format(start.getTime()),
                            DATE_FORMAT.format(end.getTime()),
                            getPaymentClient().getTaxi().getId()
                    )
            );

            setSales(getController().getSalesbyTaxiTuple(getPaymentClient().getTaxi().getId(), getStartDate(), getEndDate()));
            setExpense(getController().fillExpenseDatatableLeftJoin(getPaymentClient().getTaxi().getId(), start.getTime(), end.getTime()));
            setServiceOrders(getController().getAllServiceOrderActiveAndStatusInTuple(getPaymentClient().getTaxi().getId(), start.getTime(), end.getTime()));
            setInvoice(getController().fillInvoiceDatatableLeftJoin(getPaymentClient().getTaxi().getId(), start.getTime(), end.getTime()));
            setPaymentClientList(getController().fillPaymentClientDatatable(getPaymentClient().getTaxi().getId()));
            setRelease(getController().fillReleaseDatatable(getPaymentClient().getTaxi().getId()));
            setDetailProductOrder(getController().fillProductsDetailByOrderServices(getPaymentClient().getTaxi().getId(), start.getTime(), end.getTime()));


        } catch (Exception e) {
            LOG.error("updateReport", e);
        }
    }

    public List<ServiceOrderHasProduct> getDetails(String id) {
        LOG.info("getDetails");
        try {
            setDetails(getController().findAllProductByOrderId(id));

            LOG.info("details loaded size " + details.size());
        } catch (Exception e) {
            LOG.error("getDetails", e);
        }
        return null == details ? new ArrayList<>() : details;
    }

    public List<InvoiceHasProduct> getDetailsInvoice(String id) {
        LOG.info("getDetailsInvoice");
        try {
            setDetailsinvoice(getController().findAllProductByInvoiceId(id));

            LOG.info("detailsInvoice loaded size " + detailsinvoice.size());
        } catch (Exception e) {
            LOG.error("getDetailsInvoice", e);
        }
        return null == detailsinvoice ? new ArrayList<>() : detailsinvoice;
    }

    @PostConstruct
    public void init() {
        LOG.info("init");
        try {
            sales = new ArrayList();
            expense = new ArrayList();
            serviceOrders = new ArrayList();
            invoice = new ArrayList();
            reportCustomerPays = new ArrayList();
            paymentClientList = new ArrayList();
            detailProductOrder = new ArrayList();
            release = new ArrayList();
            loadBundlePropertyFile();
            setController(new ReportCustomerPayController());
            setPaymentCustomerId(externalContext().getRequestParameterMap().get("selectedId"));
            setPaymentClient(getController().getPaymentClientById(getPaymentCustomerId()));
            
            updateReport();

        } 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(PaymentClientDetailBean.class);
    
    private String paymentCustomerId;
    private PaymentClient paymentClient;

    private ReportCustomerPayController controller;
    private List<ReportCustomerPay> reportCustomerPays;
    private List<Sale> sales;
    private List<Expense> expense;
    private List<ServiceOrder> serviceOrders;
    private List<ServiceOrderHasProduct> details;
    private List<KardexOutStocktakingView> detailProductOrder;
    private List<KardexOutStocktakingView> selectedDetailProductOrderList;
    private List<InvoiceHasProduct> detailsinvoice;
    private List<PaymentClient> paymentClientList;
    private List<ReleaseResource> release;
    private List<Invoice> invoice;
    private Date startDate;
    private Date endDate;
    private ServiceOrder selectedServiceOrder;

    public PaymentClient getPaymentClient() {
        return paymentClient;
    }

    public void setPaymentClient(PaymentClient paymentClient) {
        this.paymentClient = paymentClient;
    }
    

    public String getPaymentCustomerId() {
        return paymentCustomerId;
    }

    public void setPaymentCustomerId(String paymentCustomerId) {
        this.paymentCustomerId = paymentCustomerId;
    }

    public List<KardexOutStocktakingView> getSelectedDetailProductOrderList() {
        if (null == selectedDetailProductOrderList) {
            selectedDetailProductOrderList = new ArrayList<>();
        }
        return selectedDetailProductOrderList;
    }

    public void setSelectedDetailProductOrderList(List<KardexOutStocktakingView> selectedDetailProductOrderList) {
        this.selectedDetailProductOrderList = selectedDetailProductOrderList;
    }

    public List<KardexOutStocktakingView> getDetailProductOrder() {
        return detailProductOrder;
    }

    public void setDetailProductOrder(List<KardexOutStocktakingView> detailProductOrder) {
        this.detailProductOrder = detailProductOrder;
    }

    public List<ReleaseResource> getRelease() {
        return release;
    }

    public void setRelease(List<ReleaseResource> release) {
        this.release = release;
    }

    public PaymentClientDetailBean() {
    }

    public List<PaymentClient> getPaymentClientList() {
        return paymentClientList;
    }

    public void setPaymentClientList(List<PaymentClient> paymentClientList) {
        this.paymentClientList = paymentClientList;
    }

    public List<InvoiceHasProduct> getDetailsinvoice() {
        return detailsinvoice;
    }

    public void setDetailsinvoice(List<InvoiceHasProduct> detailsinvoice) {
        this.detailsinvoice = detailsinvoice;
    }

    public List<Invoice> getInvoice() {
        return invoice;
    }

    public void setInvoice(List<Invoice> invoice) {
        this.invoice = invoice;
    }

    public List<ServiceOrderHasProduct> getDetails() {
        return details;
    }

    public void setDetails(List<ServiceOrderHasProduct> details) {
        this.details = details;
    }

    public ServiceOrder getSelectedServiceOrder() {
        return selectedServiceOrder;
    }

    public void setSelectedServiceOrder(ServiceOrder selectedServiceOrder) {
        this.selectedServiceOrder = selectedServiceOrder;
    }

    public List<ServiceOrder> getServiceOrders() {
        return serviceOrders;
    }

    public void setServiceOrders(List<ServiceOrder> serviceOrders) {
        this.serviceOrders = serviceOrders;
    }

    public List<Expense> getExpense() {
        return expense;
    }

    public void setExpense(List<Expense> expense) {
        this.expense = expense;
    }

    public List<Sale> getSales() {
        return sales;
    }

    public void setSales(List<Sale> sales) {
        this.sales = sales;
    }

    public ReportCustomerPayController getController() {
        return controller;
    }

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

    public List<ReportCustomerPay> getReportCustomerPays() {
        return reportCustomerPays;
    }

    public void setReportCustomerPays(List<ReportCustomerPay> reportCustomerPays) {
        this.reportCustomerPays = reportCustomerPays;
    }

    public Date getStartDate() {
        if (null == startDate) {
            Calendar calendar = Calendar.getInstance();

            calendar.add(Calendar.MONTH, -1);

            calendar.set(HOUR_OF_DAY, 0);
            calendar.set(MINUTE, 0);
            calendar.set(SECOND, 0);
            calendar.set(MILLISECOND, 0);

            startDate = calendar.getTime();
        }

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

}
