package org.jenkinsci.plugins.systemmessage.model;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.kohsuke.stapler.DataBoundConstructor;

import hudson.*;

/**
 * defines that the text in the system message panel shall be
 * based on a set of items which shall be rendered in a table
 * @author eaglerainbow
 *
 */
public class TableMessageTextStrategy extends MessageTextStrategy {
	
	private List<PlainMessageTextStrategy> messages;
	
	public TableMessageTextStrategy() {
		this.messages = new LinkedList<>();
	}
	
	@DataBoundConstructor
	public TableMessageTextStrategy(List<PlainMessageTextStrategy> messages) {
		this.messages = messages;
	}
	
	@Override
	public boolean isDisplayable() {
		/* We need to check the list of messages, which are provided,
		 * to see if there is something displayable
		 */
		
		if (this.messages == null)
			// there are simply no messages at (even not a list of it)
			return false;
		
		if (this.messages.isEmpty())
			// no messages have been configured -- there is nothing to show
			return false;
		
		for(PlainMessageTextStrategy item : this.messages) {
			if ((item.getMessageText() != null) 
					&& !("".equals(item.getMessageText().trim()))) {
				return true; // there is at least one thing to show
			}
		}
		
		// all messages do not have anything to show
		return false;
	}

	@Override
	public MessageLevel getPanelMessageLevel() {
		/* the "highest" message level wins 
		 * and thus determines how to show the entire message panel
		 */
		
		if (this.messages == null || this.messages.isEmpty()) {
			// apparently there is nothing to show; so why to be critical?
			return MessageLevel.INFORMATION;
		}
		
		MessageLevel maxLevel = MessageLevel.INFORMATION;
		for (PlainMessageTextStrategy item : this.messages) {
			if (item.getLevel().compareTo(maxLevel) > 0) 
				maxLevel = item.getLevel();
		}
		return maxLevel;
	}

	@Override
	public void updateOnConfigurationChange(MessageTextStrategy mtsBefore) {
		// Nothing to do so far...
	}
	
	public List<PlainMessageTextStrategy> getMessages() {
		return Collections.unmodifiableList(this.messages);
	}
	
	@Override
	public Set<String> getMessageUidsOnHideButton() {
		throw new Error("Not implemented yet");
	}

	
	@Extension(ordinal = 5000)
	public static class DescriptorImpl extends MessageTextStrategyDescriptor {
		@Override
		public String getDisplayName() {
			return Messages.MessageTextStrategy_MESSAGE_TEXT_TABLE();
		}
	
	}

}

@@ -153,24 +153,109 @@
 til.Set;%0D%0A%0D%0A
+import org.jenkinsci.plugins.systemmessage.user.UserReadSystemMessagesUserProperty;%0D%0A
 import org.k
@@ -2409,46 +2409,175 @@
 %0D%0A%09%09
-throw new Error(%22Not implemented yet%22)
+// TODO Unclear implementation: What shall the %22big hide%22 button do in case of TableMessageTextStrategy?%0D%0A%09%09return UserReadSystemMessagesUserProperty.EMPTY_MESSAGE_SET
 ;%0D%0A%09

Fix login issue - however, proper design still is unclear