package org.xcolab.portlets.userprofile;

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

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

import com.ext.portlet.messaging.MessageConstants;
import com.ext.portlet.messaging.MessageUtil;
import com.ext.portlet.model.Message;
import com.ext.portlet.model.MessagingUserPreferences;
import com.ext.portlet.service.ActivitySubscriptionLocalServiceUtil;
import com.ext.portlet.service.MessagingUserPreferencesLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
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.model.User;
import com.liferay.portal.security.auth.CompanyThreadLocal;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portlet.social.model.SocialActivity;
import com.liferay.util.mail.MailEngineException;

public class UserProfileBean {
    private UserWrapper currentUser; 
    private User wrappedUser;
    private final static String USER_ID_PARAM = "userId";
    private final static Log _log = LogFactoryUtil.getLog(UserProfileBean.class);
    private static final String PAGE_PARAM = "page";
    private static final String PAGE_PARAM_SUBSCRIPTIONS = "subscriptions";
    private static final String PAGE_PARAM_MANAGE_SUBSCRIPTIONS = "subscriptionsManage";
    private static final String PAGE_PARAM_EDIT_PROFILE = "edit";
    private boolean viewingOwnProfile = false;
    private boolean editing = false;
    private String messageText;
    private String messageSubject;
    private List<MessageBean> messages;
    private ArrayList<UserActivityBean> subscribedActivities;
    private PageType pageType = PageType.PROFILE_NOT_INITIALIZED;
    private UserSubscriptionsBean subscriptionsBean;
    private String messagingPortletId;
    private boolean messageSent;
    
    public UserProfileBean() {
        Map<String, String> parameters = Helper.getUrlParametersMap();
        long companyId = CompanyThreadLocal.getCompanyId(); 
        if (companyId == 0) {
            CompanyThreadLocal.setCompanyId(Helper.getThemeDisplay().getCompanyId());
        }
        
        if (parameters.containsKey(USER_ID_PARAM)) {
            try {
                Long userId = Long.parseLong(parameters.get(USER_ID_PARAM));
                wrappedUser = UserLocalServiceUtil.getUser(userId);
                currentUser = new UserWrapper(wrappedUser);
                pageType = PageType.PROFILE_DETAILS;
                subscriptionsBean = new UserSubscriptionsBean(wrappedUser);
            }
            catch (NumberFormatException e) {
                _log.error("Can't parse user id: " + parameters.get(USER_ID_PARAM), e);
            }
            catch (SystemException e) {
                _log.error("Can't load user with id " + parameters.get(USER_ID_PARAM), e);
            }
            catch (PortalException e) {
                _log.error("Can't load user with id " + parameters.get(USER_ID_PARAM), e);
            }
            
        }
        
        if (parameters.containsKey(PAGE_PARAM)) {
            if (PAGE_PARAM_SUBSCRIPTIONS.equals(parameters.get(PAGE_PARAM)))
                pageType = PageType.SUBSCRIPTIONS_VIEW;
            
        }
        
        
        if (wrappedUser != null && 
                Helper.isUserLoggedIn() && 
                wrappedUser.getUserId() == Helper.getLiferayUser().getUserId()) {
            // user is logged in and is viewing his own profile
            viewingOwnProfile = true;

            if (parameters.containsKey(PAGE_PARAM)) {
                if (PAGE_PARAM_MANAGE_SUBSCRIPTIONS.equals(parameters.get(PAGE_PARAM)))
                    pageType = PageType.SUBSCRIPTIONS_MANAGE;
                else if (PAGE_PARAM_EDIT_PROFILE.equals(parameters.get(PAGE_PARAM))) 
                    pageType = PageType.PROFILE_EDIT;
            }
            
        }
        
    }
    
    public boolean isInitialized() {
        return currentUser != null;
    }
    
    
    public UserWrapper getCurrentUser() {
        return currentUser;
    }

    public boolean isViewingOwnProfile() {
        return viewingOwnProfile;
    }

    public boolean isEditing() {
        return editing;
    }
    
    public void toggleEditing(ActionEvent e) throws PortalException, SystemException {
        editing = !editing;
        if (! editing) {
            currentUser.cancelChanges();
            pageType = PageType.PROFILE_DETAILS;
        }
        else {
            pageType = PageType.PROFILE_EDIT;
        }
    }
    
    public void updateUser(ActionEvent e) throws Exception {
        currentUser.persistChanges();
        editing = !editing;
        pageType = PageType.PROFILE_DETAILS;
    }
    
    public void setMessageText(String message) {
        this.messageText = message;
    }
    
    public String getMessageText() {
        return messageText;
    }
    
    public void setMessageSubject(String subject) {
        this.messageSubject = subject;   
    }
    
    public String getMessageSubject() {
        return messageSubject;
    }
    
    
    public void sendMessage(ActionEvent e) throws AddressException, SystemException, PortalException, MailEngineException {
        if (messageText != null && messageText.trim().length() > 0 && wrappedUser != null && Helper.isUserLoggedIn()) {
            //MessageUtil.sendMessage(subject, content, fromId, repliesTo, tousers, request)
            
            List<Long> recipients = new ArrayList<Long>();
            recipients.add(wrappedUser.getUserId());
            
            MessageUtil.sendMessage(messageSubject, messageText, Helper.getLiferayUser().getUserId(), 
                    Helper.getLiferayUser().getUserId(),recipients, null);
            messageSent = true;
        }
    }
    
    public boolean getMessageSent() {
        if (messageSent) {
            /* return message sent only once */
            messageSent = false;
            return true;
        }
        return messageSent;
    }
    
    public List<MessageBean> getMessages() throws SystemException, PortalException {
        if (messages == null) {
            messages = new ArrayList<MessageBean>();
            for (Message msg: MessageUtil.getMessages(currentUser.getUserId(), 0, 2, MessageConstants.INBOX)) {
                messages.add(new MessageBean(msg));
            }
        }
        return messages;
    }
    
    public List<UserActivityBean> getSubscribedActivities() throws SystemException, PortalException {
        if (subscribedActivities == null) {
            subscribedActivities = new ArrayList<UserActivityBean>();
            for (SocialActivity activity: ActivitySubscriptionLocalServiceUtil.getActivities(wrappedUser.getUserId(), 0, 200)) {
                subscribedActivities.add(new UserActivityBean(activity));
            }
            
        }
        return subscribedActivities;
    }
    
    public boolean getSendEmailOnMessage() throws SystemException {
        MessagingUserPreferences prefs = MessageUtil.getMessagingPreferences(Helper.getLiferayUser().getUserId());
        return prefs.getEmailOnReceipt();
    }
    
    public void setSendEmailOnMessage(boolean send) throws SystemException {
        MessagingUserPreferences prefs = MessageUtil.getMessagingPreferences(Helper.getLiferayUser().getUserId());
        prefs.setEmailOnReceipt(send);
        MessagingUserPreferencesLocalServiceUtil.updateMessagingUserPreferences(prefs);
    }
    
    public boolean getSendEmailOnActivity() throws SystemException {
        MessagingUserPreferences prefs = MessageUtil.getMessagingPreferences(Helper.getLiferayUser().getUserId());
        return prefs.getEmailOnActivity();
    }
    
    public void setSendEmailOnActivity(boolean send) throws SystemException {
        MessagingUserPreferences prefs = MessageUtil.getMessagingPreferences(Helper.getLiferayUser().getUserId());
        prefs.setEmailOnActivity(send);
        MessagingUserPreferencesLocalServiceUtil.updateMessagingUserPreferences(prefs);
    }
    
    public void showPage(ActionEvent e) {
        try {
            String name = String.valueOf(e.getComponent().getAttributes().get("name"));
            pageType = PageType.valueOf(name);
        }
        catch (Exception ex) {
            // ignore
        }
    }


    public PageType getPageType() {
        return pageType;
    }

    public void setMessagingPortletId(String messagingPortletId) {
        this.messagingPortletId = messagingPortletId;
    }

    public String getMessagingPortletId() {
        return messagingPortletId;
    }

    public UserSubscriptionsBean getSubscriptionsBean() {
        return subscriptionsBean;
    }

}

@@ -5985,151 +5985,8 @@
 ) %7B%0A
-        if (messageSent) %7B%0A            /* return message sent only once */%0A            messageSent = false;%0A            return true;%0A        %7D%0A
     

FIX : directly sending message from user profile: grey background not removed