package org.breizhcamp.video.uploader.services;

import org.breizhcamp.video.uploader.dto.Event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * Handle directory and files operations
 */
@Service
public class FileSrv {

	@Value("${videos.dir:./videos}")
	private Path videosDir;

	@Autowired
	private EventSrv eventSrv;

	private DateTimeFormatter dayFormat = DateTimeFormatter.ofPattern("dd");
	private DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("HH-mm");

	public Path getVideosDir() {
		return videosDir.toAbsolutePath();
	}

	/**
	 * Create one directory for each event in videosDir.
	 */
	public void createDirs() throws IOException {
		Files.createDirectories(videosDir);
		List<Event> events = eventSrv.list();

		for (Event event : events) {
			Path dir = videosDir.resolve(buildDirName(event));
			if (!Files.exists(dir)) {
				Files.createDirectory(dir);
			}
		}
	}


	/**
	 * @return the directory name for a specific event
	 */
	private String buildDirName(Event event) {
		String name = event.getName().replaceAll("[\\\\/:*?\"<>|]", "-");

		return dayFormat.format(event.getEventStart()) + "." + event.getVenue() + "." + timeFormat.format(event.getEventStart())
				+ " - " + name + " - " + event.getId();
	}
}

@@ -1340,16 +1340,180 @@
 %22, %22-%22);
+%0A%09%09String speakers = event.getSpeakers().replaceAll(%22%5B%5C%5C%5C%5C/:*?%5C%22%3C%3E%7C%5D%22, %22-%22);%0A%09%09if (speakers.endsWith(%22, %22)) speakers = speakers.substring(0, speakers.length() - 2);
 %0A%0A%09%09retu
@@ -1651,16 +1651,35 @@
 name + %22
+ (%22 + speakers + %22)
  - %22 + e

add speaker name in video directory