package org.jenkinsci.plugins.systemmessage.user;

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.kohsuke.stapler.DataBoundConstructor;

import hudson.Extension;
import hudson.model.User;
import hudson.model.UserProperty;
import hudson.model.UserPropertyDescriptor;
import jenkins.model.*;

/**
 * (invisible) storage of the system messages which the user has already read
 * @author eaglerainbow
 */
public class UserReadSystemMessagesUserProperty extends UserProperty {
	public final static transient Set<String> EMPTY_MESSAGE_SET = new HashSet<String>();
	
	private HashSet<String> readMessages;

	@DataBoundConstructor
	public UserReadSystemMessagesUserProperty(Set<String> readMessages) {
		this.readMessages = new HashSet<String>(readMessages);
	}
	
	/**
	 * retrieve the entire set of all message unique identifiers
	 * the user has already marked as read
	 * @return the set of unique id of messages the user has marked as read before
	 */
	public Set<String> getReadMessages() {
		return Collections.unmodifiableSet(readMessages);
	}

	/**
	 * checks a unique id of a system message, whether the user has already read it or not
	 * @param messageUid the unique id of the message, which shall be checked
	 * @return <code>true</code> if the message was read, <code>false</code> otherwise
	 */
	public boolean isMessageRead(String messageUid) {
		return this.readMessages.contains(messageUid);
	}

	/**
	 * marks a given message (identified by its unique id)
	 * as being read by the user.
	 * @param messageUid the unique id of the message which shall be marked as being read
	 */
	public void markMessageRead(String messageUid) {
		this.readMessages.add(messageUid);
	}
	
	/**
	 * clean up a given message (identified by its unique id)
	 * after the message's validity has expired (and therefore the data about
	 * having it read or not is not applicable anymore) 
	 * @param messageUid the unique id of the message which was just deleted before.
	 */
	public void cleanupMessageRead(String messageUid) {
		this.readMessages.remove(messageUid);
	}
	
	/**
	 * returns the instance of the user currently logged on
	 * @return the instance of UserReadSysteMessagesUserProperty of the user which is currently logged on 
	 */
	public static UserReadSystemMessagesUserProperty getInstanceOfCurrentUser() {
		User me = User.current();
		
		if (me == null) {
			// no user is logged on yet / anonymous user or security is not enabled in general
			return null;
		}
		
		UserReadSystemMessagesUserProperty ursmup = me.getProperty(UserReadSystemMessagesUserProperty.class);
		if (ursmup == null) {
			// User Property does not exist yet for the user
			ursmup = new UserReadSystemMessagesUserProperty(EMPTY_MESSAGE_SET);
			try {
				me.addProperty(ursmup);
				me.save();
			} catch (IOException e) {
				return null;
			}
		}
		
		return ursmup;
	}
}

@@ -79,39 +79,8 @@
 n;%0D%0A
-import java.util.Collections;%0D%0A
 impo
@@ -102,16 +102,16 @@
 shSet;%0D%0A
+
 import j
@@ -1069,36 +1069,8 @@
 urn 
-Collections.unmodifiableSet(
 read
@@ -1073,25 +1073,24 @@
 readMessages
-)
 ;%0D%0A%09%7D%0D%0A%0D%0A%09/*
@@ -2073,32 +2073,32 @@
  messageUid) %7B%0D%0A
-
 %09%09this.readMessa
@@ -2121,32 +2121,496 @@
 geUid);%0D%0A%09%7D%0D%0A%09%0D%0A
+%09@Extension%0D%0A%09public static class DescriptorImpl extends UserPropertyDescriptor %7B%0D%0A%0D%0A%09%09@Override%0D%0A%09%09public UserProperty newInstance(User user) %7B%0D%0A%09%09%09UserReadSystemMessagesUserProperty usrsmup = new UserReadSystemMessagesUserProperty(%0D%0A%09%09%09%09%09EMPTY_MESSAGE_SET%0D%0A%09%09%09%09);%0D%0A%09%09%09usrsmup.user = user;%0D%0A%09%09%09%0D%0A%09%09%09return usrsmup;%0D%0A%09%09%7D%0D%0A%0D%0A%09%09@Override%0D%0A%09%09public String getDisplayName() %7B%0D%0A%09%09%09return %22User Property storing already read System Messages of a user%22;%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%7D%0D%0A%09%0D%0A
 %09/**%0D%0A%09 * return

Revert "Removal of Descriptor of UserReadSystemMessagesUserProperties"