/*
 * +================================================================================+
 *      __  ___  ______________  __  __  ______________   ____
 *     / / / / |/ /  _/_  __/\ \/ / / / / /_  __/  _/ /  / __/
 *    / /_/ /    // /  / /    \  / / /_/ / / / _/ // /___\ \
 *    \____/_/|_/___/ /_/     /_/  \____/ /_/ /___/____/___/
 *
 * +================================================================================+
 *
 *    "THE BEER-WARE LICENSE"
 *    -----------------------
 *
 *    As long as you retain this notice you can do whatever you want with this
 *    stuff. However it is expressly forbidden to sell or commercially distribute
 *    Unity Utils or parts of it apart from your games.
 *    If you meet any of the authors some day, and you think this stuff is
 *    worth it, you can buy them a beer in return.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *    THE SOFTWARE.
 *
 * +================================================================================+
 *
 *    Authors:
 *    David Krummenacher (dave@timbookedtwo.com)
 *
 */

using UnityEngine;

namespace Utils.Helpers {

	/// <summary>
	/// Desktop platform helper.
	/// </summary>
	public abstract class DesktopHelper {

		/// <summary>
		/// Opens the folder in windows.
		/// </summary>
		/// <param name="path">The folder path.</param>
		private static void OpenFolderInWin(string path) {
			bool openInsidesOfFolder = false;

			// Make backslashes
			string winPath = path.Replace("/", "\\");

			// If path requested is a folder, automatically open insides of that folder
			if (System.IO.Directory.Exists(winPath)) openInsidesOfFolder = true;

			// Try to open
			try {
				System.Diagnostics.Process.Start("explorer.exe", (openInsidesOfFolder ? "/root," : "/select,") + winPath);
			} catch (System.ComponentModel.Win32Exception e) {
				// Silenty skip error
				e.HelpLink = "";
			}
		}

		/// <summary>
		/// Opens the folder in mac.
		/// </summary>
		/// <param name="path">The folder path.</param>
		private static void OpenFolderInMac(string path) {
			bool openInsidesOfFolder = false;
			
			// Make forward slashes
			string macPath = path.Replace("\\", "/");
			
			// If path requested is a folder, automatically open insides of that folder
			if (System.IO.Directory.Exists(macPath)) openInsidesOfFolder = true;
			
			// Fix path
			if (!macPath.StartsWith("\"")) macPath = "\"" + macPath;
			if (!macPath.EndsWith("\"")) macPath = macPath + "\"";
			
			string arguments = (openInsidesOfFolder ? "" : "-R ") + macPath;
			
			// Try to open
			try {
				System.Diagnostics.Process.Start("open", arguments);
			} catch (System.ComponentModel.Win32Exception e) {
				// Silenty skip error
				e.HelpLink = "";
			}
		}

		/// <summary>
		/// Opens a folder in the systems native file manager application.
		/// </summary>
		/// <param name="path">The folder path.</param>
		public static void OpenFolder(string path) {
			if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor) OpenFolderInWin(path);
			else if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor) OpenFolderInMac(path);
			else Debug.Log("Only windows and mac desktop platforms are implemented. " + Application.platform.ToString() + " not supported!");
		}
	}
}
