package btheu.jsoupmapper.core;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;

import org.jsoup.select.Elements;

import btheu.jsoupmapper.Attr;
import btheu.jsoupmapper.Select;
import btheu.jsoupmapper.TagName;
import btheu.jsoupmapper.Text;
import btheu.jsoupmapper.Val;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author Benoit Theunissen
 *
 */
@Slf4j
public abstract class SelectEvaluator {

	public static Elements select(SelectBean bean, Elements elementsIn, AnnotatedElement member) {

		log.debug("select with '{}' for '{}'", bean.getName(), ClassUtils.getName(member));
		log.trace("select in  '{}'", elementsIn.toString());

		Elements elementsOut = elementsIn.select(bean.getSelect());

		log.trace("select out '{}'", elementsOut.toString());

		return elementsOut;
	}


	public static Elements select(Text annotation, Elements elements, AnnotatedElement member) {
		return select(new SelectBean(annotation),elements, member);
	}
	public static Elements select(Select annotation, Elements elements, AnnotatedElement member) {
		return select(new SelectBean(annotation),elements, member);
	}
	public static Elements select(Attr annotation, Elements elements, AnnotatedElement member) {
		return select(new SelectBean(annotation),elements, member);
	}
	public static Elements select(TagName annotation, Elements elements, AnnotatedElement member) {
		return select(new SelectBean(annotation),elements, member);
	}
	public static Elements select(Val annotation, Elements elements, AnnotatedElement member) {
		return select(new SelectBean(annotation),elements, member);
	}



	@Data
	public static class SelectBean {

		/**
		 * Hold the name of the original annotation
		 */
		protected String name;

		protected String select;

		protected int index;

		protected boolean unique;

		protected boolean first;

		protected boolean last;

		protected boolean optional;

		public SelectBean(Select annotation) {
			setAnnotation(annotation);
			this.select = annotation.select();
			this.index = annotation.index();
			this.unique = annotation.unique();
			this.first = annotation.first();
			this.last = annotation.last();
			this.optional = annotation.optional();
		}

		public SelectBean(Text annotation) {
			setAnnotation(annotation);
			this.select = annotation.select();
			this.index = annotation.index();
			this.unique = annotation.unique();
			this.first = annotation.first();
			this.last = annotation.last();
			this.optional = annotation.optional();
		}

		public SelectBean(Attr annotation) {
			setAnnotation(annotation);
			this.select = annotation.select();
			this.index = annotation.index();
			this.unique = annotation.unique();
			this.first = annotation.first();
			this.last = annotation.last();
			this.optional = annotation.optional();
		}

		public SelectBean(TagName annotation) {
			setAnnotation(annotation);
			this.select = annotation.select();
			this.index = annotation.index();
			this.unique = annotation.unique();
			this.first = annotation.first();
			this.last = annotation.last();
			this.optional = annotation.optional();
		}

		public SelectBean(Val annotation) {
			setAnnotation(annotation);
			this.select = annotation.select();
			this.index = annotation.index();
			this.unique = annotation.unique();
			this.first = annotation.first();
			this.last = annotation.last();
			this.optional = annotation.optional();
		}

		protected void setAnnotation(Annotation annotation) {
			this.name = annotation.annotationType().getSimpleName();
		}

	}


}

@@ -111,24 +111,61 @@
 Element;%0D%0A%0D%0A
+import org.jsoup.helper.StringUtil;%0D%0A
 import org.j
@@ -2124,32 +2124,55 @@
 %09%09%09this.select =
+ or(annotation.value(),
  annotation.sele
@@ -2167,32 +2167,33 @@
 otation.select()
+)
 ;%0D%0A%09%09%09this.index
@@ -3736,16 +3736,131 @@
 %0A%09%09%7D%0D%0A%0D%0A
+%09%09private String or(String value1, String value2) %7B%0D%0A%09%09%09return StringUtil.isBlank(value1) ? value2 : value1;%0D%0A%09%09%7D%0D%0A
 %09%7D%0D%0A%0D%0A%0D%0A

fix @select value() evaluation