// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

// Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.IO;

namespace Microsoft.Samples.CloudDrive
{
    internal static class CloudDriveUtilities
    {
        public enum PathType
        {
            Drive,
            Container,
            Item,
            Invalid
        }

        /// <summary>
        /// Storage Drive Path Separator is different than the file system path separator
        /// </summary>
        public const char PathSeparator = '/';

        /// <summary>
        /// Adapts the Drive path, making sure the correct path separator
        /// character is used.
        /// </summary>
        /// <param name="path">Path to convert to normalized form</param>
        /// <returns>Normalized path</returns>
        public static string NormalizePath(string path)
        {
            if (!String.IsNullOrEmpty(path))
            {
                return path.Replace(Path.DirectorySeparatorChar, PathSeparator);
            }
            return path;
        }

        /// <summary>
        /// Adapts the path, making sure the correct path separator
        /// character is used.
        /// </summary>
        /// <param name="path">Path to convert to Denormalized form</param>
        /// <returns>Denormalized path</returns>
        public static string DenormalizePath(string path)
        {
            if (!String.IsNullOrEmpty(path))
            {
                return path.Replace(PathSeparator, Path.DirectorySeparatorChar);
            }
            return path;
        } 

        /// <summary>
        /// Gets the folder path.
        /// </summary>
        /// <param name="folderStart">The folder start.</param>
        /// <param name="folderEnd">The folder end.</param>
        /// <returns></returns>
        public static string GetFolderPath(string folderStart, string folderEnd)
        {
            // Ensures that there's a single trailing path separator
            string folder = (folderStart + PathSeparator + folderEnd).TrimEnd(PathSeparator);
            if (folder.Length > 0)
                return folder + PathSeparator;
            return folder;
        }

        /// <summary>
        /// Gets the full path.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="itemName">The folder.</param>
        /// 
        /// <returns></returns>
        public static string GetFullPath(string container, string itemName)
        {
            return string.Format("{1}{0}{2}", PathSeparator, container, itemName).TrimEnd(PathSeparator);
        }

        /// <summary>
        /// Gets the name of the file name from BLOB.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static string GetFileNameFromBlobName(string name)
        {
            name = name.TrimEnd(PathSeparator);
            int slashIndex = name.LastIndexOf(PathSeparator);
            return slashIndex > 0 ? name.Substring(slashIndex + 1) : name;
        }

        /// <summary>
        /// Checks to make sure the given string is a drive
        /// </summary>
        /// <param name="path">Path to check (Must be normalized)</param>
        /// <returns>Whether the path is a drive letter</returns>
        public static bool PathIsDrive(string path, string root)
        {
            root = root ?? String.Empty;
            if (String.IsNullOrEmpty(path.Replace(root, ""))
                ||
                String.IsNullOrEmpty(path.Replace(root + CloudDriveUtilities.PathSeparator, ""))
                )
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Removes the drive letters from path
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static string RemoveDriveFromPath(string path, string root)
        {
            root = root ?? String.Empty;
            string result = path ?? String.Empty;
            if (result.StartsWith(root, StringComparison.Ordinal))
            {
                return result.Substring(root.Length).TrimStart(CloudDriveUtilities.PathSeparator);
            }
            return result.TrimStart(CloudDriveUtilities.PathSeparator);
        }

        /// <summary>
        /// Converts the full path to rooted path.
        /// </summary>
        /// <param name="fullPath">The full path.</param>
        /// <returns></returns>
        public static string ConvertFullPathToRootedPath(string fullPath, string root, string name)
        {
            root = root ?? String.Empty;
            string driveName = name + ':';
            if (fullPath.StartsWith(driveName,StringComparison.CurrentCultureIgnoreCase))
            {
                return root + fullPath.Substring(driveName.Length);
            }
            else
            {
                return fullPath;
            }
        }
    }

}