/*
 * 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.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

import fr.emse.gitlab.saref.Constants;

/**
 *
 * @author maxime.lefrancois
 */
public class Repository {

	private final File directory;
	private final String name;
	private List<Version> versions;
	private String currentBranch;
	private transient Resource resource;

	public Repository(File directory, String name) {
		this.directory = directory;
		this.name = name;
		this.versions = new ArrayList<>();
	}
	
	public Repository(File directory) {
		this(directory, directory.getName());
	}

	public void addBranchVersion(Ref ref, Date issued, String branchName) {
		versions.add(new BranchVersion(this, ref, issued, branchName));
	}

	public void addMasterVersion(Ref ref, Date issued) {
		versions.add(new MasterVersion(this, ref, issued));
	}
	
	public void addReleaseVersion(Ref ref, Date issued, int major, int minor, int patch) {
		versions.add(new ReleaseVersion(this, ref, issued, major, minor, patch));
	}
	
	public String getName() {
		return name;
	}

	public Resource getResource() {
		if (resource == null) {
			resource = ResourceFactory.createResource(getNamespace());
		}
		return resource;
	}

	public String getNamespace() {
		if (getName().equals("saref-core")) {
			return String.format("%score/", Constants.BASE);
		} else {
			return String.format("%s%s/", Constants.BASE, getName());
		}
	}

	public String getPrefix() {
		if (getName().equals("saref-core")) {
			return "saref:";
		} else {
			return String.format("s%s:", getName().substring(5));
		}
	}

	public File getDirectory() {
		return directory;
	}

	public List<Version> getVersions() {
		return versions;
	}

	public String getOntologyFileName() {
		if (getName().equals("saref-core")) {
			return "saref.ttl";
		} else {
			return String.format("%s.ttl", getName());
		}
	}
	
	public void setCurrentBranch(String currentBranch) {
		this.currentBranch = currentBranch;
	}
	
	public String getCurrentBranch() {
		return currentBranch;
	}

}