/*
 * $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.eclipse.examples.tests;

import static io.sarl.eclipse.examples.tests.utils.ExamplesTestUtils.DEFAULT_RELATIVE_PATH;
import static io.sarl.eclipse.examples.tests.utils.ExamplesTestUtils.assertFile;
import static io.sarl.eclipse.examples.tests.utils.ExamplesTestUtils.createProject;
import static io.sarl.eclipse.examples.tests.utils.ExamplesTestUtils.dynamicTests;
import static io.sarl.eclipse.examples.tests.utils.ExamplesTestUtils.getSourcePath;
import static io.sarl.eclipse.examples.tests.utils.ExamplesTestUtils.installFiles;
import static io.sarl.eclipse.examples.tests.utils.ExamplesTestUtils.readFileToOpenFromXml;
import static io.sarl.eclipse.examples.tests.utils.ExamplesTestUtils.readXmlNode;
import static io.sarl.eclipse.examples.wizard.XmlUtils.readXmlAttribute;
import static io.sarl.eclipse.examples.wizard.XmlUtils.readXmlContent;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.io.File;
import java.util.List;

import org.arakhne.afc.vmutil.FileSystem;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.TestFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

/** Class for testing the examples.
 *
 * @author $Author: sgalland$
 * @version $FullVersion$
 * @mavengroupid $GroupId$
 * @mavenartifactid $ArtifactId$
 */
@SuppressWarnings("all")
@DisplayName("Example \"open-file\" in Eclipse")
@Tag("examples")
public class ExampleFileToOpenTest {

	/** Replies the dynamics tests for examples's files to open in the editor.
	 *
	 * <p>In the file {@code plugin.xml} in the project {@code ../../plugins/io.sarl.eclipse.examples}, the
	 * definition of a file to be open automatically when creating the example in the IDE could be defined.
	 * Each example must have an extension point that specify the location of the file to open:
	 * <pre>{@code 
	 * &lt;extension point="org.eclipse.emf.common.ui.examples"&gt;
	 *    &lt;example wizardID="EXAMPLE_ID"&lt;
	 *      ...
	 *      &lt;fileToOpen location="PATH_TO_FILE_TO_BE_OPEN"/&gt;
	 *      ...
	 *    &lt;/example&gt;
	 * &lt;/extension&gt;
	 * }</pre>
	 *
	 *
	 * @return the dynamic tests.
	 * @throws Exception in case of error for recovering the example descriptions.
	 */
	@TestFactory
	@DisplayName("Definition of file to open")
	public List<DynamicTest> fileToOpenInEditor() throws Exception {
		return dynamicTests(example -> {
			final File projectRoot = createProject(); 
			final List<File> installedFiles = installFiles(example, projectRoot, false);
			final File pluginFile = new File(DEFAULT_RELATIVE_PATH, "plugin.xml");
			final Document document = readXmlContent(pluginFile);
			Node node = readXmlNode(document, "plugin");
			node = readFileToOpenFromXml(node, example.archive);
			assumeTrue(node != null);
			
			String locationStr = readXmlAttribute(node, "location");
			assertNotNull(locationStr);
			IPath location = Path.fromPortableString(locationStr);
		
			// Format 1: <project-name>/<source-folder>/<qualified-filename>
			IPath filePath = location.removeFirstSegments(1);
			File file = FileSystem.join(projectRoot, filePath.toFile());
			if (file == null || !file.exists()) {
				// Format 2: <source-folder>/<qualified-filename>
				file = FileSystem.join(projectRoot, location.toFile());
				if (file == null || !file.exists()) {
					// Format 3: <qualified-filename>
					final File folder = getSourcePath(projectRoot);
					file = FileSystem.join(folder, location.toFile());
					assertFile(file, projectRoot);
				}
			}
		});
	}

}
