/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.crov.prase.controller.prase;

import com.arrebol.taxiservicios.controller.connection.ConnectionManager;
import com.arrebol.taxiservicios.controller.util.HibernateUtil;
import com.arrebol.taxiservicios.model.catalog.Location;
import com.arrebol.taxiservicios.model.enums.GenericEnumType;
import com.arrebol.taxiservicios.model.enums.TipoCobertura;
import com.crov.prase.model.prase.Coberturas;
import com.crov.prase.model.prase.OpcionesUsoVehiculo;
import com.crov.prase.model.prase.PaquetesCobertura;
import com.crov.prase.model.prase.Poliza;
import com.crov.prase.model.prase.UsoVehiculo;
import java.io.Serializable;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
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 Crov
 */
public class CatalogosCotizadorController extends ConnectionManager implements Serializable {

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

    public List<UsoVehiculo> fillUsoVehiculoByLocation(Location location) {
        logger.info("fillUsoVehiculoByLocation");
        List<UsoVehiculo> resultList = null;
        Transaction transaction = null;
        try {
            logger.info("Searching uso vehiculo for location..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

            CriteriaQuery<UsoVehiculo> criteria = builder.createQuery(UsoVehiculo.class);
            Root<UsoVehiculo> root = criteria.from(UsoVehiculo.class);

            Predicate criterio1 = builder.equal(root.get("location"), location);
            Predicate criterio2 = builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED);
            criteria.where(builder.and(criterio1, criterio2));
            criteria.orderBy(builder.desc(root.get("titulo")));

            resultList = session.createQuery(criteria).getResultList();

            logger.info("Total of uso vehiculo found: " + (null == resultList ? -1 : resultList.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Uso vehiculo cannot be loade ");
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillUsoVehiculoByLocation ", e);
            rollback(transaction);
        }
        return resultList;
    }

    public List<OpcionesUsoVehiculo> fillOpcionesUsoVehiculoByUsoVehiculo(String uso) {
        logger.info("fillOpcionesUsoVehiculoByUsoVehiculo");
        List<OpcionesUsoVehiculo> resultList = null;
        Transaction transaction = null;
        try {
            logger.info("Searching opciones uso vehiculo for uso..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

            CriteriaQuery<OpcionesUsoVehiculo> criteria = builder.createQuery(OpcionesUsoVehiculo.class);
            Root<OpcionesUsoVehiculo> root = criteria.from(OpcionesUsoVehiculo.class);

            Predicate criterio1 = builder.equal(root.get("usoVehiculo").get("id"), uso);
            Predicate criterio2 = builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED);
            criteria.where(builder.and(criterio1, criterio2));
            criteria.orderBy(builder.desc(root.get("titulo")));

            resultList = session.createQuery(criteria).getResultList();

            logger.info("Total of opciones uso vehiculo found: " + (null == resultList ? -1 : resultList.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Opciones uso vehiculo cannot be loade ");
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillOpcionesUsoVehiculoByUsoVehiculo ", e);
            rollback(transaction);
        }
        return resultList;
    }

    public List<PaquetesCobertura> fillPaquetesCoberturaByLocation(Location location) {
        logger.info("fillPaquetesCoberturaByLocation");
        List<PaquetesCobertura> resultList = null;
        Transaction transaction = null;
        try {
            logger.info("Searching paquetes cobertura for location..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

            CriteriaQuery<PaquetesCobertura> criteria = builder.createQuery(PaquetesCobertura.class);
            Root<PaquetesCobertura> root = criteria.from(PaquetesCobertura.class);

            Predicate criterio1 = builder.equal(root.get("location"), location);
            Predicate criterio2 = builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED);
            criteria.where(builder.and(criterio1, criterio2));
            criteria.orderBy(builder.desc(root.get("tipo")));

            resultList = session.createQuery(criteria).getResultList();

            logger.info("Total of paquetes cobertura found: " + (null == resultList ? -1 : resultList.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Paquetes cobertura cannot be loade ");
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillPaquetesCoberturaByLocation ", e);
            rollback(transaction);
        }
        return resultList;
    }

    public PaquetesCobertura findPaqueteCobertura(String idPoliza) {
        logger.info("findPaqueteCobertura");
        PaquetesCobertura results = null;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

            CriteriaQuery<PaquetesCobertura> query = builder.createQuery(PaquetesCobertura.class);
            Root<PaquetesCobertura> root = query.from(PaquetesCobertura.class);

            Predicate id = builder.equal(root.get("id"), idPoliza);
            query.where(builder.and(id));
            results = session.createQuery(query).getSingleResult();
            transaction.commit();

            logger.info("Poliza" + results.getId());
        } catch (HibernateException e) {
            logger.error("Can not load Paquete Cobertura", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method findPaqueteCobertura() ", e);
            rollback(transaction);
        }
        return results;
    }

    public List<Coberturas> fillCoberturasByPaquete(String idPaquete) {
        logger.info("fillCoberturasByPaquete");
        List<Coberturas> resultList = null;
        Transaction transaction = null;
        try {
            logger.info("Searching coberturas for paquete..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

            CriteriaQuery<Coberturas> criteria = builder.createQuery(Coberturas.class);
            Root<Coberturas> root = criteria.from(Coberturas.class);

            Predicate criterio1 = builder.equal(root.get("paquete").get("id"), idPaquete);
            Predicate criterio2 = builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED);
            criteria.where(builder.and(criterio1, criterio2));
            criteria.orderBy(builder.desc(root.get("tipo")));

            resultList = session.createQuery(criteria).getResultList();

            logger.info("Total of coberturas found: " + (null == resultList ? -1 : resultList.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Coberturas cannot be loade ");
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillCoberturasByPaquete ", e);
            rollback(transaction);
        }
        return resultList;
    }

    public List<Coberturas> fillCoberturasByPaqueteAndTipo(String idPaquete, TipoCobertura tipo) {
        logger.info("fillCoberturasByPaquete");
        List<Coberturas> resultList = null;
        Transaction transaction = null;
        try {
            logger.info("Searching coberturas for paquete..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

            CriteriaQuery<Coberturas> criteria = builder.createQuery(Coberturas.class);
            Root<Coberturas> root = criteria.from(Coberturas.class);

            Predicate criterio1 = builder.equal(root.get("paquete").get("id"), idPaquete);
            Predicate criterio2 = builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED);
            Predicate criterio3 = builder.equal(root.get("tipo"), tipo);
            criteria.where(builder.and(criterio1, criterio2, criterio3));
            criteria.orderBy(builder.desc(root.get("tipo")));

            resultList = session.createQuery(criteria).getResultList();

            logger.info("Total of coberturas found: " + (null == resultList ? -1 : resultList.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Coberturas cannot be loade ");
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillCoberturasByPaquete ", e);
            rollback(transaction);
        }
        return resultList;
    }

}
