package br.mp.mprj.portal.portlet.gestaomensagens.action;

import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.ProcessAction;

import br.mp.mprj.portal.portlet.gestaomensagens.service.model.EmailDestinatario;
import br.mp.mprj.portal.portlet.gestaomensagens.service.model.GrupoDestinatario;
import br.mp.mprj.portal.portlet.gestaomensagens.service.service.EmailDestinatarioLocalServiceUtil;
import br.mp.mprj.portal.portlet.gestaomensagens.action.GrupoDestinatarioAlterarIncluirActionCommand;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.util.bridges.mvc.ActionCommand;

public class IncluirEmailAction implements ActionCommand  {

	
	private static final Log _log = LogFactoryUtil.getLog(GrupoDestinatarioAlterarIncluirActionCommand.class);
	
		public IncluirEmailAction() {
			
			
			
			
		}
		@SuppressWarnings("unused")
		private void ProcesssAction() {
			// TODO Auto-generated method stub
			System.out.println("teste 0 ");
		}
		
		@Override
		public boolean processCommand(PortletRequest request, PortletResponse arg1)
				throws PortletException {
			
				System.out.println("teste 1 ");
				 GrupoDestinatario grupoDestinatario = (GrupoDestinatario) request.getAttribute("grupoDestinatario");
				try {
					final List<EmailDestinatario> listaEmails =  loadEmailsDestinatario(grupoDestinatario.getGrupoDestinatarioId(), request);
				} catch (SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				
			return false;
		}
		
		/**
		 * Carrega a lista de emails dos destinarios
		 */
		private List<EmailDestinatario> loadEmailsDestinatario(final long grupoDestinatarioId , final PortletRequest req) throws SystemException {
			
			List<EmailDestinatario> emails = new ArrayList<EmailDestinatario>();
			
			String [] listaDestinatarios = getEmails(req);
			
			if (listaDestinatarios != null){
				
				for (String email : listaDestinatarios){
					
					EmailDestinatario emailDestinatario = EmailDestinatarioLocalServiceUtil.
							createEmailDestinatario(CounterLocalServiceUtil.increment());
					
					emailDestinatario.setGrupoDestinatarioId(grupoDestinatarioId);			
					emailDestinatario.setEmail(email);
					
					emails.add(emailDestinatario);
					
				}
			}

			return emails;
		}
		
		private String [] getEmails(final PortletRequest req){
			
			if (Validator.isNotNull(ParamUtil.getString(req, "emailDestinatariosPrimaryKeys"))){
				return ParamUtil.getParameterValues(req, "emailDestinatariosPrimaryKeys");
			}
			return null;
		}
}
