/*
 * Arrebol Consultancy copyright.
 *
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.apc.web.beans.admin;

import com.arrebol.apc.controller.GenericController;
import com.arrebol.apc.controller.admin.PeopleController;
import com.arrebol.apc.controller.catalog.RouteController;
import com.arrebol.apc.model.catalog.People;
import com.arrebol.apc.model.catalog.RouteCtlg;
import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.core.User;
import com.arrebol.apc.model.enums.ActiveStatus;
import com.arrebol.apc.model.enums.CustomerClassification;
import com.arrebol.apc.model.enums.PeopleType;
import com.arrebol.apc.web.beans.GenericBean;
import java.io.Serializable;
import java.util.Date;
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 Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */
@Named("peopleManager")
@ViewScoped
public class PeopleBean extends GenericBean implements Serializable {

   public void addPeople() {
      try {
         logger.debug("addPeople");
         getSavePeople().setOffice(new Office(getLoggedUser().getOffice().getId()));
         getSavePeople().setActiveStatus(ActiveStatus.ENEBLED);
         getSavePeople().setRouteCtlg(new RouteCtlg(routeId));
         getSavePeople().setLetraCuenta("");
         getSavePeople().setThumbnail("");
         getSavePeople().setClassification(CustomerClassification.WHITE);
         getSavePeople().setDatosCreditoVendedor(userVId);
         getSavePeople().setDatosCreditoAsignado(userAId);
         getSavePeople().setDatosCreditoInvestigador(userIId);

         if (isCustomer == true && isEndorsement == false) {
            getSavePeople().setPeopleType(PeopleType.CUSTOMER);
         }
         if (isCustomer == true && isEndorsement == true) {
            getSavePeople().setPeopleType(PeopleType.BOTH);
         }
         if (isCustomer == false && isEndorsement == true) {
            getSavePeople().setPeopleType(PeopleType.ENDORSEMENT);
         }
         if (isCustomer == false && isEndorsement == false) {
            getSavePeople().setPeopleType(PeopleType.BOTH);
         }

         String messafeFormat = getBundlePropertyFile().getString("message.format.failure");
         FacesMessage.Severity severity = FacesMessage.SEVERITY_WARN;

         if (getSavePeople().getId() != null) {
            getSavePeople().setLastUpdatedBy(getLoggedUser().getUser().getId());
            getSavePeople().setLastUpdatedOn(new Date());
            if (getPeopleCtrl().updateByPeopleId(getSavePeople())) {
               messafeFormat = "Datos actualizados";
               severity = FacesMessage.SEVERITY_INFO;
            }
         } else {
            getSavePeople().setCreatedBy(getLoggedUser().getUser().getId());
            getSavePeople().setCreatedOn(new Date());
            if (getPeopleCtrl().savePeople(getSavePeople())) {
               messafeFormat = getBundlePropertyFile().getString("message.format.sucess");
               severity = FacesMessage.SEVERITY_INFO;
            }
         }

         Object[] param = {getBundlePropertyFile().getString("people"), getBundlePropertyFile().getString("created")};

         buildAndSendMessage(param, messafeFormat, severity, getBundlePropertyFile().getString("people"));

      } catch (Exception e) {
         logger.error("savePeople", e);
         Object[] param = {getBundlePropertyFile().getString("created")};

         buildAndSendMessage(
                 param,
                 getBundlePropertyFile().getString("message.format.fatal"),
                 FacesMessage.SEVERITY_FATAL,
                 getBundlePropertyFile().getString("people")
         );
      }
   }

   public void limpiarForm() {
      setSavePeople(new People());
      isCustomer = false;
      isEndorsement = false;
   }

   public PeopleController getPeopleCtrl() {
      return peopleCtrl;
   }

   public void setPeopleCtrl(PeopleController peopleCtrl) {
      this.peopleCtrl = peopleCtrl;
   }

   public boolean isIsCustomer() {
      return isCustomer;
   }

   public void setIsCustomer(boolean isCustomer) {
      this.isCustomer = isCustomer;
   }

   public boolean isIsEndorsement() {
      return isEndorsement;
   }

   public void setIsEndorsement(boolean isEndorsement) {
      this.isEndorsement = isEndorsement;
   }

   public People getSavePeople() {
      return savePeople;
   }

   public void setSavePeople(People savePeople) {
      this.savePeople = savePeople;
   }

   public String getRouteId() {
      return routeId;
   }

   public void setRouteId(String routeId) {
      this.routeId = routeId;
   }

   public List<RouteCtlg> getRoutes() {
      return routes;
   }

   public void setRoutes(List<RouteCtlg> routes) {
      this.routes = routes;
   }

   public RouteController getRouteCtrl() {
      return routeCtrl;
   }

   public void setRouteCtrl(RouteController routeCtrl) {
      this.routeCtrl = routeCtrl;
   }

   public String getRouteLetra() {
      return routeLetra;
   }

   public void setRouteLetra(String routeLetra) {
      this.routeLetra = routeLetra;
   }

   public List<User> getUser() {
      return user;
   }

   public void setUser(List<User> user) {
      this.user = user;
   }

   public String getUserVId() {
      return userVId;
   }

   public void setUserVId(String userVId) {
      this.userVId = userVId;
   }

   public String getUserIId() {
      return userIId;
   }

   public void setUserIId(String userIId) {
      this.userIId = userIId;
   }

   public String getUserAId() {
      return userAId;
   }

   public void setUserAId(String userAId) {
      this.userAId = userAId;
   }

   final Logger logger = LogManager.getLogger(PeopleBean.class);

   private PeopleController peopleCtrl;
   private RouteController routeCtrl;
   private GenericController genericCtrl;
   boolean isCustomer;
   boolean isEndorsement;
   private People savePeople;
   String routeId;
   String routeLetra;
   List<RouteCtlg> routes;
   private List<User> user;
   String userVId;
   String userIId;
   String userAId;

   @PostConstruct
   public void init() {
      loadBundlePropertyFile();
      peopleCtrl = new PeopleController();
      routeCtrl = new RouteController();
      genericCtrl = new GenericController();
      setSavePeople(new People());
      isCustomer = false;
      isEndorsement = false;
      routes = routeCtrl.fillRoutesDatatable(getLoggedUser().getOffice().getId());
      user = genericCtrl.getAllUsersByOffice(getLoggedUser().getOffice().getId());
   }
}
