/*
 * Copyright 2014 Tagbangers, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.wallride.web.support;

import org.springframework.data.domain.Page;
import org.thymeleaf.context.IProcessingContext;
import org.wallride.domain.Article;
import org.wallride.model.ArticleSearchRequest;
import org.wallride.support.ArticleUtils;

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

public class Articles {

	private IProcessingContext processingContext;
	private ArticleUtils articleUtils;

	public Articles(IProcessingContext processingContext, ArticleUtils articleUtils) {
		this.processingContext = processingContext;
		this.articleUtils = articleUtils;
	}

	public List<Article> search(Condition condition) {
		Page<Article> result = articleUtils.search(condition.buildArticleSearchRequest(), condition.size);
		return new ArrayList<>(result.getContent());
	}

	public Condition condition() {
		return new Condition();
	}

	class Condition {

		private int size = 1;

		private String keyword;
		private Collection<Long> categoryIds;
		private Collection<String> categoryCodes;
		private Collection<String> tagNames;
		private Long authorId;
//		private Post.Status status;
//		private LocalDateTime dateFrom;
//		private LocalDateTime dateTo;

		public Condition size(int size) {
			this.size = size;
			return this;
		}

		public Condition keyword(String keyword) {
			this.keyword = keyword;
			return this;
		}

		public Condition category(Long... values) {
			List<Long> categoryIds = new ArrayList<>();
			for (Long value : values) {
				categoryIds.add(value);
			}
			this.categoryIds = categoryIds;
			return this;
		}

		public Condition category(String... values) {
			List<String> categoryCodes = new ArrayList<>();
			for (String value : values) {
				categoryCodes.add(value);
			}
			this.categoryCodes = categoryCodes;
			return this;
		}

		public Condition tag(String... values) {
			List<String> tagNames = new ArrayList<>();
			for (String value : values) {
				tagNames.add(value);
			}
			this.tagNames = tagNames;
			return this;
		}

		public Condition author(Long authorId) {
			this.authorId = authorId;
			return this;
		}

		private ArticleSearchRequest buildArticleSearchRequest() {
			ArticleSearchRequest request = new ArticleSearchRequest(processingContext.getContext().getLocale().getLanguage())
					.withKeyword(this.keyword)
					.withCategoryIds(this.categoryIds)
					.withCategoryCodes(this.categoryCodes)
					.withTagNames(this.tagNames)
					.withAuthorId(this.authorId);
			return request;
		}
	}
}
@@ -2001,29 +2001,26 @@
 ory(Long... 
-value
+id
 s) %7B%0A%09%09%09List
@@ -2072,37 +2072,34 @@
 r (Long value : 
-value
+id
 s) %7B%0A%09%09%09%09categor
@@ -2206,36 +2206,35 @@
 egory(String... 
-valu
+cod
 es) %7B%0A%09%09%09List%3CSt
@@ -2289,36 +2289,35 @@
 (String value : 
-valu
+cod
 es) %7B%0A%09%09%09%09catego
@@ -2434,20 +2434,19 @@
 ring... 
-valu
+nam
 es) %7B%0A%09%09
@@ -2512,20 +2512,19 @@
 value : 
-valu
+nam
 es) %7B%0A%09%09
@@ -2632,23 +2632,17 @@
 or(Long 
-authorI
+i
 d) %7B%0A%09%09%09
@@ -2657,23 +2657,17 @@
 horId = 
-authorI
+i
 d;%0A%09%09%09re

Fix Articles