﻿#region Copyright

/* -----------------------------------------------------------------------------
 *  Copyright © 2024 • Chomp Studios • All Rights Reserved
 * -----------------------------------------------------------------------------
 *  Date: 30/10/2024
 *  -----------------------------------------------------------------------------
 *  Description: Houses functions for Files
 *  -----------------------------------------------------------------------------
 */

#endregion

#if UNITY_EDITOR

#region Imports

using System.IO;
using UnityEditor;

#endregion

namespace Workflow_Toolkit.Utilities {
    public class FileElements : EditorWindow {
        /// <summary>
        ///     This method gets a shortened down directory list to display.
        /// </summary>
        /// <param name="fullPath">The Absolute path of the folder.</param>
        /// <returns>the last two folders in the absolute path</returns>
        public string GetLastFolders(string fullPath) {
            if (string.IsNullOrEmpty(fullPath)) return "None Selected";
            var folders = fullPath.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
            return folders.Length < 2 ? fullPath : Path.Combine(folders[^2], folders[^1]);
        }

        /// <summary>
        ///     Opens a folder selection dialog, allowing the user to choose a directory.
        /// </summary>
        /// <param name="title">The title displayed at the top of the folder selection dialog</param>
        /// <param name="defaultPath">the root filepath that the folder selection dialog will display.</param>
        /// <returns>
        ///     The path of the selected directory. If the user cancels the selection or there is an error, returns the
        ///     <paramref name="defaultPath" />.
        /// </returns>
        public string SelectDirectory(string title, string defaultPath) {
            var selectedPath = EditorUtility.OpenFolderPanel(title, defaultPath, "");
            return !string.IsNullOrEmpty(selectedPath) ? selectedPath : defaultPath;
        }
    }
}
#endif