/*
SPDX-FileCopyrightText: Copyright 2021 Siemens
SPDX-License: GPL-3.0-only
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.


 */

package javareporter;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;

public class Utils {


	static private boolean runningUnittest = false;

	static private String NXVersion = "";

	static public void ClearNXVersion()
	{
		NXVersion = "";
	}

	static private String NXVersionFromEnvPrint = "";

	final protected static int BLOCK_HEADER_SIZE = 11;
	final protected static int PADDING_SIZE = 510;
	final protected static int VERSION_SIZE = 2;
	final protected static int BLANK_PADDING = 0x20;


	static public void SetIsRunningUnittest(boolean isUnittest)
	{
		runningUnittest = isUnittest;
	}

	static public void Reset()
	{
		JavaBomReporter.Reset();
		NXOpenPrinter.Reset();
		lastErrorMessage = "";
	}

	static public boolean GetIsRunningUnittest()
	{
		return runningUnittest;
	}
	static private String lastErrorMessage = "";

	static public String GetLastErrorMessage()
	{
		return lastErrorMessage;
	}

	private static String ugii_base_dir;


	private static String RunEnvPrint(String offset, String arg)
	{

		String version = null;
		try {

			ProcessBuilder pb = new ProcessBuilder(ugii_base_dir + "\\" + offset + "\\env_print.exe",arg);
			pb.directory(new File(ugii_base_dir + "\\" + offset));
			pb.redirectErrorStream(true);

			Process p = pb.start();

			BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
			version = stdInput.readLine();

			p.destroy();
		} catch (IOException e) {
			e.getMessage();
		}
		return version;
	}


	private static String GetFromVersionFromEnvPrint()
	{
		//only want to do this once
		if (NXVersionFromEnvPrint.equals(""))
		{
			ugii_base_dir = System.getenv("UGII_BASE_DIR");

			if (ugii_base_dir == null)
			{
				Utils.Error("Environment variable UGII_BASE_DIR is not set.  Please set UGII_BASE_DIR to your NX install directory.\"");

			}

			String version = RunEnvPrint("ugopen", "NX_VERSION");

			if (version == null)
			{
				version = RunEnvPrint("nxbin", "NX_VERSION");
			}

			if (version == null)
			{
				version = RunEnvPrint("nxbin", "UGII_VERSION");
			}
			if (version != null)
			{
				version = version.replace("v", "");
				if (version.length() == 2)
				{
					//for NX12, CPPReporter expects 1200
					version = version + "00";
				}
			}
			NXVersionFromEnvPrint = version;
		}

		return NXVersionFromEnvPrint;
	}

	public static String GetFromVersion()
	{
		if (Utils.GetIsRunningUnittest())
		{
			//hardcode this for the unittest masters
			return "2007";
		}

		if (NXVersion == "")
		{
			return GetFromVersionFromEnvPrint();
		}
		return NXVersion;
	}


	static public void ReadNXOpenSigningResource(InputStream signingResourceInput) throws IOException, NoSuchAlgorithmException, InvalidKeyException, SignatureException
	{

		signingResourceInput.skip(BLOCK_HEADER_SIZE+VERSION_SIZE);
		byte[] b_message = new byte[PADDING_SIZE/2];
		signingResourceInput.read(b_message, 0, PADDING_SIZE/2);  

		String  messageString = new String(b_message);
		if (messageString.startsWith("NX "))
		{
			//get just the 4 digit version number
			NXVersion = messageString.substring(3, 7);
		}
		else
		{
			NXVersion = "";
		}
	}


    static public String GetBuildVersion()
    {
    	return ToolVersion.BUILDVERSION;
    }
    

    static public String GetToolVersion()
    {
    	return ToolVersion.TOOLVERSION;
    }

	static public void Error(String errorMessage)
	{
		System.out.println(errorMessage);
		lastErrorMessage = errorMessage;
		if (!runningUnittest)
		{
			System.exit(1);
		}
	}

}
