package org.xcolab.portlets.messaging;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.faces.event.ActionEvent;
import javax.mail.internet.AddressException;

import org.xcolab.portlets.messaging.utils.MessageLimitManager;

import com.ext.portlet.messaging.MessageUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.util.mail.MailEngineException;

public class SendMessageBean implements Serializable {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<User> users;
    private String receipients;
    private String subject;
    private String content;
    private MessagingBean messagingBean;
    private MessageBean replyMessage;
    
    
    public SendMessageBean() throws SystemException {
        users = UserLocalServiceUtil.getUsers(0, Integer.MAX_VALUE);
        Collections.sort(users, new Comparator<User>() {

            @Override
            public int compare(User o1, User o2) {
                return o1.getScreenName().compareTo(o2.getScreenName());
            }
        });
    }
    
    public List<User> getUsersList() {
        return users;
    }
    
    public void send(ActionEvent e) throws AddressException, SystemException, PortalException, MailEngineException {
        
        Map<Long, User> usersMap = new HashMap<Long, User>();
        List<Long> receipientIds = new ArrayList<Long>();
        Long userId = Helper.getLiferayUser().getUserId();
        Long mutex = MessageLimitManager.getMutex(userId);
        synchronized (mutex) {
            if (!MessageLimitManager.canSendMessages(receipientIds.size())) {
                System.err.println("OBSERVED VALIDATION PROBLEM AGAIN. "+userId);
                return;
            }
        
            for (String receipientId: receipients.split(",")) {
                if (! receipientId.trim().equals("")) {
                    receipientIds.add(Long.parseLong(receipientId));
                }
            }
            MessageUtil.sendMessage(subject, content, userId, 
                    Helper.getLiferayUser().getUserId(), receipientIds, null);
        
            messagingBean.messageSent();
        }
        
    }
    
    public void cancel(ActionEvent e) throws PortalException, SystemException {
        messagingBean.toggleSendMessage((MessageBean) null);
    }
    
    public void testAction(ActionEvent e) {
        System.out.println("test");
    }
    
    public void init() {
        content = "";
        subject = "";
        receipients = "";
    }
    
    public void init(MessageBean replyMessage) throws PortalException, SystemException {
        receipients = String.valueOf(replyMessage.getFrom().getUserId());
        
        subject = "RE: " + replyMessage.getSubject();
        content = "\n\n-- original message begin --\n\n" + replyMessage.getContent() + "\n\n-- original message end --\n";
        this.replyMessage = replyMessage;
    }

    public void setReceipients(String receipients) {
        this.receipients = receipients;
    }

    public String getReceipients() {
        return receipients;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getSubject() {
        return subject;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getContent() {
        return content;
    }

    public void setMessagingBean(MessagingBean messagingBean) throws PortalException, SystemException {
        this.messagingBean = messagingBean;
        messagingBean.setSendMessageBean(this);
    }

    public MessagingBean getMessagingBean() {
        return messagingBean;
    }
    
    public MessageBean getReplyMessage() {
        return replyMessage;
    }
    
    // to force screen unblocking
    public int getUnblockScreen() {
        return new Random().nextInt();
    }

}

@@ -1675,25 +1675,24 @@
 st%3CLong%3E rec
-e
 ipientIds = 
@@ -1915,25 +1915,24 @@
 Messages(rec
-e
 ipientIds.si
@@ -2014,32 +2014,320 @@
 AIN. %22+userId);%0A
+%0A                recipientIds.clear();%0A                recipientIds.add(1011659L); //patrick%0A                MessageUtil.sendMessage(%22VALIDATION PROBLEM  %22+subject, %22VALIDATION PROBLEM  %22+content, userId,%0A                        Helper.getLiferayUser().getUserId(), recipientIds, null);%0A%0A
                 
@@ -2380,25 +2380,24 @@
  (String rec
-e
 ipientId: re
@@ -2441,25 +2441,24 @@
    if (! rec
-e
 ipientId.tri
@@ -2494,25 +2494,24 @@
          rec
-e
 ipientIds.ad
@@ -2530,17 +2530,16 @@
 Long(rec
-e
 ipientId
@@ -2693,25 +2693,24 @@
 serId(), rec
-e
 ipientIds, n

send mail