package estivate.core.eval.lang;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import estivate.core.ast.ReduceAST;
import estivate.core.ast.lang.TextReduceAST;
import estivate.core.eval.EstivateEvaluator;
import estivate.core.eval.ReduceASTEvaluator;
import estivate.core.eval.EstivateEvaluator.EvalContext;
import estivate.core.eval.ReduceASTEvaluator.Factory;
import estivate.core.eval.ReduceASTEvaluator.ReduceResult;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TextReduceEvaluator implements ReduceASTEvaluator {

	public ReduceResult eval(EvalContext context, ReduceAST reduce) {
		return eval(context, reduce, false);
	}

	public ReduceResult eval(EvalContext context, ReduceAST reduce, boolean isValueList) {
		TextReduceAST text = (TextReduceAST) reduce;

		Elements elements = context.getDom();
		
		Object value;
		
		if (isValueList) {
			List<String> list = new ArrayList<String>();
			if (text.isOwn()) {
				log.debug("using list owntext()");
				for (Element element : elements) {
					list.add(element.ownText());
				}
			} else {
				log.debug("using list text()");
				for (Element element : elements) {
					list.add(element.text());
				}
			}
			value = list;
		} else {
			if (elements.size() > 1) {
				log.warn(
						"'{}' text using first element. Consider fixing the select expression to get only one element.",
						context.getMemberName());
			}
			if (text.isOwn()) {
				log.debug("using simple owntext()");

				StringBuilder sb = new StringBuilder(50);
				for (Element element : elements) {
					if (sb.length() != 0)
						sb.append(" ");
					sb.append(element.ownText());
				}

				value = sb.toString();
			} else {
				log.debug("using simple text()");
				value = elements.text();
			}
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
//		String value;
//		if(text.isOwn()){
//
//			StringBuilder sb = new StringBuilder(50);
//			for (Element element : context.getDom()) {
//				if (sb.length() != 0)
//					sb.append(" ");
//				sb.append(element.ownText());
//			}
//
//			value = sb.toString();
//		}else{
//			value = context.getDom().text();
//		}

		return ReduceResult.builder().value(value).build();
	}

	public static estivate.core.eval.ReduceASTEvaluator.Factory factory = new Factory() {

		@Override
		public ReduceASTEvaluator expressionEvaluater(ReduceAST reduce) {
			if(reduce instanceof TextReduceAST){
				return new TextReduceEvaluator();
			}
			return super.expressionEvaluater(reduce);
		}
	};

}

@@ -272,32 +272,44 @@
 stivateEvaluator
+.EvalContext
 ;%0D%0Aimport estiva
@@ -343,181 +343,8 @@
 ator
-;%0D%0Aimport estivate.core.eval.EstivateEvaluator.EvalContext;%0D%0Aimport estivate.core.eval.ReduceASTEvaluator.Factory;%0D%0Aimport estivate.core.eval.ReduceASTEvaluator.ReduceResult
 ;%0D%0Ai
@@ -1720,404 +1720,8 @@
 %09%09%7D%0D
-%0A%09%09%0D%0A%09%09%0D%0A%09%09%0D%0A%09%09%0D%0A%09%09%0D%0A%09%09%0D%0A%09%09%0D%0A%09%09%0D%0A%09%09%0D%0A%09%09%0D%0A%09%09%0D%0A%09%09%0D%0A%09%09%0D%0A%09%09%0D%0A%09%09%0D%0A%09%09%0D%0A//%09%09String value;%0D%0A//%09%09if(text.isOwn())%7B%0D%0A//%0D%0A//%09%09%09StringBuilder sb = new StringBuilder(50);%0D%0A//%09%09%09for (Element element : context.getDom()) %7B%0D%0A//%09%09%09%09if (sb.length() != 0)%0D%0A//%09%09%09%09%09sb.append(%22 %22);%0D%0A//%09%09%09%09sb.append(element.ownText());%0D%0A//%09%09%09%7D%0D%0A//%0D%0A//%09%09%09value = sb.toString();%0D%0A//%09%09%7Delse%7B%0D%0A//%09%09%09value = context.getDom().text();%0D%0A//%09%09%7D%0D
 %0A%0D%0A%09

clean code