using Microsoft.Win32;
using ryCommon;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace VSoft.Prams
{
   public static class API
    {
        // 关闭64位（文件系统）的操作转向
        [DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int Wow64DisableWow64FsRedirection(ref IntPtr ptr);
        // 开启64位（文件系统）的操作转向
        [DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
        public static Image GetImg(string url)
        {
            return GetImg(url, 128);
        }
        public static Image GetImg(string url,int maxSize)
        {
            var ext = System.IO.Path.GetExtension(url).ToLower();
            var img_ext = ";.jpg;.png;.bmp;.gif;.jpeg;.jfif;.jpe;.tif;.ico;";
            if (img_ext.IndexOfEx(";" + ext + ";") >= 0)
            {
                return RyFiles.LoadPicFromFile(url);
            }
            else
            {
                var exe_ext = ";.exe;.dll;";
                if (exe_ext.IndexOfEx(";" + ext + ";") >= 0)
                {
                    var img= RyFiles.GetFileIcon(url, 0, out _, maxSize);
                    if (img == null)
                    {
                        var icon = ShellIcon.GetLargeIcon(url);
                        return icon;
                    }
                    return img;
                }
                //FileIcon ico = new FileIcon(url);
                //ico.GetInfo();
                //var icon2 = ico.ShellIcon;
                var icon2 = ShellIcon.GetLargeIcon(url);
                return icon2;
            }
        }
        /// <summary>
        /// 读取一个快捷方式的信息
        /// </summary>
        /// <param name="lnkFilePath"></param>
        /// <returns></returns>
        public static ShortcutDescription ReadShortcut(string lnkFilePath)
        {
            var shellType = Type.GetTypeFromProgID("WScript.Shell");
            dynamic shell = Activator.CreateInstance(shellType);
            var shortcut = shell.CreateShortcut(lnkFilePath);
            string TargetPath = shortcut.TargetPath;
            if (TargetPath.StartsWith("C:\\Program Files (x86)", StringComparison.OrdinalIgnoreCase))
            {
                if (!System.IO.File.Exists(TargetPath) && !System.IO.Directory.Exists(TargetPath))
                {
                    TargetPath = TargetPath.Replace("C:\\Program Files (x86)\\", "C:\\Program Files\\");
                }
            }
            return new ShortcutDescription()
            {
                TargetPath = TargetPath,
                Arguments = shortcut.Arguments,
                WorkDir = shortcut.WorkingDirectory,
                IconLocation= shortcut.IconLocation
            };
        }
    }
}
