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

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.ArrayList;
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("employeeListBean")
@ViewScoped
public class EmployeeListBean extends GenericPersonBean implements Serializable {

   public void deleteEmployeeSelected() {
      logger.info("deleteRow");
      try {

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

            if (success) {
               getEmployees().remove(getSelectedEmployee());
               setSelectedEmployee(null);

               showMessage(FacesMessage.SEVERITY_INFO,
                       getBundlePropertyFile().getString("employee.delete.title"),
                       getBundlePropertyFile().getString("employee.delete.success.desc"));
            } else {
               logger.error("Employee " + getSelectedEmployee() + "was not deleted");
               showMessage(FacesMessage.SEVERITY_ERROR,
                       getBundlePropertyFile().getString("employee.delete.title"),
                       getBundlePropertyFile().getString("employee.delete.fail.desc"));
            }
         }
      } catch (Exception e) {
         logger.error("deleteRow: " + e);

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

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

   private static final long serialVersionUID = 7563498302907468479L;

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

   private List<PersonListView> employees;
   private PersonListView selectedEmployee;

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

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

   public List<PersonListView> getEmployees() {
      if (null == employees) {
         employees = new ArrayList<>();
      }
      return employees;
   }

   public void setEmployees(List<PersonListView> employees) {
      this.employees = employees;
   }

   public PersonListView getSelectedEmployee() {
      return selectedEmployee;
   }

   public void setSelectedEmployee(PersonListView selectedEmployee) {
      this.selectedEmployee = selectedEmployee;
   }

}
