package eu.dnetlib.openaire.exporter.model.community;

import javax.validation.constraints.NotNull;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import eu.dnetlib.openaire.exporter.model.community.selectioncriteria.SelectionCriteria;
import io.swagger.v3.oas.annotations.media.Schema;

@JsonAutoDetect
public class CommunityContentprovider {

	@Schema(description = "OpenAIRE identifier for this content provider, if available", required = false)
	private String openaireId;

	@NotNull
	@Schema(description = "the community identifier this content provider belongs to", required = true)
	private String communityId;

	@NotNull
	@Schema(description = "identifies this content provider within the context it belongs to", required = true)
	private String id;

	@Schema(description = "content provider name", required = false)
	private String name;

	@NotNull
	@Schema(description = "content provider official name", required = true)
	private String officialname;

	// @NotNull
	@Schema(description = "content provider selection criteria", required = false)
	private SelectionCriteria selectioncriteria;

	public String getOpenaireId() {
		return openaireId;
	}

	public void setOpenaireId(final String openaireId) {
		this.openaireId = openaireId;
	}

	public String getCommunityId() {
		return communityId;
	}

	public void setCommunityId(final String communityId) {
		this.communityId = communityId;
	}

	public String getId() {
		return id;
	}

	public void setId(final String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(final String name) {
		this.name = name;
	}

	public String getOfficialname() {
		return officialname;
	}

	public void setOfficialname(final String officialname) {
		this.officialname = officialname;
	}

	public SelectionCriteria getSelectioncriteria() {

		return this.selectioncriteria;
	}

	public void setSelectioncriteria(final SelectionCriteria selectioncriteria) {
		this.selectioncriteria = selectioncriteria;

	}

	@Override
	public String toString() {
		return String.format("id %s, name %s, selection criteria %s", this.id, this.name, toJson());
	}

	public String toJson() {
		if (selectioncriteria == null) { return ""; }
		try {
			return new ObjectMapper().writeValueAsString(selectioncriteria);
		} catch (final JsonProcessingException e) {
			throw new RuntimeException(e);
		}
	}

	public String toXML() {
		if (selectioncriteria == null) { return ""; }
		return "<![CDATA[" + toJson() + "]]>";
	}
}
