package eu.dnetlib.domain.functionality;

import java.net.URL;

/**
 * This class represents a topic of the alert service.
 * @author thanos@di.uoa.gr
 * @see eu.dnetlib.api.functionality.AlertService
 * @see eu.dnetlib.functionality.alert.alerter.Alerter
 * @see AlertTemplate
 * @see AlertSubscription
 * 
 */
public class AlertTopic implements Comparable<AlertTopic> {
	private String templateId;
	private URL notificationService;
	private String queryId;
	private String resultId;
	private String name;
	private boolean enabled;
	
	/**
	 * Construct a new alert topic.
	 * @param templateId the unique identifier of the template used to format alerts of this topic
	 * @param notificationService the notification service generating results for this topic
	 * @param queryId the unique identifier of the query generating results for this topic
	 * @param resultId the unique identifier of the results of this topic
	 * @param name the name of the topic
	 * @param enabled true if this topic is currently enabled, false if it currently disabled
	 */
	public AlertTopic(final String templateId, final URL notificationService, final String queryId, final String resultId, final String name, final boolean enabled) {
		this.templateId = templateId;
		this.notificationService = notificationService;
		this.queryId = queryId;
		this.resultId = resultId;
		this.name = name;
		this.enabled = enabled;
	}
	
	/**
	 * Construct a new alert topic with template identifier, notification service, query identifier, result identifier and name set to null and enabled set to false.
	 */
	public AlertTopic() {
		this(null, null, null, null, null, false);
	}
	
	/**
	 * Get the template of this topic.
	 * @return the unique identifier of the template of this topic
	 */
	public String getTemplateId() {
		return templateId;
	}
	
	/**
	 * Set the template of this topic.
	 * @param templateId the unique identifier of the template of this topic
	 */
	public void setTemplateId(final String templateId) {
		this.templateId = templateId;
	}
	
	/**
	 * Get the notification service of this topic.
	 * @return the URL of the notification service of this topic 
	 */
	public URL getNotificationService() {
		return notificationService;
	}
	
	/**
	 * Set the notification service of this topic.
	 * @param notificationService the URL of the notification service of this topic
	 */
	public void setNotificationService(final URL notificationService) {
		this.notificationService = notificationService;
	}
	
	/**
	 * Get the notification query of this topic.
	 * @return the unique identifier of the notification query of this topic
	 */
	public String getQueryId() {
		return queryId;
	}
	
	/**
	 * Set the notification query of this topic. 
	 * @param queryId the unique identifier of the notification query of this topic
	 */
	public void setQueryId(final String queryId) {
		this.queryId = queryId;
	}
	
	/**
	 * Get the notification result of this topic.
	 * @return the unique identifier of the notification result of this topic
	 */
	public String getResultId() {
		return resultId;
	}
	
	/**
	 * Set the notification result of this topic.
	 * @param resultId the unique identifier of the notification result of this topic
	 */
	public void setResultId(final String resultId) {
		this.resultId = resultId;
	}
	
	/**
	 * Get the name of this topic.
	 * @return the name of this topic
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Set the name of this topic.
	 * @param name the name of this topic
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * Check if this topic is enabled.
	 * @return true if this topic is currently enabled, false otherwise
	 */
	public boolean isEnabled() {
		return enabled;
	}
	
	/**
	 * Set the enabled status of this topic.
	 * @param enabled true to enable this topic, false to disable it
	 */
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	@Override
	public int compareTo(final AlertTopic topic) {
		final int templateIdComparison = templateId.compareTo(topic.templateId);
		final int notificationServiceComparison = notificationService.toString().compareTo(topic.notificationService.toString());
		final int queryIdComparison = queryId.compareTo(topic.queryId);
		final int resultIdComparison = resultId.compareTo(topic.resultId);
		return (templateIdComparison == 0) ? ((notificationServiceComparison == 0) ? ((queryIdComparison == 0) ? resultIdComparison : queryIdComparison) : notificationServiceComparison) : templateIdComparison;
	}
	
	@Override
	public boolean equals(final Object object) {
		if (!(object instanceof AlertTopic))
			return false;
		AlertTopic topic = (AlertTopic) object;
		return ((templateId == null) ? (topic.templateId == null) : templateId.equals(topic.templateId)) &&
				((notificationService == null) ? (topic.notificationService == null) : notificationService.equals(topic.notificationService)) &&
				((queryId == null) ? (topic.queryId == null) : queryId.equals(topic.queryId)) &&
				((resultId == null) ? (topic.resultId == null) : resultId.equals(topic.resultId));
	}
	
	@Override
	public int hashCode() {
		return ((templateId == null) ? 0 : templateId.hashCode()) + ((notificationService == null) ? 0 : notificationService.hashCode()) + ((queryId == null) ? 0 : queryId.hashCode()) + ((resultId == null) ? 0 :
				resultId.hashCode());
	}
	
	@Override
	public String toString() {
		return "(template: " + templateId + ", notification service: " + notificationService + ", query: " + queryId + ", result: " + resultId + ")";
	}
}
