using CPF.Controls;
using CPF.Drawing;
//using System;
using System.Configuration;
using System.Text.RegularExpressions;

namespace OVCS.ClientLinux.Tools
{
    /// <summary>
    /// 表情、头像、聊天时间 格式渲染
    /// </summary>
    public static class CommonHelper
    {
        public const string ProcessName = "OVCS.ClientLinux";//项目名称
        public static string CompanyUrl = ConfigurationManager.AppSettings["CompanyUrl"];
        public static string SeparAtor = ESBasic.Helpers.FileHelper.GetFilePathSeparatorChar().ToString();
        public static string EmotionPath = $"{System.AppDomain.CurrentDomain.BaseDirectory}Emotion{CommonHelper.SeparAtor}";
        /// <summary>
        /// 图片扩展名
        /// </summary>
        public static string[] ImageExtensions = new string[] { "bmp", "jpeg", "png", "jpg", "gif" };

        /// <summary>
        /// 资源路径目录
        /// </summary>
        public const string ResourcesCatalog = "res://OVCS.ClientLinux/Resources/";

        ///// <summary>
        ///// 弹出系统提示框
        ///// </summary> 
        //public static void Show(string msg, string title = "系统提示")
        //{
        //    CPF.Threading.Dispatcher.MainThread.BeginInvoke(delegate
        //    {
        //        MessageBoxWindow win = new MessageBoxWindow(title, msg);
        //        win.ShowDialog();
        //    });
        //}


        /// <summary>
        /// 打开文件或文件夹
        /// </summary>
        /// <param name="url"></param>
        /// <param name="isUrl"></param>

        public static void OpenFileOrUrl(string url, bool isUrl = false)
        {
            if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
            {
                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("Explorer.exe");
                psi.Arguments = " /open," + url;
                System.Diagnostics.Process.Start(psi);
            }
            else if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
            {
                System.Diagnostics.Process.Start("xdg-open", url);
            }
            else if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
            {
                System.Diagnostics.Process.Start("open", url);
            }
        }


        /// <summary>
        /// 获取文件保存路径
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetFilePathToSave(string fileName)
        {
            return System.AppDomain.CurrentDomain.BaseDirectory + $"Files{SeparAtor}" + fileName;
        }

        /// <summary>
        /// 最后一条聊天记录发送时间的显示格式
        /// </summary> 
        public static string ShowLastMsgTime(System.DateTime date)
        {
            string v = date.ToString("yyy-MM-dd");
            string v1 = System.DateTime.Now.ToString("yyy-MM-dd");
            if (v == v1)
            {
                string str = "上午";
                int v2 = int.Parse(date.ToString("HH"));
                if (v2 > 12)
                {
                    str = "下午";
                }
                string dateTime = date.ToString("h:mm");
                if (!dateTime.Contains(str))
                {
                    dateTime = str + dateTime;
                }
                return dateTime;
            }
            return date.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 显示的用户头像
        /// </summary> 
        public static Image GetUserHead4ID(string id)
        {
            string index = "" +
                "1";
            System.String str = id.ToCharArray()[id.Length - 1].ToString();
            Regex _numericregex = new Regex(@"^[-]?[0-9]+(\.[0-9]+)?$");
            bool isExist = _numericregex.IsMatch(str);
            if (isExist)
            {
                int last = int.Parse(str);
                if (last % 2 == 0)
                {
                    index = "2";
                }
                else
                {
                    index = "3";
                }
            }
            return GlobalResourceManager.HeadPicManger.Get(index);
        }

        /// <summary>
        /// 返回对应的文件类型图片
        /// </summary> 
        public static Image GetFileIconBitmap(string fileExtendName)
        {
            try
            {
                FileType fileType = GetFileType(fileExtendName.Replace(".", ""));
                string iconDirectory = "FileIcon\\";
                string iconName = "unknown";
                switch (fileType)
                {
                    case FileType.Video:
                        iconName = "video";
                        break;
                    case FileType.Music:
                        iconName = "music";
                        break;
                    case FileType.Rar:
                        iconName = "rar";
                        break;
                    case FileType.PPT:
                        iconName = "ppt";
                        break;
                    case FileType.Word:
                        iconName = "word";
                        break;
                    case FileType.Excel:
                        iconName = "excel";
                        break;
                    case FileType.Txt:
                        iconName = "txt";
                        break;
                    case FileType.PDF:
                        iconName = "pdf";
                        break;
                    case FileType.Image:
                        iconName = "image";
                        break;
                    case FileType.Other:
                        iconName = "unknown";
                        break;
                    case FileType.None:
                        iconName = "folder";
                        break;
                }
                Image image = GlobalResourceManager.FileTypeManger.Get(iconName);

                return image;
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(ee.Message + " - " + ee.StackTrace);
                return null;
            }

        }

        public static FileType GetFileType(string fileExtendName)
        {
            try
            {
                switch (fileExtendName.ToLower())
                {
                    case "txt":
                        return FileType.Txt;
                    case "xls":
                    case "xlsx":
                        return FileType.Excel;
                    case "doc":
                    case "docx":
                        return FileType.Word;
                    case "ppt":
                    case "pptx":
                        return FileType.PPT;
                    case "pdf":
                        return FileType.PDF;
                    case "zip":
                    case "rar":
                        return FileType.Rar;
                    case "exe":
                        return FileType.Exe;
                    case "gif":
                    case "jpg":
                    case "jpeg":
                    case "png":
                    case "bmp":
                    case "ico":
                    case "svg":
                    case "tiff":
                        return FileType.Image;
                    case "mp3":
                    case "wma":
                    case "wav":
                    case "aif":
                    case "aiff":
                    case "au":
                    case "ram":
                    case "mid":
                    case "rmi":
                        return FileType.Music;
                    case "mp4":
                    case "avi":
                    case "mov":
                    case "rmvb":
                    case "rm":
                    case "flv":
                    case "3gp":
                    case "mpeg":
                    case "mpg":
                    case "dat":
                    case "mkv":
                        return FileType.Video;
                    default:
                        return FileType.Other;
                }

            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message + " - " + e.StackTrace);
                return FileType.None;
            }
        }

        public enum FileType
        {
            None,
            Other,
            Txt,
            Excel,
            Word,
            PPT,
            PDF,
            Rar,
            Exe,
            Image,
            Music,
            Video,
        }

        /// <summary>
        /// Resource
        /// </summary>
        /// <param lab_title="filePath">Resources下的文件路径（不需填写Resources之前的 如 32.ioc 或者 Head/0.png）</param> 
        public static Bitmap FindAssetsBitmap(string filePath)
        {
            try
            {
                if (filePath.Contains("Droppedlogo.png"))
                {


                }
                string fullPath = string.Format("res://{0}/Resource/{1}", ProcessName, filePath);
                Image image = null;
                CPF.Styling.ResourceManager.GetImage(fullPath, i => { image = i; });
                if (image == null) { return null; }
                return new Bitmap(image);
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(ee.Message + " - " + ee.StackTrace);
                return null;
            }
        }

        public static void Show_TopMost(this Window window)
        {
            window.TopMost = true;
            window.Show();
            window.TopMost = false;
        }
    }
}
