/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.server.events;

import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.ResourceBundle;

import org.bodington.server.BuildingServerException;
import org.bodington.server.realm.User;
import org.bodington.server.resources.Resource;
import org.bodington.util.IntStringEnum;

/**
 * Track events for the home container.
 * @author buckett
 */
public class HomeContainerEvent extends Event
{
    
    private static ResourceBundle resourceBundle = ResourceBundle
        .getBundle(HomeContainerEvent.class.getName());
    
    public static HomeContainerEventType SIGNUP = new HomeContainerEventType("event.signup");
    
    public static class HomeContainerEventType extends IntStringEnum
    {
        protected HomeContainerEventType(String name)
        {
            super(name, HomeContainerEventType.class);
        }
        
        public static HomeContainerEventType getMapping(int index)
        {
            return (HomeContainerEventType)getConst(index,HomeContainerEventType.class);
        }
        
        public static HomeContainerEventType getMapping(String name)
        {
            return (HomeContainerEventType)getConst(name, HomeContainerEventType.class);
        }
        
    }

    public HomeContainerEvent()
    {
        super();
    }
    
    /**
     * Create a new event.
     * @param resource The homecontainer in which this event occured.
     * @param active The user who created the event.
     * @param passive The user to which the event relates.
     * @param type The type of event.
     */
    public HomeContainerEvent(Resource resource, User active, User passive, HomeContainerEventType type)
    {
        resource_id = resource.getResourceId();
        active_user_id = active.getUserId();
        passive_user_id = passive.getUserId();
        event_code = type.getId();
        setImportance(Event.IMPORTANCE_MANAGEMENT_MIN);
    }
    
    public String messageTitle()
    {
        String params[] = {"Someone", "Someone"};
        return MessageFormat.format(resourceBundle
            .getString(HomeContainerEventType.getMapping(event_code).getName()),
            params);
    }
    
    public void printMessage(PrintWriter out, boolean html)
    {
        Object params[] = { "Someone", "Someone" };
        try
        {
            params[0] = getActiveUser();
            params[1] = getPassiveUser();
        }
        catch (BuildingServerException bse)
        {}
        out.write(MessageFormat.format(resourceBundle
            .getString(HomeContainerEventType.getMapping(event_code).getName()),
            params));
    }
    
    
}
