/*Copyright (c) 2016 "hbz"

This file is part of zettel.

etikett is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package models;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.typesafe.config.ConfigFactory;
import play.data.validation.ValidationError;
import services.ZettelFields;
import services.ZettelModel;

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

/**
 * @author Jan Schnasse
 *
 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Article extends ZettelModel {

	/**
	 * The id under which this model is registered in the ZettelRegister
	 */
	public final static String id = "katalog:article";

	@Override
	protected String getType() {
		return "http://hbz-nrw.de/regal#Article";
	}

	/**
	 * The help text url must provide help texts in a certain html form. For each
	 * field a <div id="fieldName"> must be provided to contain the help text for
	 * the corresponding field.
	 * 
	 * @return return a url with help texts for the form
	 */
	public static String getHelpTextUrl() {
		String url = ConfigFactory.load().getString("zettel.article.helpText");
		return url;
	}

	@Override
	public List<ValidationError> validate() {
		List<ValidationError> errors = new ArrayList<>();
		validateStatus(errors);
		validateAuthorship(errors);
		validateSimpleFields(errors);
		validateListFields(errors);
		return errors.isEmpty() ? null : errors;
	}

	private void validateStatus(List<ValidationError> errors) {
		addErrorMessage("publicationStatus",
				String.format("Bitte vergeben Sie einen %s!", ZettelFields.publicationStatusZF.getLabel()),
				() -> getTitle(), errors);
		// reviewStatus is optional
	}

	private void validateListFields(List<ValidationError> errors) {
		if (containsNothing(getDdc())) {
			setDdc(new ArrayList<>());
			errors.add(new ValidationError("ddc",
					"Bitte orden Sie Ihre Daten einem Dewey Schlagwort zu!"));
		}
	}

	private void validateSimpleFields(List<ValidationError> errors) {
		addErrorMessage("title", "Bitte vergeben Sie einen Titel!",
				() -> getTitle(), errors);
		addErrorMessage("license", "Bitte vergeben Sie eine Lizenz!",
				() -> getLicense(), errors);
		addErrorMessage("professionalGroup",
				"Bitte orden Sie Ihre Daten einer Fachgruppe zu!",
				() -> getProfessionalGroup(), errors);
		addErrorMessage("language",
				"Welche Sprache passt am ehesten zu Ihrer Eingabe?",
				() -> getLanguage(), errors);
		addErrorMessage("medium", "Bitte ordnen Sie ihre Eingabe einem Medium zu!",
				() -> getMedium(), errors);
	}

	private void validateAuthorship(List<ValidationError> errors) {
		if (containsNothing(getCreator())) {
			setCreator(new ArrayList<>());
		}
		if (containsNothing(getContributor())) {
			setContributor(new ArrayList<>());
		}
		if (containsNothing(getCreatorName())) {
			setCreatorName(new ArrayList<>());
		}
		if (containsNothing(getContributorName())) {
			setContributorName(new ArrayList<>());
		}
		if (getCreator().isEmpty() && getContributor().isEmpty()
				&& getCreatorName().isEmpty() && getContributorName().isEmpty()) {
			errors.add(new ValidationError("creator",
					"Bitte geben Sie einen Autor oder Beteiligten an!"));
			errors.add(new ValidationError("contributor",
					"Bitte geben Sie einen Autor oder Beteiligten an!"));
			errors.add(new ValidationError("creatorName",
					"Bitte geben Sie einen Autor oder Beteiligten an!"));
			errors.add(new ValidationError("contributorName",
					"Bitte geben Sie einen Autor oder Beteiligten an!"));
		}
	}
}
@@ -1809,32 +1809,57 @@
 Status(errors);%0A
+%09%09validateTitle(errors);%0A
 %09%09validateAuthor
@@ -2182,37 +2182,49 @@
 ),%0A%09%09%09%09() -%3E get
-Title
+PublicationStatus
 (), errors);%0A%09%09/
@@ -2250,24 +2250,264 @@
 ptional%0A%09%7D%0A%0A
+%09private void validateTitle(List%3CValidationError%3E errors) %7B%0A%09%09addErrorMessage(%22title%22,%0A%09%09%09%09String.format(%22Bitte vergeben Sie einen %25s!%22, ZettelFields.titleZF.getLabel()),%0A%09%09%09%09() -%3E getTitle(), errors);%0A%09%09// alternativeTitle is optional%0A%09%7D%0A%0A
 %09private voi
@@ -2808,101 +2808,8 @@
 ) %7B%0A
-%09%09addErrorMessage(%22title%22, %22Bitte vergeben Sie einen Titel!%22,%0A%09%09%09%09() -%3E getTitle(), errors);%0A
 %09%09ad

Refactor title validation for Article