using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
namespace xmlTools
{
    /// 
    /// Utilities class. Contain function used along all the program and classes.
    /// 
    class Util
    {
        /// 
        /// Backup a file, return true if sucessful otherwise returns false
        /// 
        /// 
        static public bool backupFile(string file)
        {
            try
            {
                System.IO.File.Copy(file, file + ".bak", false); //make a backup first //false is to not override already created backups
            }
            catch (Exception e)
            {
                Program.printAppError(Program.appErrors.BACKUPS_ALREADY_EXISTS, "Couldn't backup file " + file + " :\n" + e.Message);
                return false;
            }
            return true;
        }
        /// 
        /// Get all elements with a determined name and optionally a parent name. Result is saved on the paremeter list "result"
        /// 
        /// 
        /// 
        /// 
        /// 
        public static void getAllSpecificElements(XmlNode rootNode, ref List result, String posElement, String posParentElement = "")
        {
            foreach (XmlNode element in rootNode.ChildNodes)
            {
                if (element.Name == posElement && (posParentElement == "" || posParentElement == element.ParentNode.Name))
                {
                    result.Add(element);
                    continue;
                }
                getAllSpecificElements(element, ref result, posElement, posParentElement); //If not found in this node continue search in subnodes
            }
        }
        /// 
        /// Converts a string to a xmlNode. Throws XmlException if the parsing of xml fails.
        /// 
        /// 
        /// 
        public static XmlNode stringToXmlNode(string xmlContent)
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.LoadXml(xmlContent);
            }
            catch (XmlException e)
            {
                throw e;
            }
            return doc.DocumentElement;
        }
        /// 
        /// Gets all xml files in the same directory of the executable
        /// 
        /// 
        static public List getAllXmlFiles()
        {
            return getXmlFilesWildcard("*.xml");
        }
        /// 
        /// Converts wildcard to regex and uses it to make the match
        /// 
        /// 
        /// 
        public static List getXmlFilesWildcard(String filewildcard)
        {
            List xmlFiles = new List();
            String dir = Path.GetDirectoryName(filewildcard); // Get the specified directory
            if (dir == "")
            {
                dir = Util.getExePath();
            }
            String wildcard = Path.GetFileName(filewildcard); // Get files/wildcard
            String[] files = System.IO.Directory.GetFiles(dir); //Get all files in specified directory
            foreach (String file in files)
            {
                Regex wildcardRegex = new Regex(Util.WildcardToRegex(wildcard), RegexOptions.IgnoreCase); //case insensitivity
                if (wildcardRegex.IsMatch(Path.GetFileName(file)))
                {
                    xmlFiles.Add(file);
                }
            }
            return xmlFiles;
        }
        /// 
        /// Converts wildcard to regex (from here: http://www.codeproject.com/Articles/11556/Converting-Wildcards-to-Regexes)
        /// 
        /// 
        /// 
        private static string WildcardToRegex(string pattern)
        {
            return "^" + Regex.Escape(pattern).
            Replace("\\*", ".*").
            Replace("\\?", ".") + "$";
        }
        /// 
        /// Check when a string contains a wildcard or not
        /// 
        /// 
        /// 
        public static bool containsWildcard(String myString)
        {
            return (myString.Contains("*") || myString.Contains("?"));
        }
        public static string getExePath()
        {
            return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        }
        public static string getExeFileName()
        {
            return Environment.GetCommandLineArgs()[0];
        }
        public static bool IsRunningOnMono()
        {
            return Type.GetType("Mono.Runtime") != null;
        }
        public static bool ContainsIgnoreCase(string source, string sToSearch)
        {
            return source.IndexOf(sToSearch, StringComparison.OrdinalIgnoreCase) >= 0;
        }
        // Thanks DarthDevilous for the regex! http://forums.thedailywtf.com/forums/t/2478.aspx
        public static string[] stringToArgsArray(string args)
        {
            MatchCollection ms = Regex.Matches(args, "([^\" ]*(\"[^\"]*\")[^\" ]*)|[^\" ]+");
            List listArgs=new List();
            foreach (Match m in ms)
            {
                listArgs.Add(m.Value.Replace("\"","")); //remove quotes or it will cause an error
            }
            return listArgs.ToArray();
        }
    }
}