package com.peetz.tradingcards.service;

import com.peetz.tradingcards.dal.ManufacturerDao;
import java.util.Collection;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import com.peetz.tradingcards.entity.BaseSetEntity;
import com.peetz.tradingcards.entity.InsertEntity;
import com.peetz.tradingcards.entity.ManufacturerEntity;
import com.peetz.tradingcards.entity.ParallelSetEntity;
import com.peetz.tradingcards.entity.SportCardEntity;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless(name="TradingcardService")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class TradingcardServiceImpl implements TradingcardService {

    @PersistenceContext(unitName = "kontor")
    private EntityManager em;
    
    @EJB
    ManufacturerDao manufacturerDao;

    @SuppressWarnings("unchecked")
	@Override
    public Collection<ManufacturerEntity> getAllManufacturers() {
        Query query = em.createNamedQuery("Manufacturer.findAll");
        ArrayList<ManufacturerEntity> manufacturerList = new ArrayList<ManufacturerEntity>(query.getResultList());
        return manufacturerList;
    }

    @Override
    public void addManufacturer(String name) {
        List<ManufacturerEntity> resultList = manufacturerDao.findByName(name);
        if (resultList.isEmpty()) {
            ManufacturerEntity manufacturer = new ManufacturerEntity();
            manufacturer.setName(name);
            manufacturerDao.store(manufacturer);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public Collection<SportCardEntity> getAllSportCards() {
        Query query = em.createNamedQuery("SportCard.findAll");
        ArrayList<SportCardEntity> sportCardList = new ArrayList<SportCardEntity>(query.getResultList());
        return sportCardList;
    }

    @Override
    public Collection<BaseSetEntity> getBaseSetsByManufacturer(ManufacturerEntity manufacturer) {
		// TODO Auto-generated method stub
    	return null;
    }

    @Override
    public Collection<ParallelSetEntity> getParallelSetsByManufacturer(ManufacturerEntity manufacturer) {
		// TODO Auto-generated method stub
    	return null;
    }

    @Override
	public Collection<InsertEntity> getInsertsByManufacturer(ManufacturerEntity manufacturer) {
		// TODO Auto-generated method stub
    	return null;
    }
}