/*
 * 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.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.admin.AdminSalesController;
import com.arrebol.taxiservicios.model.core.Person;
import com.arrebol.taxiservicios.model.core.User;
import com.arrebol.taxiservicios.model.dashboard.ToCollect;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
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;

/**
 *
 * @author Picasso
 */
@Named("adminSaleBean")
@ViewScoped
public class AdminSaleBean extends TaxiGenericBean implements Serializable {

    /**
     * Exceute add/delete into TO_COLLECT table.
     */
    public void actionToCollect() {
        logger.info("actionToCollect");
        try {
            if (isNewToCollect()) {
                addToCollect();
            } else {
                deleteToCollect();
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    /**
     * On change admin to collect select one menu.
     */
    public void onAdminToCollectSlctChange() {
        logger.info("onAdminToCollectSlctChange");
        try {
            setStart(true);
            setShowButton(false);

            if (null != getActionToColect()) {
                setShowButton(true);
                setNewToCollect(false);
                setStart(false);

                if (getActionToColect().equals("add")) {
                    setNewToCollect(true);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    /**
     * On change driver select one menu.
     */
    public void onDriveSlctChange() {
        logger.info("onDriveSlctChange");
        try {
            if (null != getDriverId()) {
                setToCollects(
                        getController().getAllToCollectsByDriver(
                                new Person(getDriverId())
                        )
                );
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    /**
     *
     * @param date
     * @return
     */
    public String converterDate(Date date) {
        logger.info("concerter");
        String result = "";
        try {
            SimpleDateFormat format = new SimpleDateFormat("dd - MMMM - yyyy");

            result = format.format(date);
        } catch (Exception e) {
            logger.error(e);
        }
        return result;
    }

    /**
     * Delete a register in TO_COLLECT table from driver by date.
     */
    private void deleteToCollect() {
        logger.info("deleteToCollect");
        try {
            if (getController().deleteToCollect(getIdToDelete())) {
                onDriveSlctChange();
                showMessage(FacesMessage.SEVERITY_INFO,
                        getBundlePropertyFile().getString("admin.sale.to.collect.title"),
                        getBundlePropertyFile().getString("admin.sale.to.collect.delete.success.desc"));
            } else {
                logger.error("To collect can not be deleted");
                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("admin.sale.to.collect.title"),
                        getBundlePropertyFile().getString("admin.sale.to.collect.delete.fail.desc"));
            }
        } catch (Exception e) {
            logger.error("deleteToColelct: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("admin.sale.to.collect.title"),
                    getBundlePropertyFile().getString("admin.sale.to.collect.delete.exception.desc"));
        }
    }

    /**
     * Add a new register in TO_COLLECT table.
     */
    public void addToCollect() {
        logger.info("addToCollect");
        try {
            Calendar date = Calendar.getInstance();

            date.setTime(getToCollectDate());

            date.set(Calendar.HOUR_OF_DAY, 0);
            date.set(Calendar.MINUTE, 0);
            date.set(Calendar.SECOND, 0);
            date.set(Calendar.MILLISECOND, 0);

            setToCollect(
                    new ToCollect(
                            UUID.randomUUID().toString(),
                            getToCollectDate(), Boolean.TRUE, 0, new Person(getDriverId()),
                            new User(getLoggedUser().getId(), ""),
                            new Date()
                    )
            );

            if (getController().addToCollect(getToCollect())) {

                showMessage(FacesMessage.SEVERITY_INFO,
                        getBundlePropertyFile().getString("admin.sale.to.collect.title"),
                        getBundlePropertyFile().getString("admin.sale.to.collect.add.success.desc"));
            } else {
                logger.error("To collect can not be saved");
                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("admin.sale.to.collect.title"),
                        getBundlePropertyFile().getString("admin.sale.to.collect.add.fail.desc"));
            }
        } catch (Exception e) {
            logger.error("addToCollect: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("admin.sale.to.collect.title"),
                    getBundlePropertyFile().getString("admin.sale.to.collect.add.exception.desc"));
        }
    }

    private static final long serialVersionUID = -6479847794382160143L;
    final Logger logger = LogManager.getLogger(AdminSaleBean.class);

    private AdminSalesController controller;
    private Person driver;
    private ToCollect toCollect;
    private List<Person> drivers;
    private String idToDelete;
    private Date toCollectDate;
    private boolean showButton;
    private boolean newToCollect;
    private String actionToColect;
    private boolean start;
    private String driverId;
    private List<ToCollect> toCollects;

    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            loadBundlePropertyFile();
            setController(new AdminSalesController());
            setDrivers(getController().getAllDrivers());
            setShowButton(false);
            setStart(true);
        } catch (Exception e) {
            logger.error(e);
        }
    }

    public AdminSalesController getController() {
        return controller;
    }

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

    public Person getDriver() {
        return driver;
    }

    public void setDriver(Person driver) {
        this.driver = driver;
    }

    public ToCollect getToCollect() {
        return toCollect;
    }

    public void setToCollect(ToCollect toCollect) {
        this.toCollect = toCollect;
    }

    public List<Person> getDrivers() {
        return drivers;
    }

    public void setDrivers(List<Person> drivers) {
        this.drivers = drivers;
    }

    public String getIdToDelete() {
        return idToDelete;
    }

    public void setIdToDelete(String idToDelete) {
        this.idToDelete = idToDelete;
    }

    public Date getToCollectDate() {
        return toCollectDate;
    }

    public void setToCollectDate(Date toCollectDate) {
        this.toCollectDate = toCollectDate;
    }

    public boolean isShowButton() {
        return showButton;
    }

    public void setShowButton(boolean showButton) {
        this.showButton = showButton;
    }

    public boolean isNewToCollect() {
        return newToCollect;
    }

    public void setNewToCollect(boolean newToCollect) {
        this.newToCollect = newToCollect;
    }

    public String getActionToColect() {
        return actionToColect;
    }

    public void setActionToColect(String actionToColect) {
        this.actionToColect = actionToColect;
    }

    public boolean isStart() {
        return start;
    }

    public void setStart(boolean start) {
        this.start = start;
    }

    public String getDriverId() {
        return driverId;
    }

    public void setDriverId(String driverId) {
        this.driverId = driverId;
    }

    public List<ToCollect> getToCollects() {
        return toCollects;
    }

    public void setToCollects(List<ToCollect> toCollects) {
        this.toCollects = toCollects;
    }

}
