/*
 * $Id$
 *
 * SARL is an general-purpose agent programming language.
 * More details on http://www.sarl.io
 *
 * Copyright (C) 2014-2021 the original authors or 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.examples.tests;

import static io.sarl.examples.tests.utils.ExamplesTestUtils.DEFAULT_RELATIVE_PATH;
import static io.sarl.examples.tests.utils.ExamplesTestUtils.assertFile;
import static io.sarl.examples.tests.utils.ExamplesTestUtils.createProject;
import static io.sarl.examples.tests.utils.ExamplesTestUtils.dynamicTests;
import static io.sarl.examples.tests.utils.ExamplesTestUtils.getSourcePath;
import static io.sarl.examples.tests.utils.ExamplesTestUtils.readFileToOpenFromXml;
import static io.sarl.examples.tests.utils.ExamplesTestUtils.readXmlNode;
import static io.sarl.examples.wizard.XmlUtils.readXmlAttribute;
import static io.sarl.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;

import io.sarl.examples.tests.utils.AbstractExampleTest;

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

	/** Replies the dynamics tests for examples's files to open in the editor.
	 *
	 * @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);
				}
			}
		});
	}

}
