package info.bukova.isspst.storage;

import info.bukova.isspst.data.SignedDocumentItem;

import java.io.File;
import java.util.UUID;

/**
 * @author Pepa Rokos
 */
public class ReportFileStorageImpl extends AbstractFileStorage<SignedDocumentItem> implements ReportFileStorage {

	private String rootPath;

	public void setRootPath(String rootPath) {
		this.rootPath = rootPath;
	}

	@Override
	public String getRootPath() {
		return this.rootPath;
	}

	private String getFilePath(String fileName) {
		return rootPath + "/" + fileName;
	}

	@Override
	public void saveFile(byte[] data, SignedDocumentItem signedDocumentItem) {
		String fileName = signedDocumentItem.getFileName();

		if (fileName == null) {
			fileName = UUID.randomUUID().toString() + ".pdf";
		}

		saveFileDataToPath(data, getFilePath(fileName));
		signedDocumentItem.setFileName(fileName);
	}

	@Override
	public void saveFile(File file, SignedDocumentItem fileId) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void removeFile(SignedDocumentItem signedDocumentItem) {
		removeFileByPath(getFilePath(signedDocumentItem.getFileName()));
	}

	@Override
	public void moveFile(String source, String destination) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void createDirectory(String dir) {
		throw new UnsupportedOperationException();
	}

	@Override
	public byte[] fileData(SignedDocumentItem signedDocumentItem) {
		return fileDataFromPath(getFilePath(signedDocumentItem.getFileName()));
	}

	@Override
	public File file(SignedDocumentItem signedDocumentItem) {
		return null;
	}

	@Override
	public boolean dirExists(String path) {
		return false;
	}

	@Override
	public String serverPath(SignedDocumentItem signedDocumentItem) {
		return null;
	}

}
