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

import com.arrebol.apc.controller.util.HibernateUtil;
import com.arrebol.apc.model.ModelParameter;
import com.arrebol.apc.model.catalog.People;
import com.arrebol.apc.model.catalog.constance.PeopleCfg;
import com.arrebol.apc.model.core.User;
import com.arrebol.apc.model.core.constance.LoanCfg;
import com.arrebol.apc.model.enums.ActiveStatus;
import com.arrebol.apc.model.loan.Loan;
import com.arrebol.apc.model.views.CustomerView;
import com.arrebol.apc.model.views.LoanDetailZeroView;
import com.arrebol.apc.model.views.LoanFinishedView;
import com.arrebol.apc.repository.GenericEntityRepository;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */
public class CustomerController extends PeopleController implements Serializable {

   public CustomerView findCustomerViewByPeopleId(String peopleId) {
      logger.debug("findCustomerViewByPeopleId");
      return (CustomerView) genericEntityRepository.selectAPCEntityById(CustomerView.class, peopleId);
   }

   /**
    *
    * Searching all customers.
    *
    * @param officeId
    * @return
    */
   public List<People> fillCustomersDatatable(String officeId) {
      logger.debug("fillCustomersDatatable");
      List<ModelParameter> parameters = new ArrayList<>();

      parameters.add(new ModelParameter(PeopleCfg.FIELD_ACTIVE_STATUS, ActiveStatus.ENEBLED));

      return genericEntityRepository.xmlQueryAPCEntities(People.class, PeopleCfg.QUERY_FIND_ALL_CUSTOMER, parameters);
   }

   /**
    *
    * Searching all loan by customer.
    *
    * @param peopleId
    * @return
    */
   public List<Loan> findLoanByCustomer(String peopleId) {
      logger.debug("findLoanByCustomer");
      List<ModelParameter> parameters = new ArrayList<>();

      parameters.add(new ModelParameter(LoanCfg.FIELD_CUSTOMER, new People(peopleId)));

      return genericEntityRepository.xmlQueryAPCEntities(Loan.class, LoanCfg.QUERY_FIND_LOAN_BY_CUSTOMER, parameters);
   }

   public List<Loan> findLoanByCustomerLimit(String peopleId) {
      logger.debug("findLoanByCustomer");
      List<Loan> results = new ArrayList<>();

      Transaction transaction = null;

      try {
         Session session = HibernateUtil.getSessionFactory().getCurrentSession();

         transaction = session.beginTransaction();

         results = session.createQuery("SELECT l FROM Loan l WHERE customer = :customer AND loanStatus != 'DELETED' ORDER BY createdOn DESC")
                 .setParameter("customer", peopleId)
                 .setMaxResults(2)
                 .getResultList();

         transaction.commit();

         logger.info("Closing daily detail's list " + results.size());
      } catch (HibernateException e) {
         logger.error("Can not find closing daily details list", e);
         rollback(transaction);
      } catch (Exception e) {
         logger.error("Method findDetailsFromClosingDayID()", e);
         rollback(transaction);
      }

      return results;
   }

   /**
    *
    * Searching all loan for juridical.
    *
    * @param idUser
    * @return
    */
   public List<Loan> findLoanJuridical(String idUser) {
      logger.debug("findLoanJuridical");
      List<ModelParameter> parameters = new ArrayList<>();
      parameters.add(new ModelParameter(LoanCfg.FIELD_USER, new User(idUser)));
      return genericEntityRepository.xmlQueryAPCEntities(Loan.class, LoanCfg.QUERY_FIND_LOAN_JURIDICAL, parameters);
   }

   public List<LoanDetailZeroView> findLoanZero() {
      logger.debug("findLoanZero");
      List<ModelParameter> parameters = new ArrayList<>();
      return genericEntityRepository.xmlQueryAPCEntities(LoanDetailZeroView.class, LoanCfg.QUERY_FIND_LOAN_ZERO, parameters);
   }

   public List<LoanFinishedView> findLoanFinished() {
      logger.debug("findLoanFinished");
      List<ModelParameter> parameters = new ArrayList<>();
      return genericEntityRepository.xmlQueryAPCEntities(LoanFinishedView.class, LoanCfg.QUERY_FIND_LOAN_FINISHED, parameters);
   }

   final Logger logger = LogManager.getLogger(CustomerController.class);
   private final GenericEntityRepository genericEntityRepository;

   public CustomerController() {
      this.genericEntityRepository = new GenericEntityRepository();
   }

   protected void rollback(Transaction transaction) {
      if (null != transaction) {
         transaction.rollback();
      }
   }
}
