package siprp.database.cayenne.providers;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.cayenne.DataObjectUtils;
import org.apache.cayenne.exp.ExpressionFactory;
import org.apache.cayenne.query.SelectQuery;

import siprp.database.cayenne.objects.EmailPlanoDeActuacao;
import siprp.database.cayenne.objects.Estabelecimentos;
import siprp.database.cayenne.objects.HsEmailEstabelecimento;
import siprp.database.cayenne.objects.HsEquipamento;
import siprp.database.cayenne.objects.HsLegislacao;
import siprp.database.cayenne.objects.HsLegislacaoCategoria;
import siprp.database.cayenne.objects.HsNormalizacao;
import siprp.database.cayenne.objects.HsPosto;
import siprp.database.cayenne.objects.HsPostoRisco;
import siprp.database.cayenne.objects.HsRelatorio;
import siprp.database.cayenne.objects.HsRelatorioRisco;
import siprp.database.cayenne.objects.HsRelatorioRiscoValorQualitativo;
import siprp.database.cayenne.objects.HsRisco;
import siprp.database.cayenne.objects.HsRiscoTema;
import siprp.database.cayenne.objects.MarcacoesEstabelecimento;

public class PlanoActuacaoDAO extends MainDAO
{
	
	private static final Integer ID_COMPANHIA_HIPERMERCADOS = 32;

	public Estabelecimentos getEstabelecimentoByID( Integer id )
	{
		return (Estabelecimentos) DataObjectUtils.objectForPK( context, Estabelecimentos.class, id );
	}

	public void addEmailToEstabelecimento( String email, Estabelecimentos estabelecimento )
	{
		EmailPlanoDeActuacao emailObject = (EmailPlanoDeActuacao) context.newObject( EmailPlanoDeActuacao.class );
		emailObject.setDescription( email );
		emailObject.setToEstabelecimentos( estabelecimento );
		context.commitChanges();
	}

	public List<HsRiscoTema> getAllRiscoTemas()
	{
		SelectQuery query = new SelectQuery( HsRiscoTema.class );
		query.andQualifier( ExpressionFactory.matchExp( HsRiscoTema.DELETED_DATE_PROPERTY, null ) );
		query.addOrdering( HsRiscoTema.DESCRIPTION_PROPERTY, true );
		return context.performQuery( query );
	}

	public HsRelatorio criarRelatorioForVisita( MarcacoesEstabelecimento visita )
	{
		HsRelatorio result = null;
		if( visita != null )
		{
			result = (HsRelatorio) context.newObject( HsRelatorio.class );
			result.setToHsMarcacoesEstabelecimento( visita );
			result.setData( new Date() );
			context.commitChanges();
		}
		return result;
	}

	public HsPostoRisco createValoresFor( HsRisco risco, HsPosto posto )
	{
		HsPostoRisco result = null;
		if( risco != null && posto != null )
		{
			result = (HsPostoRisco) context.newObject( HsPostoRisco.class );
			result.setToHsPosto( posto );
			result.setToHsRisco( risco );
			result.setIsPlanoActuacao( "n" );
			context.commitChanges();
		}
		return result;
	}

	public List<HsRelatorioRiscoValorQualitativo> getAllValoresQualitativos()
	{
		SelectQuery query = new SelectQuery( HsRelatorioRiscoValorQualitativo.class );
		return context.performQuery( query );
	}

	/**
	 * Get 
	 * @param dates
	 * @param estabelecimentoID
	 * @return
	 */
	public List<HsRelatorio> getRelatoriosNotIn( List<Date> dates, Integer estabelecimentoID )
	{
		List<HsRelatorio> result = new LinkedList<HsRelatorio>();
		dates = dates != null ? dates : new LinkedList<Date>();
		SelectQuery query = new SelectQuery( HsRelatorio.class );
		query.andQualifier( ExpressionFactory.noMatchExp( HsRelatorio.IS_SUBMETIDO_PROPERTY, null ) );
		query.andQualifier( ExpressionFactory.matchExp( HsRelatorio.DELETED_DATE_PROPERTY, null ) );
		List<HsRelatorio> all = context.performQuery( query );
		for( HsRelatorio relatorio : all )
		{
			MarcacoesEstabelecimento visita = relatorio.getToHsMarcacoesEstabelecimento();
			if( visita != null )
			{
				if( visita.getToEstabelecimentos() != null && visita.getToEstabelecimentos().getToEmpresas().getId().equals( ID_COMPANHIA_HIPERMERCADOS ) )
				{
					if( estabelecimentoID == null || estabelecimentoID != null && estabelecimentoID.equals( visita.getEstabelecimentoID() ) )
					{
						Date dateVisita = visita.getData();
						if( dateVisita != null && !dates.contains( dateVisita ) )
						{
							result.add( relatorio );
						}
					}
				}
			}
		}
		return result;
	}

    public List<HsRelatorio> getRelatoriosNotIn( List<Integer> knownEstabelecimentosIDs )
	{
		List<HsRelatorio> result = new LinkedList<HsRelatorio>();
		knownEstabelecimentosIDs = knownEstabelecimentosIDs != null ? knownEstabelecimentosIDs : new LinkedList<Integer>();
		SelectQuery query = new SelectQuery( HsRelatorio.class );
		query.andQualifier( ExpressionFactory.noMatchExp( HsRelatorio.IS_SUBMETIDO_PROPERTY, null ) );
		query.andQualifier( ExpressionFactory.matchExp( HsRelatorio.DELETED_DATE_PROPERTY, null ) );
		List<HsRelatorio> all = context.performQuery( query );
		for( HsRelatorio relatorio : all )
		{
			MarcacoesEstabelecimento visita = relatorio.getToHsMarcacoesEstabelecimento();
			if( visita != null )
			{
				if( visita.getToEstabelecimentos() != null && visita.getToEstabelecimentos().getToEmpresas().getId().equals( ID_COMPANHIA_HIPERMERCADOS ) )
				{
                    Integer estabelecimentoID = visita.getToEstabelecimentos().getId();
					if( !knownEstabelecimentosIDs.contains(estabelecimentoID) )
					{
                        result.add( relatorio );
					}
				}
			}
		}
		return result;
	}

	public List<HsLegislacao> getAllLegislacaoGeral()
	{
		SelectQuery query = new SelectQuery( HsLegislacao.class );
		query.andQualifier( ExpressionFactory.matchExp( HsLegislacao.DELETED_DATE_PROPERTY, null ) );
		query.andQualifier( ExpressionFactory.matchExp( HsLegislacao.TO_HS_LEGISLACAO_CATEGORIA_PROPERTY, null ) );
		query.addOrdering( HsLegislacao.DESCRIPTION_PROPERTY, true );
		return context.performQuery( query );
	}
	
	public List<HsLegislacaoCategoria> getAllLegislacaoCategorias()
	{
		SelectQuery query = new SelectQuery( HsLegislacaoCategoria.class );
		query.andQualifier( ExpressionFactory.matchExp( HsLegislacaoCategoria.DELETED_DATE_PROPERTY, null ) );
		query.addOrdering( HsLegislacao.DESCRIPTION_PROPERTY, true );
		return context.performQuery( query );
	}

	public List<HsNormalizacao> getNormalizacao( boolean portuguesa )
	{
		SelectQuery query = new SelectQuery( HsNormalizacao.class );
		query.andQualifier( ExpressionFactory.matchExp( HsNormalizacao.DELETED_DATE_PROPERTY, null ) );
		query.andQualifier( ExpressionFactory.matchExp( HsNormalizacao.PORTUGUESA_PROPERTY, portuguesa ) );
		query.addOrdering( HsNormalizacao.CODIGO_PROPERTY, true );
		return context.performQuery( query );
	}

	public List<HsEquipamento> getAllEquipamentos()
	{
		SelectQuery query = new SelectQuery( HsEquipamento.class );
		query.andQualifier( ExpressionFactory.matchExp( HsEquipamento.DELETED_DATE_PROPERTY, null ) );
		query.addOrdering( HsEquipamento.TIPO_PROPERTY, true );
		return context.performQuery( query );
	}

	public List<HsEmailEstabelecimento> getEmailsForEstabelecimento( Estabelecimentos estabelecimento )
	{
		SelectQuery query = new SelectQuery( HsEmailEstabelecimento.class );
		query.andQualifier( ExpressionFactory.matchExp( HsEmailEstabelecimento.TO_ESTABELECIMENTO_PROPERTY, estabelecimento ) );
		return context.performQuery( query );
	}

	public List<HsRisco> findHsRiscosFor( HsRelatorioRisco relatorioRisco )
	{
		SelectQuery query = new SelectQuery( HsRisco.class );
		query.andQualifier( ExpressionFactory.likeIgnoreCaseExp( HsRisco.DESCRIPTION_PROPERTY, relatorioRisco.getDescription() ) );
		return context.performQuery( query );
	}

}
