/*
 * Copyright (C) 2005-2013 Alfresco Software Limited.
 *
 * This file is part of Alfresco
 *
 * Alfresco is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Alfresco is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco. If not, see .
 */
package org.alfresco.repo.activities.feed;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.domain.activities.ActivityFeedEntity;
import org.alfresco.service.cmr.activities.ActivityService;
import org.alfresco.service.cmr.admin.RepoAdminService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.TemplateService;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.service.namespace.NamespaceException;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.ModelUtil;
import org.alfresco.util.Pair;
import org.alfresco.util.PropertyCheck;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.springframework.beans.factory.ObjectFactory;
/**
 * 
 * @since 4.0
 *
 */
public abstract class AbstractUserNotifier implements UserNotifier
{
    protected static Log logger = LogFactory.getLog(FeedNotifier.class);
    protected ActivityService activityService;
    protected NamespaceService namespaceService;
    protected RepoAdminService repoAdminService;
    protected NodeService nodeService;
    protected SiteService siteService;
    protected ObjectFactory activitiesFeedModelBuilderFactory;
	public void setActivityService(ActivityService activityService)
	{
		this.activityService = activityService;
	}
	public void setNamespaceService(NamespaceService namespaceService)
	{
		this.namespaceService = namespaceService;
	}
	public void setRepoAdminService(RepoAdminService repoAdminService)
	{
		this.repoAdminService = repoAdminService;
	}
	public void setNodeService(NodeService nodeService)
	{
		this.nodeService = nodeService;
	}
	public void setSiteService(SiteService siteService)
	{
		this.siteService = siteService;
	}
	
	public void setActivitiesFeedModdelBuilderFactory(ObjectFactory activitivitesFeedModelBuilderFactory) 
	{
	    this.activitiesFeedModelBuilderFactory = activitivitesFeedModelBuilderFactory;    
	}
	
	/**
     * Perform basic checks to ensure that the necessary dependencies were injected.
     */
    protected void checkProperties()
    {
        PropertyCheck.mandatory(this, "activitiesFeedModdelBuilderFactory", activitiesFeedModelBuilderFactory);
        PropertyCheck.mandatory(this, "activityService", activityService);
        PropertyCheck.mandatory(this, "nodeService", nodeService);
        PropertyCheck.mandatory(this, "namespaceService", namespaceService);
        PropertyCheck.mandatory(this, "siteService", siteService);
    }
	protected abstract boolean skipUser(NodeRef personNodeRef);
	protected abstract Long getFeedId(NodeRef personNodeRef);
	protected abstract void notifyUser(NodeRef personNodeRef, String subjectLine, Object[] subjectParams, Map model, String templateNodeRef);
	
    private void addSiteName(String siteId, Map siteNames)
    {
        if (siteId == null)
        {
            return;
        }
        
        String siteName = siteNames.get(siteId);
        if (siteName == null)
        {
            SiteInfo site = siteService.getSite(siteId);
            if (site == null)
            {
                return;
            }
            
            String siteTitle = site.getTitle();
            if (siteTitle != null && siteTitle.length() > 0)
            {
                siteName = siteTitle;
            }
            else
            {
                siteName = siteId;
            }
            
            siteNames.put(siteId, siteName);
        }
    }
    
	public Pair notifyUser(final NodeRef personNodeRef, String subject, Object[] subjectParams, Map siteNames,
			String shareUrl, int repeatIntervalMins, String templateNodeRef)
	{
		Map personProps = nodeService.getProperties(personNodeRef);
		String feedUserId = (String)personProps.get(ContentModel.PROP_USERNAME);
		if (skipUser(personNodeRef))
		{
			// skip
			return null;
		}
		// where did we get up to ?
		Long feedDBID = getFeedId(personNodeRef);
		// own + others (note: template can be changed to filter out user's own activities if needed)
		if (logger.isDebugEnabled())
		{
		    logger.debug("Get user feed entries: " + feedUserId + ", " + feedDBID);
		}
		List feedEntries = activityService.getUserFeedEntries(feedUserId, null, false, false, null, null, feedDBID);
		
		if (feedEntries.size() > 0)
		{
			ActivitiesFeedModelBuilder modelBuilder;
            try
            {
                modelBuilder = activitiesFeedModelBuilderFactory.getObject();
            }
            catch (Exception error)
            {
                logger.warn("Unable to create model builder: " + error.getMessage());
                return null;
            }
			
			for (ActivityFeedEntity feedEntry : feedEntries)
			{
				try
				{
				    modelBuilder.addAcctivitiyFeedEntry(feedEntry);
				    String siteId = feedEntry.getSiteNetwork();
			        addSiteName(siteId, siteNames);
				}
				catch (JSONException je)
				{
					// skip this feed entry
					logger.warn("Skip feed entry for user ("+feedUserId+"): " + je.getMessage());
					continue;
				}
			}
			final int activityCount = modelBuilder.activityCount();
            if (activityCount > 0)
			{
			    Map model = modelBuilder.buildModel();
				
		        model.put("siteTitles", siteNames);
		        model.put("repeatIntervalMins", repeatIntervalMins);
		        model.put("feedItemsMax", activityService.getMaxFeedItems());
				// add Share info to model
				model.put(TemplateService.KEY_PRODUCT_NAME, ModelUtil.getProductName(repoAdminService));
				Map personPrefixProps = new HashMap(personProps.size());
				for (QName propQName : personProps.keySet())
				{
					try
					{
						String propPrefix = propQName.toPrefixString(namespaceService);
						personPrefixProps.put(propPrefix, personProps.get(propQName));
					}
					catch (NamespaceException ne)
					{
						// ignore properties that do not have a registered namespace
						logger.warn("Ignoring property '" + propQName + "' as it's namespace is not registered");
					}
				}
				model.put("personProps", personPrefixProps);
				// send
				notifyUser(personNodeRef, subject, subjectParams, model, templateNodeRef);
				return new Pair(activityCount, modelBuilder.getMaxFeedId());
			}
		}
		return null;
	}
}