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

import com.arrebol.taxiservicios.controller.admin.*;
import com.arrebol.taxiservicios.controller.connection.ConnectionManager;
import com.arrebol.taxiservicios.controller.util.HibernateUtil;
import com.arrebol.taxiservicios.model.admin.EmployeeSanction;
import com.arrebol.taxiservicios.model.admin.Sanction;
import com.arrebol.taxiservicios.model.admin.Taxi;
import com.arrebol.taxiservicios.model.catalog.Color;
import com.arrebol.taxiservicios.model.catalog.Location;
import com.arrebol.taxiservicios.model.catalog.Vehicle;
import com.arrebol.taxiservicios.model.core.Person;
import com.arrebol.taxiservicios.model.core.User;
import com.arrebol.taxiservicios.model.enums.EmployeeSanctionType;
import com.arrebol.taxiservicios.model.enums.PersonType;
import com.arrebol.taxiservicios.model.enums.SanctionType;
import com.arrebol.taxiservicios.model.mutual.MutualAdd;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaDelete;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Path;
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 oscarvargas
 */
public class MutualAddController extends ConnectionManager implements Serializable{
    
    public List<Taxi> fillTaxisComboBox() {

        Transaction transaction = null;
        List<Taxi> result = null;

        try {
            logger.info("Searching all taxis to select..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

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

            Path<String> id = root.get("id");
            Path<String> placa = root.get("placa");
            Path<String> year = root.get("year");
            Path<Vehicle> vehicle = root.get("vehicle");
            Path<Color> color = root.get("color");

            criteria.select(
                    builder.construct(Taxi.class, id,
                            placa, year, vehicle, color
                    ));

            criteria.where(builder.equal(root.get("active"), Boolean.TRUE));

            criteria.orderBy(
                    builder.asc(root.get("placa"))
            );
            criteria.orderBy(
                    builder.asc(root.get("year"))
            );

            result = session.createQuery(criteria).getResultList();
            transaction.commit();
            logger.info("Total of taxis found for ComboBox: " + (null == result ? -1 : result.size()));
        } catch (HibernateException e) {
            logger.error("Can not load list of taxis for ComboBox ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillTaxisComboBox of Sale", e);
            rollback(transaction);
        }

        return result;
    }
    
    public List<MutualAdd> fillDataTableBetweenDates(Date starDate, Date endDate, Location location) {
        logger.info("fillDataTableBetweenDates");

        List<MutualAdd> results = null;
        Transaction transaction = null;

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

            results = session.createQuery("SELECT exp FROM MutualAdd exp "
                    + "WHERE exp.active = 1 AND "
                    + "(DATE(exp.createdOn) BETWEEN DATE(:starDate) AND DATE(:endDate)) AND "
                    + "exp.location.id = :location ORDER BY exp.createdOn DESC")
                    .setParameter("starDate", starDate)
                    .setParameter("endDate", endDate)
                    .setParameter("location", location.getId())
                    .getResultList();

            transaction.commit();
            logger.info("Total of MutualAdd founds in auto complete search " + results.size());
        } catch (HibernateException e) {
            rollback(transaction);
            logger.error("Can not load auto complete list of MutualAdd", e);
        } catch (Exception e) {
            rollback(transaction);
            logger.error("Method fillDataTableBetweenDates", e);
        }

        return results;
    }

    public List<Person> fillDriversComboBox() {

        Transaction transaction = null;
        List<Person> result = null;

        try {
            logger.info("Searching all drivers to select..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

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

            Path<String> idPath = root.get("id");
            Path<String> firstNamePath = root.get("firstName");
            Path<String> secondNamePath = root.get("secondName");
            Path<String> lastNamePath = root.get("lastName");
            Path<String> middleNamePath = root.get("middleName");

            criteria.select(
                    builder.construct(Person.class, idPath, firstNamePath, secondNamePath, lastNamePath, middleNamePath
                    ));

            criteria.where(builder.equal(root.get("personType"), PersonType.DRIVER));
            
            criteria.orderBy(
                    builder.asc(root.get("firstName")),
                    builder.asc(root.get("secondName")),
                    builder.asc(root.get("lastName")),
                    builder.asc(root.get("middleName"))
            );

            result = session.createQuery(criteria).getResultList();
            transaction.commit();
            logger.info("Total of drivers found for datatable: " + (null == result ? -1 : result.size()));
        } catch (HibernateException e) {
            logger.error("Can not load list of drivers for datatable ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillDriversComboBox of Sanction", e);
            rollback(transaction);
        }

        return result;
    }
    
    public boolean addMutualAdd(MutualAdd mutual, String userName, String driverId, String taxiId) {
        logger.info("addMutualAdd");
        boolean success = false;

        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            mutual.setCreatedBy(new User(userName, ""));
            if(driverId != null && !driverId.isEmpty())
                mutual.setDriver(new Person(driverId));
            else
                mutual.setDriver(null);
            
            if(taxiId != null && !taxiId.isEmpty())
                mutual.setTaxi(new Taxi(taxiId));
            else
                mutual.setTaxi(null);
            
            mutual.setActive(true);
            session.save(mutual);
            logger.info("mutual saved " + mutual.getId());

            transaction.commit();

            success = true;
        } catch (HibernateException e) {
            logger.error("mutual can not be added ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method addMutualAdd() ", e);
            rollback(transaction);
        }
        return success;
    }
    
   
    public MutualAdd getMutualAddById(String mutualAddId) {
        logger.info("getMutualAddById");

        MutualAdd mutualAdd = null;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            mutualAdd = session.get(MutualAdd.class, mutualAddId);

            transaction.commit();
            logger.info("mutualAdd " + mutualAdd);
        } catch (HibernateException e) {
            logger.error("mutualAdd cannot be loaded " + mutualAddId, e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method getMutualAddById(" + mutualAddId + ") ", e);
            rollback(transaction);
        }
        return mutualAdd;
    }
    
    public boolean deleteById(String mutualAddId) {
        logger.info("deleteById");

        boolean success = false;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            CriteriaBuilder builder = session.getCriteriaBuilder();

            builder = session.getCriteriaBuilder();
            CriteriaDelete<MutualAdd> delete = builder.createCriteriaDelete(MutualAdd.class);
            Root<MutualAdd> root = delete.from(MutualAdd.class);

                delete.where(builder.equal(root.get("id"), mutualAddId));

                int total = session.createQuery(delete).executeUpdate();

            if (1 == total) {
                transaction.commit();
                success = true;
                logger.info("MutualAdd " + mutualAddId + " deleted");
            } else {
                transaction.rollback();
                logger.error("Error MutualAdd deleted total: " + total);
            }
        } catch (HibernateException e) {
            logger.error("MutualAdd can not be deleted ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method deleteById() ", e);
            rollback(transaction);
        }
        return success;
    }
    
    private static final long serialVersionUID = 1020525241432403643L;
    
    final Logger logger = LogManager.getLogger(MutualAddController.class);

    public MutualAddController() {
    }
    
}
