/*
 * 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 plate.wellmann.shop.business;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;
import plate.wellmann.shop.persistence.Customer;
import plate.wellmann.shop.persistence.Item;
import plate.wellmann.shop.persistence.UserDomain;

/**
 *
 * @author Patrick
 */
@Named(value = "itemManager")
@SessionScoped
public class ItemManager implements Serializable {

    @Inject
    private Customer activeCustomer;

    private List<Item> tickets;

    @PersistenceContext(unitName = "MySQL-Wellmann")
    private EntityManager em;

    @Produces
    @RequestScoped
    public List<Item> getActiveTickets() {
        if (tickets == null) {
            tickets = new ArrayList<>();
            return tickets;
        }

        List<Item> activeTickets;
        activeTickets = new ArrayList<>();

        tickets.stream().filter((ticket) -> (ticket.getStatus() != Item.TicketStatus.CLOSED)).forEach((ticket) -> {
            activeTickets.add(ticket);
        });
        return activeTickets;
    }

    @Transactional
    public List<Item> findActiveDomainTickets(UserDomain userDomain) {
        Query qry = em.createQuery("select object(o) from Item o "
                + "where o.creator IN (:creators) "
                + "AND "
                + "o.status <> :status",
                Item.class);

        qry.setParameter("creators", userDomain.getCustomers());
        qry.setParameter("status", Item.TicketStatus.CLOSED);
        try {
            return qry.getResultList();
        } catch (Exception e) {
        }
        return new ArrayList<>();
    }
    
    @Transactional
    public List<Item> findDomainTickets(UserDomain userDomain){
        Query qry = em.createQuery("select object(o) from Item o "
                + "where o.creator IN (:creators)",
                Item.class);
        
        qry.setParameter("creators", userDomain.getCustomers());
        
        try {
            return qry.getResultList();
        } catch (Exception e) {
        }
        return new ArrayList<>();
    }
    
    @Transactional
    public List<Item> findDomainTicketsByStatus(UserDomain userDomain, List <Item.TicketStatus> status) {
        Query qry = em.createQuery("select object(o) from Item o "
                + "where o.creator IN (:creators) "
                + "AND "
                + "o.status IN (:status)",
                Item.class);

        qry.setParameter("creators", userDomain.getCustomers());
        qry.setParameter("status", status);
        try {
            return qry.getResultList();
        } catch (Exception e) {
        }
        return new ArrayList<>();
    }
    
    @Transactional
    public List<Item> findAllTicketsByStatus(List <Item.TicketStatus> status){
        Query qry = em.createQuery("select object(o) from Item o "
                + "where o.status IN (:status)",
                Item.class);
        qry.setParameter("status", status);
        
        try {
            return qry.getResultList();
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }
}
