/*
 * $Id$
 *
 * SARL is an general-purpose agent programming language.
 * More details on http://www.sarl.io
 *
 * Copyright (C) 2014-2026 SARL.io, the original authors and main authors.
 *
 * 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 io.sarl.sre.janus.itests;

import static io.sarl.tests.api.tools.TestAssertions.assertContains;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.nio.file.Files;
import java.util.Arrays;

import org.arakhne.afc.vmutil.FileSystem;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import io.sarl.lang.core.SARLVersion;
import io.sarl.sre.janus.boot.Boot;
import io.sarl.tests.api.tools.TestAssertions;
import io.sarl.tests.api.tools.TestReflections;
import io.sarl.tests.api.tools.TestShell;

/**
 * @author $Author: sgalland$
 * @version $FullVersion$
 * @mavengroupid $GroupId$
 * @mavenartifactid $ArtifactId$
 */
@SuppressWarnings("all")
@DisplayName("Janus")
@Tag("janus")
@Tag("run")
public class JanusIT {
	
	private static File java;

	private static File sarlcCmd;
	
	private static File janusCmd;

	@BeforeAll
	public static void setUp() throws Exception {
		java = TestShell.findExecutableJava();
		sarlcCmd = TestShell.findExecutableJar("sarlc", "cli");
		janusCmd = TestShell.findExecutableJar("janus", "cli");
	}

	@Test
	@DisplayName("Generate help")
	public void runHelp() throws Exception {
		final var arguments = TestShell.mergeJarArguments(this.java.getAbsolutePath(), this.janusCmd.getAbsolutePath(), "--help");
		final var stdout = TestShell.run(arguments);
		TestAssertions.assertMultiContains(stdout,
				"Janus is an open-source multi-agent platform fully",
				"SARL " + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING,
				"Java " + SARLVersion.MINIMAL_JDK_VERSION_FOR_SARL_COMPILATION_ENVIRONMENT,
				"--classpath=path",
				"SRE_BOOT_ROOT_CONTEXT_BOOT_TYPE");
	}


	@Test
	@DisplayName("Run in JRE")
	@Disabled
	public void runSREInJRE() throws Exception {
		final var tmpDir = FileSystem.createTempDirectory("janustest", null);
		FileSystem.deleteOnExit(tmpDir);
		final var currentDir = new File(FileSystem.CURRENT_DIRECTORY).getAbsoluteFile();
		final var sourceCode = FileSystem.join(currentDir, "src", "it", "resources", "test03.sarl");
		final var srcDir = FileSystem.join(tmpDir, "src", "main", "sarl");
		final var srcFile = FileSystem.join(srcDir, "test03.sarl");
		final var clsDir = FileSystem.join(tmpDir, "target", "classes");
		srcFile.getParentFile().mkdirs();
		FileSystem.copy(sourceCode, srcFile);

		// Compile SARL to Java
		final var sarlcArguments = TestShell.mergeJarArguments(this.java.getAbsolutePath(),
				this.sarlcCmd.getAbsolutePath(), srcDir.getAbsolutePath());
		TestShell.run(sarlcArguments);

		final var clsFile = FileSystem.join(tmpDir,
				"target", "classes",
				"io", "sarl", "sre", "janus", "itests", "MyAgent.class");
		assertTrue(clsFile.exists(), "No generated byte-code file: " + clsFile.getPath());

		// Run Janus SRE
		final var janusArguments = new String[] {
				"-cp",
				clsDir.getAbsolutePath(),
				"io.sarl.sre.janus.itests.MyAgent"
		};
		final String stdout = TestReflections.runRun(Boot.class.getName(), null, janusArguments);
		assertContains("] Hello", stdout);
	}

	@Test
	@DisplayName("Run assembly")
	public void runSREInProcess() throws Exception {
		final var tmpDir = FileSystem.createTempDirectory("janustest", null);
		FileSystem.deleteOnExit(tmpDir);
		final var currentDir = new File(FileSystem.CURRENT_DIRECTORY).getAbsoluteFile();
		final var sourceCode = FileSystem.join(currentDir, "src", "it", "resources", "test03.sarl");
		final var srcDir = FileSystem.join(tmpDir, "src", "main", "sarl");
		final var srcFile = FileSystem.join(srcDir, "test03.sarl");
		final var clsDir = FileSystem.join(tmpDir, "target", "classes");
		srcFile.getParentFile().mkdirs();
		FileSystem.copy(sourceCode, srcFile);

		// Compile SARL to Java
		final String[] sarlcArguments = TestShell.mergeJarArguments(this.java.getAbsolutePath(),
				this.sarlcCmd.getAbsolutePath(), srcDir.getAbsolutePath());
		final var output = TestShell.run(sarlcArguments);

		final File clsFile = FileSystem.join(tmpDir,
				"target", "classes",
				"io", "sarl", "sre", "janus", "itests", "MyAgent.class");
		assertTrue(clsFile.exists(), "No generated byte-code file: " + clsFile.getPath() + "; Output:\n" + output);

		// Run Janus SRE
		final String[] janusArguments = TestShell.mergeJarArguments(
				this.java.getAbsolutePath(),
				this.janusCmd.getAbsolutePath(),
				"-cp",
				clsDir.getAbsolutePath(),
				"io.sarl.sre.janus.itests.MyAgent",
				tmpDir.getAbsolutePath());
		TestShell.runWithoutStreamOverriding(janusArguments);
		final File createdDir = new File(tmpDir, "agentexecuted.dir");
		assertTrue(createdDir.isDirectory(), "Agent was not successfully executed: Folder not found: " + createdDir.getAbsolutePath());
	}

}
