using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
namespace MediaBrowser.Controller.IO
{
    /// 
    /// Class NativeMethods
    /// 
    [SuppressUnmanagedCodeSecurity]
    public static class NativeMethods
    {
        //declare the Netapi32 : NetServerEnum method import
        /// 
        /// Nets the server enum.
        /// 
        /// Name of the server.
        /// The dw level.
        /// The p buf.
        /// The dw pref max len.
        /// The dw entries read.
        /// The dw total entries.
        /// Type of the dw server.
        /// The domain.
        /// The dw resume handle.
        /// System.Int32.
        [DllImport("Netapi32", CharSet = CharSet.Auto, SetLastError = true),
        SuppressUnmanagedCodeSecurityAttribute]
        public static extern int NetServerEnum(
            string ServerName, // must be null
            int dwLevel,
            ref IntPtr pBuf,
            int dwPrefMaxLen,
            out int dwEntriesRead,
            out int dwTotalEntries,
            int dwServerType,
            string domain, // null for login domain
            out int dwResumeHandle
            );
        //declare the Netapi32 : NetApiBufferFree method import
        /// 
        /// Nets the API buffer free.
        /// 
        /// The p buf.
        /// System.Int32.
        [DllImport("Netapi32", SetLastError = true),
        SuppressUnmanagedCodeSecurityAttribute]
        public static extern int NetApiBufferFree(
            IntPtr pBuf);
        /// 
        /// The MA x_ PATH
        /// 
        public const int MAX_PATH = 260;
        /// 
        /// The MA x_ ALTERNATE
        /// 
        public const int MAX_ALTERNATE = 14;
        /// 
        /// The INVALI d_ HANDL e_ VALUE
        /// 
        public static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
        /// 
        /// The STG m_ READ
        /// 
        public const uint STGM_READ = 0;
        /// 
        /// Finds the first file ex.
        /// 
        /// Name of the lp file.
        /// The f info level id.
        /// The lp find file data.
        /// The f search op.
        /// The lp search filter.
        /// The dw additional flags.
        /// IntPtr.
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr FindFirstFileEx(string lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, out WIN32_FIND_DATA lpFindFileData, FINDEX_SEARCH_OPS fSearchOp, IntPtr lpSearchFilter, int dwAdditionalFlags);
        /// 
        /// Finds the first file.
        /// 
        /// Name of the file.
        /// The lp find file data.
        /// IntPtr.
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr FindFirstFile(string fileName, out WIN32_FIND_DATA lpFindFileData);
        /// 
        /// Finds the next file.
        /// 
        /// The h find file.
        /// The lp find file data.
        /// IntPtr.
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr FindNextFile(IntPtr hFindFile, out WIN32_FIND_DATA lpFindFileData);
        /// 
        /// Finds the close.
        /// 
        /// The h find file.
        /// true if XXXX, false otherwise
        [DllImport("kernel32")]
        public static extern bool FindClose(IntPtr hFindFile);
    }
    //create a _SERVER_INFO_100 STRUCTURE
    /// 
    /// Struct _SERVER_INFO_100
    /// 
    [StructLayout(LayoutKind.Sequential)]
    public struct _SERVER_INFO_100
    {
        /// 
        /// The sv100_platform_id
        /// 
        internal int sv100_platform_id;
        /// 
        /// The sv100_name
        /// 
        [MarshalAs(UnmanagedType.LPWStr)]
        internal string sv100_name;
    }
    /// 
    /// Class FindFirstFileExFlags
    /// 
    public class FindFirstFileExFlags
    {
        /// 
        /// The NONE
        /// 
        public const int NONE = 0;
        /// 
        /// Searches are case-sensitive.Searches are case-sensitive.
        /// 
        public const int FIND_FIRST_EX_CASE_SENSITIVE = 1;
        /// 
        /// Uses a larger buffer for directory queries, which can increase performance of the find operation.
        /// 
        public const int FIND_FIRST_EX_LARGE_FETCH = 2;
    }
    /// 
    /// Enum FINDEX_INFO_LEVELS
    /// 
    public enum FINDEX_INFO_LEVELS
    {
        /// 
        /// The FindFirstFileEx function retrieves a standard set of attribute information. The data is returned in a WIN32_FIND_DATA structure.
        /// 
        FindExInfoStandard = 0,
        /// 
        /// The FindFirstFileEx function does not query the short file name, improving overall enumeration speed. The data is returned in a WIN32_FIND_DATA structure, and the cAlternateFileName member is always a NULL string.
        /// 
        FindExInfoBasic = 1
    }
    /// 
    /// Enum FINDEX_SEARCH_OPS
    /// 
    public enum FINDEX_SEARCH_OPS
    {
        /// 
        /// The search for a file that matches a specified file name.
        /// The lpSearchFilter parameter of FindFirstFileEx must be NULL when this search operation is used.
        /// 
        FindExSearchNameMatch = 0,
        /// 
        /// The find ex search limit to directories
        /// 
        FindExSearchLimitToDirectories = 1,
        /// 
        /// This filtering type is not available.
        /// 
        FindExSearchLimitToDevices = 2
    }
    /// 
    /// Struct FILETIME
    /// 
    [StructLayout(LayoutKind.Sequential)]
    public struct FILETIME
    {
        /// 
        /// The dw low date time
        /// 
        public uint dwLowDateTime;
        /// 
        /// The dw high date time
        /// 
        public uint dwHighDateTime;
    }
    /// 
    /// Struct WIN32_FIND_DATA
    /// 
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct WIN32_FIND_DATA
    {
        /// 
        /// The dw file attributes
        /// 
        public FileAttributes dwFileAttributes;
        /// 
        /// The ft creation time
        /// 
        public FILETIME ftCreationTime;
        /// 
        /// The ft last access time
        /// 
        public FILETIME ftLastAccessTime;
        /// 
        /// The ft last write time
        /// 
        public FILETIME ftLastWriteTime;
        /// 
        /// The n file size high
        /// 
        public int nFileSizeHigh;
        /// 
        /// The n file size low
        /// 
        public int nFileSizeLow;
        /// 
        /// The dw reserved0
        /// 
        public int dwReserved0;
        /// 
        /// The dw reserved1
        /// 
        public int dwReserved1;
        /// 
        /// The c file name
        /// 
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_PATH)]
        public string cFileName;
        /// 
        /// This will always be null when FINDEX_INFO_LEVELS = basic
        /// 
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = NativeMethods.MAX_ALTERNATE)]
        public string cAlternate;
        /// 
        /// Gets a value indicating whether this instance is hidden.
        /// 
        /// true if this instance is hidden; otherwise, false.
        public bool IsHidden
        {
            get
            {
                return dwFileAttributes.HasFlag(FileAttributes.Hidden);
            }
        }
        /// 
        /// Gets a value indicating whether this instance is system file.
        /// 
        /// true if this instance is system file; otherwise, false.
        public bool IsSystemFile
        {
            get
            {
                return dwFileAttributes.HasFlag(FileAttributes.System);
            }
        }
        /// 
        /// Gets a value indicating whether this instance is directory.
        /// 
        /// true if this instance is directory; otherwise, false.
        public bool IsDirectory
        {
            get
            {
                return dwFileAttributes.HasFlag(FileAttributes.Directory);
            }
        }
        /// 
        /// Gets the creation time UTC.
        /// 
        /// The creation time UTC.
        public DateTime CreationTimeUtc
        {
            get
            {
                return ParseFileTime(ftCreationTime);
            }
        }
        /// 
        /// Gets the last access time UTC.
        /// 
        /// The last access time UTC.
        public DateTime LastAccessTimeUtc
        {
            get
            {
                return ParseFileTime(ftLastAccessTime);
            }
        }
        /// 
        /// Gets the last write time UTC.
        /// 
        /// The last write time UTC.
        public DateTime LastWriteTimeUtc
        {
            get
            {
                return ParseFileTime(ftLastWriteTime);
            }
        }
        /// 
        /// Parses the file time.
        /// 
        /// The filetime.
        /// DateTime.
        private DateTime ParseFileTime(FILETIME filetime)
        {
            long highBits = filetime.dwHighDateTime;
            highBits = highBits << 32;
            var val = highBits + (long) filetime.dwLowDateTime;
            if (val < 0L)
            {
                return DateTime.MinValue;
            }
            if (val > 2650467743999999999L)
            {
                return DateTime.MaxValue;
            }
            return DateTime.FromFileTimeUtc(val);
        }
        /// 
        /// Gets or sets the path.
        /// 
        /// The path.
        public string Path { get; set; }
        /// 
        /// Returns a  that represents this instance.
        /// 
        /// A  that represents this instance.
        public override string ToString()
        {
            return Path ?? string.Empty;
        }
    }
    /// 
    /// Enum SLGP_FLAGS
    /// 
    [Flags]
    public enum SLGP_FLAGS
    {
        /// 
        /// Retrieves the standard short (8.3 format) file name
        /// 
        SLGP_SHORTPATH = 0x1,
        /// 
        /// Retrieves the Universal Naming Convention (UNC) path name of the file
        /// 
        SLGP_UNCPRIORITY = 0x2,
        /// 
        /// Retrieves the raw path name. A raw path is something that might not exist and may include environment variables that need to be expanded
        /// 
        SLGP_RAWPATH = 0x4
    }
    /// 
    /// Enum SLR_FLAGS
    /// 
    [Flags]
    public enum SLR_FLAGS
    {
        /// 
        /// Do not display a dialog box if the link cannot be resolved. When SLR_NO_UI is set,
        /// the high-order word of fFlags can be set to a time-out value that specifies the
        /// maximum amount of time to be spent resolving the link. The function returns if the
        /// link cannot be resolved within the time-out duration. If the high-order word is set
        /// to zero, the time-out duration will be set to the default value of 3,000 milliseconds
        /// (3 seconds). To specify a value, set the high word of fFlags to the desired time-out
        /// duration, in milliseconds.
        /// 
        SLR_NO_UI = 0x1,
        /// 
        /// Obsolete and no longer used
        /// 
        SLR_ANY_MATCH = 0x2,
        /// 
        /// If the link object has changed, update its path and list of identifiers.
        /// If SLR_UPDATE is set, you do not need to call IPersistFile::IsDirty to determine
        /// whether or not the link object has changed.
        /// 
        SLR_UPDATE = 0x4,
        /// 
        /// Do not update the link information
        /// 
        SLR_NOUPDATE = 0x8,
        /// 
        /// Do not execute the search heuristics
        /// 
        SLR_NOSEARCH = 0x10,
        /// 
        /// Do not use distributed link tracking
        /// 
        SLR_NOTRACK = 0x20,
        /// 
        /// Disable distributed link tracking. By default, distributed link tracking tracks
        /// removable media across multiple devices based on the volume name. It also uses the
        /// Universal Naming Convention (UNC) path to track remote file systems whose drive letter
        /// has changed. Setting SLR_NOLINKINFO disables both types of tracking.
        /// 
        SLR_NOLINKINFO = 0x40,
        /// 
        /// Call the Microsoft Windows Installer
        /// 
        SLR_INVOKE_MSI = 0x80
    }
    /// 
    /// The IShellLink interface allows Shell links to be created, modified, and resolved
    /// 
    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")]
    public interface IShellLinkW
    {
        /// 
        /// Retrieves the path and file name of a Shell link object
        /// 
        /// The PSZ file.
        /// The CCH max path.
        /// The PFD.
        /// The f flags.
        void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out WIN32_FIND_DATA pfd, SLGP_FLAGS fFlags);
        /// 
        /// Retrieves the list of item identifiers for a Shell link object
        /// 
        /// The ppidl.
        void GetIDList(out IntPtr ppidl);
        /// 
        /// Sets the pointer to an item identifier list (PIDL) for a Shell link object.
        /// 
        /// The pidl.
        void SetIDList(IntPtr pidl);
        /// 
        /// Retrieves the description string for a Shell link object
        /// 
        /// Name of the PSZ.
        /// Name of the CCH max.
        void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
        /// 
        /// Sets the description for a Shell link object. The description can be any application-defined string
        /// 
        /// Name of the PSZ.
        void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
        /// 
        /// Retrieves the name of the working directory for a Shell link object
        /// 
        /// The PSZ dir.
        /// The CCH max path.
        void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
        /// 
        /// Sets the name of the working directory for a Shell link object
        /// 
        /// The PSZ dir.
        void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
        /// 
        /// Retrieves the command-line arguments associated with a Shell link object
        /// 
        /// The PSZ args.
        /// The CCH max path.
        void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
        /// 
        /// Sets the command-line arguments for a Shell link object
        /// 
        /// The PSZ args.
        void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
        /// 
        /// Retrieves the hot key for a Shell link object
        /// 
        /// The pw hotkey.
        void GetHotkey(out short pwHotkey);
        /// 
        /// Sets a hot key for a Shell link object
        /// 
        /// The w hotkey.
        void SetHotkey(short wHotkey);
        /// 
        /// Retrieves the show command for a Shell link object
        /// 
        /// The pi show CMD.
        void GetShowCmd(out int piShowCmd);
        /// 
        /// Sets the show command for a Shell link object. The show command sets the initial show state of the window.
        /// 
        /// The i show CMD.
        void SetShowCmd(int iShowCmd);
        /// 
        /// Retrieves the location (path and index) of the icon for a Shell link object
        /// 
        /// The PSZ icon path.
        /// The CCH icon path.
        /// The pi icon.
        void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath,
            int cchIconPath, out int piIcon);
        /// 
        /// Sets the location (path and index) of the icon for a Shell link object
        /// 
        /// The PSZ icon path.
        /// The i icon.
        void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
        /// 
        /// Sets the relative path to the Shell link object
        /// 
        /// The PSZ path rel.
        /// The dw reserved.
        void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
        /// 
        /// Attempts to find the target of a Shell link, even if it has been moved or renamed
        /// 
        /// The HWND.
        /// The f flags.
        void Resolve(IntPtr hwnd, SLR_FLAGS fFlags);
        /// 
        /// Sets the path and file name of a Shell link object
        /// 
        /// The PSZ file.
        void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
    }
    /// 
    /// Interface IPersist
    /// 
    [ComImport, Guid("0000010c-0000-0000-c000-000000000046"),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IPersist
    {
        /// 
        /// Gets the class ID.
        /// 
        /// The p class ID.
        [PreserveSig]
        void GetClassID(out Guid pClassID);
    }
    /// 
    /// Interface IPersistFile
    /// 
    [ComImport, Guid("0000010b-0000-0000-C000-000000000046"),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IPersistFile : IPersist
    {
        /// 
        /// Gets the class ID.
        /// 
        /// The p class ID.
        new void GetClassID(out Guid pClassID);
        /// 
        /// Determines whether this instance is dirty.
        /// 
        [PreserveSig]
        int IsDirty();
        /// 
        /// Loads the specified PSZ file name.
        /// 
        /// Name of the PSZ file.
        /// The dw mode.
        [PreserveSig]
        void Load([In, MarshalAs(UnmanagedType.LPWStr)]
            string pszFileName, uint dwMode);
        /// 
        /// Saves the specified PSZ file name.
        /// 
        /// Name of the PSZ file.
        /// if set to true [remember].
        [PreserveSig]
        void Save([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
            [In, MarshalAs(UnmanagedType.Bool)] bool remember);
        /// 
        /// Saves the completed.
        /// 
        /// Name of the PSZ file.
        [PreserveSig]
        void SaveCompleted([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName);
        /// 
        /// Gets the cur file.
        /// 
        /// Name of the PPSZ file.
        [PreserveSig]
        void GetCurFile([In, MarshalAs(UnmanagedType.LPWStr)] string ppszFileName);
    }
    // CLSID_ShellLink from ShlGuid.h 
    /// 
    /// Class ShellLink
    /// 
    [
        ComImport,
        Guid("00021401-0000-0000-C000-000000000046")
    ]
    public class ShellLink
    {
    }
}