package fr.openwide.maven.artifact.notifier.web.application.artifact.component;

import java.util.regex.Pattern;

import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;

import fr.openwide.core.wicket.behavior.ClassAttributeAppender;
import fr.openwide.core.wicket.markup.html.panel.GenericPanel;
import fr.openwide.maven.artifact.notifier.web.application.common.component.LabelWithPlaceholder;

public class ArtifactVersionTagPanel extends GenericPanel<String> {

	private static final long serialVersionUID = -8022735891213157213L;
	
	private static final Pattern NON_FINAL_VERSION_PATTERN = Pattern.compile("^.*[\\.-](rc|RC|cr|beta|alpha|incubating).*$");

	private static final String CSS_FINAL_VERSION_TAG = "label label-info";
	
	private static final String CSS_NON_FINAL_VERSION_TAG = "label label-warning";
	
	public ArtifactVersionTagPanel(String id, IModel<? extends String> model) {
		this(id, model, true);
	}
	
	public ArtifactVersionTagPanel(String id, IModel<? extends String> model, boolean showPlaceholder) {
		super(id, model);
		IModel<String> cssClassModel = new LoadableDetachableModel<String>() {
			private static final long serialVersionUID = 1L;

			@Override
			protected String load() {
				String version = getModelObject();
				if (version != null && NON_FINAL_VERSION_PATTERN.matcher(version).matches()) {
					return CSS_NON_FINAL_VERSION_TAG;
				}
				return CSS_FINAL_VERSION_TAG;
			}
		};
		
		LabelWithPlaceholder latestVersionLabel = new LabelWithPlaceholder("latestVersion", model);
		latestVersionLabel.setHideIfEmpty(!showPlaceholder);
		latestVersionLabel.add(new ClassAttributeAppender(cssClassModel));
		add(latestVersionLabel);
	}
}

@@ -648,9 +648,8 @@
 le(%22
-%5E
 .*%5B%5C
@@ -660,11 +660,8 @@
 (rc%7C
-RC%7C
 cr%7Cb
@@ -687,10 +687,35 @@
 g).*
-$
 %22
+, Pattern.CASE_INSENSITIVE
 );%0A%0A

Use of flag CASE_INSENSITIVE for non-final version regex.