/* ###
 * IP: GHIDRA
 *
 * 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 ghidra.util;

import java.util.Objects;

import ghidra.framework.OperatingSystem;
import ghidra.framework.Platform;
import ghidra.framework.options.CustomOption;
import ghidra.framework.options.GProperties;

public class ManualViewerCommandWrappedOption implements CustomOption {
	public static final String OPTIONS_CATEGORY_NAME = "Processor Manuals";
	public static final String MANUAL_VIEWER_OPTIONS = "Manual Viewer Options";

	static final String HTTP_URL_REPLACEMENT_STRING = "${HTTP_URL}";
	static final String FILE_URL_REPLACEMENT_STRING = "${FILE_URL}";
	static final String FILENAME_REPLACEMENT_STRING = "${FILENAME}";

	/**
	 * Token that may be used within the command arguments; it is replaced with the manual page
	 * number when the command is launched.
	 */
	static final String PAGE_REPLACEMENT_STRING = "${PAGE}";

	private static final String COMMAND_STRING = "commandString";
	private static final String COMMAND_ARGUMENTS = "commandArguments";
	private static final String URL_STRING = "urlReplacementString";

	private static final String DEFAULT_URL_REPLACEMENT_STRING = HTTP_URL_REPLACEMENT_STRING;

	private String commandString;
	private String[] commandArguments;
	private String fileFormat;

	public ManualViewerCommandWrappedOption() {
		// required for persistence
	}

	@Override
	public void readState(GProperties properties) {
		commandString = properties.getString(COMMAND_STRING, null);
		commandArguments = properties.getStrings(COMMAND_ARGUMENTS, null);
		fileFormat = properties.getString(URL_STRING, null);
	}

	@Override
	public void writeState(GProperties properties) {
		properties.putString(COMMAND_STRING, commandString);
		properties.putStrings(COMMAND_ARGUMENTS, commandArguments);
		properties.putString(URL_STRING, fileFormat);
	}

	@Override
	public int hashCode() {
		int hash = 0;

		hash += commandString == null ? 0 : commandString.hashCode();
		hash += fileFormat == null ? 0 : fileFormat.hashCode();

		if (commandArguments != null) {
			for (String arg : commandArguments) {
				hash += arg.hashCode();
			}
		}

		return hash;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof ManualViewerCommandWrappedOption)) {
			return false;
		}

		if (this == obj) {
			return true;
		}

		ManualViewerCommandWrappedOption otherOption = (ManualViewerCommandWrappedOption) obj;
		return Objects.equals(commandString, otherOption.commandString) &&
			Objects.equals(fileFormat, otherOption.fileFormat) &&
			Objects.deepEquals(commandArguments, otherOption.commandArguments);
	}

	public String getCommandString() {
		return commandString;
	}

	public void setCommandString(String commandString) {
		this.commandString = commandString;
	}

	public String[] getCommandArguments() {
		return commandArguments;
	}

	public void setCommandArguments(String[] commandArguments) {
		this.commandArguments = commandArguments;
	}

	public String getFileFormat() {
		return fileFormat;
	}

	public void setFileFormat(String newFormat) {
		this.fileFormat = newFormat;
	}

	public static ManualViewerCommandWrappedOption getDefaultBrowserLoaderOptions() {
		ManualViewerCommandWrappedOption option = new ManualViewerCommandWrappedOption();

		if (Platform.CURRENT_PLATFORM.getOperatingSystem() == OperatingSystem.WINDOWS) {
			option.setCommandString(System.getenv("ComSpec"));
			String[] args = new String[] { "/c", "start" };
			option.setCommandArguments(args);
			option.setFileFormat(DEFAULT_URL_REPLACEMENT_STRING);
		}
		else if (Platform.CURRENT_PLATFORM.getOperatingSystem() == OperatingSystem.LINUX) {
			option.setCommandString("google-chrome");
			option.setCommandArguments(new String[] {});
			option.setFileFormat(DEFAULT_URL_REPLACEMENT_STRING);
		}
		else if (Platform.CURRENT_PLATFORM.getOperatingSystem() == OperatingSystem.MAC_OS_X) {
			option.setCommandString("open");
			option.setCommandArguments(new String[] {});
			option.setFileFormat(DEFAULT_URL_REPLACEMENT_STRING);
		}
		else {
			option.setCommandString("");
			option.setCommandArguments(new String[] {});
			option.setFileFormat(DEFAULT_URL_REPLACEMENT_STRING);
		}

		return option;
	}
}
