/*
 * Copyright 2020 École des Mines de Saint-Étienne.
 *
 * 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 fr.emse.gitlab.saref.entities.git;

import java.util.Date;

import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.eclipse.jgit.lib.Ref;

import fr.emse.gitlab.saref.Constants;

/**
 *
 * @author maxime.lefrancois
 */
public abstract class Version {

	protected final Repository repository;
	protected final Ref ref;
	protected final Date issued;
	private Resource resource;
	private Version priorVersion;
	private Version nextVersion;
	
	public Version(Repository repository, Ref ref, Date issued) {
		this.repository = repository;
		this.ref = ref;
		this.issued = issued;
	}

	public final String getRepositoryName() {
		return repository.getName();
	}
	
	public Repository getRepository() {
		return repository;
	}

	public final Ref getRef() {
		return ref;
	}

	public final Date getIssued() {
		return issued;
	}

	public abstract String getUri();

	public abstract String getPrefix();

	public abstract String getVersionName();
	
	public final Resource getResource() {
		if(resource == null) {
			resource = ResourceFactory.createResource(getUri());
		}
		return resource;
	}
	
	@Override
	public String toString() {
		return String.format("%s/%s", getRepositoryName(), getVersionName());
	}

	protected final String getSuperUri() {
		if (getRepositoryName().equals("saref-core")) {
			return String.format("%score/", Constants.BASE);
		} else {
			return String.format("%s%s/", Constants.BASE, getRepositoryName());
		}
	}

	protected final  String getSuperPrefix() {
		if (getRepositoryName().equals("saref-core")) {
			return "saref";
		} else {
			String shortName = getRepositoryName().substring(5);
			return String.format("s%s", shortName);
		}
	}
	
	public void setPriorVersion(Version version) {
		this.priorVersion = version;
	}
	
	public Version getPriorVersion() {
		return priorVersion;
	}
	
	public void setNextVersion(Version version) {
		this.nextVersion = version;
	}
	
	public Version getNextVersion() {
		return nextVersion;
	}
	
}
