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

import com.arrebol.taxiservicios.beans.GenericPersonBean;
import com.arrebol.taxiservicios.controller.admin.DriverController;
import com.arrebol.taxiservicios.model.enums.PersonType;
import com.arrebol.taxiservicios.model.view.PersonListView;
import java.io.Serializable;
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;

/**
 *
 * @author Picasso
 */
@Named("providerListBean")
@ViewScoped
public class ProviderListBean extends GenericPersonBean implements Serializable {

    /**
     *
     * @param outcome
     * @return
     */
    public String editProvider(String outcome) {
        return outcome;
    }

    public void deleteSelectedProvider() {
        try {
            if (null == getLoggedUser().getPerson()
                    || null == getLoggedUser().getPerson().getId()
                    || getLoggedUser().getPerson().getId().equals(getSelectedProvider().getId())) {
                showMessage(FacesMessage.SEVERITY_WARN,
                        getBundlePropertyFile().getString("provider.delete.title"),
                        getBundlePropertyFile().getString(
                                null == getLoggedUser().getPerson()
                                || null == getLoggedUser().getPerson().getId()
                                ? "provider.delete.cannot.empty"
                                : "provider.delete.cannot.delete"
                        ));
            } else {
                boolean success = getController().deletePersonById(
                        getSelectedProvider().getId(), getLoggedUser().getUserName());

                if (success) {
                    getProviders().remove(getSelectedProvider());
                    setSelectedProvider(null);

                    showMessage(FacesMessage.SEVERITY_INFO,
                            getBundlePropertyFile().getString("provider.delete.title"),
                            getBundlePropertyFile().getString("provider.delete.success.desc"));
                } else {
                    logger.error("Provider " + getSelectedProvider() + "was not deleted");
                    showMessage(FacesMessage.SEVERITY_ERROR,
                            getBundlePropertyFile().getString("provider.delete.title"),
                            getBundlePropertyFile().getString("provider.delete.fail.desc"));
                }
            }
        } catch (Exception e) {
            logger.error("deleteSelectedProvider: " + e);

            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("provider.delete.title"),
                    getBundlePropertyFile().getString("provider.delete.exception.desc"));
        }
    }

    private static final long serialVersionUID = 8669216775269708237L;

    final Logger logger = LogManager.getLogger(getClass());

    private List<PersonListView> providers;
    private PersonListView selectedProvider;

    @PostConstruct
    public void init() {
        try {
            loadBundlePropertyFile();

            setController(new DriverController());
            setProviders(getController().findListOfPersonsByType(PersonType.PROVIDER, getLoggedUser().getLocation()));
        } catch (Exception e) {
            logger.error("Provider init", e);
        }
    }

    public List<PersonListView> getProviders() {
        return providers;
    }

    public void setProviders(List<PersonListView> providers) {
        this.providers = providers;
    }

    public PersonListView getSelectedProvider() {
        return selectedProvider;
    }

    public void setSelectedProvider(PersonListView selectedProvider) {
        this.selectedProvider = selectedProvider;
    }

}
