//BSD License  
//DXGfx® - http://www.eteractions.com
//Copyright (c) 2005
//by Guillaume Randon
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software
//and associated documentation files (the "Software"), to deal in the Software without restriction,
//including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
//and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
//subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all copies or substantial
//portions of the Software.
//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.
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace DXGfxLib
{
    /// <summary>
    /// This helper class is not really meant to be instanciated but it's static methods are going to be very usefull for the
    /// kind of string manipulation we need in EterEngine. For example extract the extension of a fileName, struncate
    /// the base directory out of a full pathname and build relative path.
    /// Not a very exciting job but this has to be done.
    /// </summary>
    public class StringUtil
    {
        /// <summary>
        /// 
        /// </summary>
        public StringUtil()
        {
        }

        /// <summary>
        /// This method will return a string containing the extension of the fileName given in argument.
        /// </summary>
        /// <param name="fileName">the relative or full pathname from which we want to extract the
        /// extension.</param>
        /// <returns></returns>
        public static string GetExtension(string fileName)
        {
            // I know I am a paranoid man but I still prefer to check even if performances can be affected.
            // Several years as support engineer deeply affected the way I program (not that I don't have any
            // defect or bad habit, but I try to be cautious).
            if (fileName != null)
            {
                int lind = fileName.LastIndexOf(".");
                if (lind != -1)
                {
                    return fileName.Remove(0, lind);
                }
                else
                {
                    // if lind = -1 it means there was no . in the fileName so there is no
                    // extention to return.
                    return "";
                }
            }
            return null;
        }

        /// <summary>
        /// This static method return the name of an entity from a pathname to the file. Thus it returns only the file name with
        /// it's extension. Generally that the name which is used to reference afterward the entity through it's name.
        /// </summary>
        /// <param name="fileName">the pathName to extract the file name or the name from.</param>
        /// <returns></returns>
        public static string GetName(string fileName)
        {
            if (fileName != null)
            {
                int lind = fileName.LastIndexOf("\\");
                if (lind != -1)
                {
                    return fileName.Remove(0, lind + 1);
                }
                else
                {
                    // if lind = -1 it means there was no \ in the fileName so our fileName can already be considered
                    // only a name of a file or of an entity.
                    return fileName;
                }
            }
            return null;
        }

        /// <summary>
        /// This static method removes the string passed as first argument from the string passed as second argument.
        /// </summary>
        /// <param name="basePath">the string to remove from the other string.</param>
        /// <param name="fileName">the string to work on.</param>
        /// <returns></returns>
        public static string GetRelativePath(string basePath, string fileName)
        {
            if ((fileName != null) && (basePath != null))
            {
                int lind = fileName.IndexOf(basePath);
                if (lind != -1)
                {
                    return fileName.Remove(lind, basePath.Length);
                }
                else
                {
                    // if lind = -1 it means the basePath is not  in fileName so our fileName can already be considered
                    // a relative path. Hum not really so let's return null which means there was a problem!
                    return null;
                }
            }
            return null;
        }

        /// <summary>
        /// This static method removes the string passed as first argument from the string passed as second argument.
        /// </summary>
        /// <param name="basePath">the string to remove from the other string.</param>
        /// <param name="fileName">the string to work on.</param>
        /// <returns></returns>
        public static string GetRelativePathWithoutStartingSlash(string basePath, string fileName)
        {
            if ((fileName != null) && (basePath != null))
            {
                int lind = fileName.IndexOf(basePath);
                if (lind != -1)
                {
                    return fileName.Remove(lind, basePath.Length+1);
                }
                else
                {
                    // if lind = -1 it means the basePath is not  in fileName so our fileName can already be considered
                    // a relative path. Hum not really so let's return null which means there was a problem!
                    return null;
                }
            }
            return null;
        }

        /// <summary>
        /// This static method removes the string passed as first argument from the string passed as second argument.
        /// </summary>
        /// <param name="basePath">the string to remove from the other string.</param>
        /// <param name="fileName">the string to work on.</param>
        /// <returns></returns>
        public static string GetPath(string fileName)
        {
            if (fileName != null)
            {
                int lind = fileName.LastIndexOf("\\");
                if (lind != -1)
                {
                    return fileName.Remove(lind + 1, fileName.Length - (lind + 1));
                }
                else
                {
                    // if lind = -1 it means there is a problem somewhere.
                    return null;
                }
            }
            return null;
        }

        /// <summary>
        /// Same as GetPath except the return path does not contain any \\ at the end.
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetPathWithoutTrailingSlash(string fileName)
        {
            if (fileName != null)
            {
                int lind = fileName.LastIndexOf("\\");
                if (lind != -1)
                {
                    return fileName.Remove(lind, fileName.Length - (lind));
                }
                else
                {
                    // if lind = -1 it means there is a problem somewhere.
                    return null;
                }
            }
            return null;
        }

        /// <summary>
        /// This method will append a \ at the end of the provided string.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string AppendDirectorySeparator(string path)
        {
            return (string)(path + "\\");
        }

        /// <summary>
        /// This will return a name that we guaranty is not in the given hashtable.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="hash"></param>
        /// <returns></returns>
        public static string GetNewIndexedName(string name, Hashtable hash)
        {
            if (hash.Contains(name))
            {
                int index = 1;
                while (hash.Contains(name + index))
                    index++;

                return name + index;
            }
            else
                return name;
        }
    }
}
