// Path: core/src/main/java/se/tfiskgul/mux2fs/Constants.java
// public static final int MUX_WAIT_LOOP_MS = 500;
// 
// Path: core/src/main/java/se/tfiskgul/mux2fs/Constants.java
// public static final int SUCCESS = 0;
// 
// Path: core/src/main/java/se/tfiskgul/mux2fs/fs/base/Sleeper.java
// @FunctionalInterface
// public interface Sleeper {
// 
// 	void sleep(int millis)
// 			throws InterruptedException;
// }


import static se.tfiskgul.mux2fs.Constants.MUX_WAIT_LOOP_MS;
import static se.tfiskgul.mux2fs.Constants.SUCCESS;
import static se.tfiskgul.mux2fs.mux.Muxer.State.FAILED;
import static se.tfiskgul.mux2fs.mux.Muxer.State.NOT_STARTED;
import static se.tfiskgul.mux2fs.mux.Muxer.State.RUNNING;
import static se.tfiskgul.mux2fs.mux.Muxer.State.SUCCESSFUL;
import java.io.File;
import java.io.IOException;
import java.nio.file.AccessMode;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.annotations.VisibleForTesting;
import se.tfiskgul.mux2fs.fs.base.Sleeper;
/*
MIT License

Copyright (c) 2017 Carl-Frederik Hallberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
 */
package se.tfiskgul.mux2fs.mux;






/**
 * TODO: Add cancel()
 *
 * TODO: Support for multiple srtFiles
 */
public class Muxer {

	private static final Logger logger = LoggerFactory.getLogger(Muxer.class);
	private final Path mkv;
	private final Path srt;
	private final Path tempDir;
	private final Path output;
	private final AtomicReference<State> state = new AtomicReference<Muxer.State>(NOT_STARTED);
	private volatile Process process;
	private final ProcessBuilderFactory factory;
	private final Sleeper sleeper;

	public enum State {
		NOT_STARTED, RUNNING, SUCCESSFUL, FAILED
	}

	@FunctionalInterface
	public static interface ProcessBuilderFactory {
		ProcessBuilder from(String... command);
	}

	@FunctionalInterface
	public static interface MuxerFactory {
		Muxer from(Path mkv, Path srt, Path tempDir);

		static MuxerFactory defaultFactory() {
			return (mkv, srt, tempDir) -> Muxer.of(mkv, srt, tempDir);
		}
	}

	private Muxer(Path mkv, Path srt, Path tempDir, ProcessBuilderFactory factory, Sleeper sleeper) {
// Path: core/src/main/java/se/tfiskgul/mux2fs/Constants.java
// public static final int MUX_WAIT_LOOP_MS = 500;
// 
// Path: core/src/main/java/se/tfiskgul/mux2fs/Constants.java
// public static final int SUCCESS = 0;
// 
// Path: core/src/main/java/se/tfiskgul/mux2fs/fs/base/Sleeper.java
// @FunctionalInterface
// public interface Sleeper {
// 
// 	void sleep(int millis)
// 			throws InterruptedException;
// }


// Path: core/src/main/java/se/tfiskgul/mux2fs/mux/Muxer.java
import static se.tfiskgul.mux2fs.Constants.MUX_WAIT_LOOP_MS;
import static se.tfiskgul.mux2fs.Constants.SUCCESS;
import static se.tfiskgul.mux2fs.mux.Muxer.State.FAILED;
import static se.tfiskgul.mux2fs.mux.Muxer.State.NOT_STARTED;
import static se.tfiskgul.mux2fs.mux.Muxer.State.RUNNING;
import static se.tfiskgul.mux2fs.mux.Muxer.State.SUCCESSFUL;
import java.io.File;
import java.io.IOException;
import java.nio.file.AccessMode;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.annotations.VisibleForTesting;
import se.tfiskgul.mux2fs.fs.base.Sleeper;

/*
MIT License

Copyright (c) 2017 Carl-Frederik Hallberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
 */
package se.tfiskgul.mux2fs.mux;






/**
 * TODO: Add cancel()
 *
 * TODO: Support for multiple srtFiles
 */
public class Muxer {

	private static final Logger logger = LoggerFactory.getLogger(Muxer.class);
	private final Path mkv;
	private final Path srt;
	private final Path tempDir;
	private final Path output;
	private final AtomicReference<State> state = new AtomicReference<Muxer.State>(NOT_STARTED);
	private volatile Process process;
	private final ProcessBuilderFactory factory;
	private final Sleeper sleeper;

	public enum State {
		NOT_STARTED, RUNNING, SUCCESSFUL, FAILED
	}

	@FunctionalInterface
	public static interface ProcessBuilderFactory {
		ProcessBuilder from(String... command);
	}

	@FunctionalInterface
	public static interface MuxerFactory {
		Muxer from(Path mkv, Path srt, Path tempDir);

		static MuxerFactory defaultFactory() {
			return (mkv, srt, tempDir) -> Muxer.of(mkv, srt, tempDir);
		}
	}

	private Muxer(Path mkv, Path srt, Path tempDir, ProcessBuilderFactory factory, Sleeper sleeper) {
		this.mkv = mkv;