using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace ryCommon
{
    /// <summary>
    /// 文件信息
    /// </summary>
    public class RySoft
    {
        /// <summary>
        /// 获取版本号
        /// </summary>
        public static string VersionStr
        {
            get
            {
                //Version ver = Assembly.LoadFile(Application.ExecutablePath).GetName().Version;
                //Version ver = Assembly.GetEntryAssembly().GetName().Version;
                //return ver.Major.ToString() + "." + ver.Minor.ToString() + "." + ver.Build.ToString() + "." + ver.;
                return GetVersionStr(Application.ExecutablePath);
            }
        }
        /// <summary>
        /// 获取版本号
        /// </summary>
        /// <param name="exe_path"></param>
        /// <returns></returns>
        public static string GetVersionStr(string exe_path)
        {
            try
            {
                System.Diagnostics.FileVersionInfo info = System.Diagnostics.FileVersionInfo.GetVersionInfo(exe_path);
                return info.ProductVersion;
            }
            catch { return ""; }
            //Version ver = Assembly.LoadFile(exe_path).GetName().Version;
            //return ver.Major.ToString() + "." + ver.Minor.ToString() + "." + ver.Build.ToString() + "." + ver.Revision.ToString();
        }
        /// <summary>
        /// 根据版本字符串输出double类型的版本号
        /// </summary>
        /// <param name="ver"></param>
        /// <param name="_softVer"></param>
        /// <param name="_reVer"></param>
        public static void GetVerByVerStr(string ver, out double _softVer, out double _reVer)
        {
            _softVer = 0;
            _reVer = 0;
            string[] ver_item = ver.Split('.');
            switch (ver_item.Length)
            {
                case 1:
                    _softVer = ver_item[0].ToDouble(0);
                    break;
                case 2:
                    _softVer = ver.ToDouble(0);
                    break;
                case 3:
                    _softVer = (ver_item[0] + "." + ver_item[1]).ToDouble(0);
                    _reVer = ver_item[2].ToDouble(0);
                    break;
                case 4:
                    _softVer = (ver_item[0] + "." + ver_item[1]).ToDouble(0);
                    _reVer = (ver_item[2] + "." + ver_item[3]).ToDouble(0);
                    break;
            }
        }
      
        /// <summary>
        /// 比较两个版本号大小
        /// </summary>
        /// <param name="oldVer"></param>
        /// <param name="newVer"></param>
        /// <returns>oldVer小于newVer,返回1,大于则返回-1,相等返回0,不支持则返回-1000</returns>
        public static int CompareVer(string oldVer,string newVer)
        {
            if (oldVer == newVer) { return 0; }
            var _oldVer = oldVer;
            var _newVer = newVer;
            if (_oldVer.StartsWith("v", StringComparison.OrdinalIgnoreCase)) { _oldVer = _oldVer.Substring(1); }
            if (_newVer.StartsWith("v", StringComparison.OrdinalIgnoreCase)) { _newVer = _newVer.Substring(1); }
            var have_ts = false;
            if (!_oldVer.Replace(".", "").Replace("_", "").IsInt())
            {
                _oldVer = GetVer(_oldVer);
                have_ts = true;
                if (_oldVer.Length == 0)
                {
                    return -1000;
                }
            }
            if (!_newVer.Replace(".", "").Replace("_", "").IsInt())
            {
                _newVer = GetVer(_newVer);
                have_ts = true;
                if (_newVer.Length == 0)
                {
                    return -1000;
                }
            }
            var old_split = _oldVer.Split("._".ToCharArray());
            var new_split = _newVer.Split("._".ToCharArray());
            for (int i = 0; i < new_split.Length; i++)
            {
                var item_new = new_split[i].ToInt();
                var item_old = 0;
                if (old_split.Length > i) { item_old = old_split[i].ToInt(); }
                if (item_new > item_old) { return 1; }
                if (item_new < item_old) { return -1; }
            }
            if (have_ts)
            {
                return -1000;
            }
            return 0;
        }
        /// <summary>
        /// 从字符串中提取版本号
        /// </summary>
        /// <param name="_VerStr"></param>
        /// <returns></returns>
        public static string GetVer(string _VerStr)
        {
            var have_ver = false;
            var ResName = _VerStr.Replace("最新破解版", "").Replace("中文版", "").Replace("完美直装版", "");
            var start = ResName.Length;
            while (start >= 1)
            {
                var word = ResName.Substring(start - 1, 1);
                if (word == "." || word.IsInt())
                {
                    have_ver = true;
                    start--;
                }
                else
                {
                    break;
                }
            }
            if (have_ver)
            {
                var ver = ResName.Substring(start);
                if (ver.IsInt())
                {
                    var ver2 = GetVer(ResName.Substring(0, start));
                    if (ver2.Length > 0)
                    {
                        return ver2;
                    }
                }
                return ver;
            }
            else
            {
                start = ResName.Length;
                var end_index = -1;
                while (start >= 1)
                {
                    var word = ResName.Substring(start - 1, 1);
                    if (word == "." || word.IsInt())
                    {
                        if (end_index == -1) { end_index = start - 1; }
                        have_ver = true;
                    }
                    else
                    {
                        if (end_index != -1)
                        {
                            break;
                        }
                    }
                    start--;
                }
                if (have_ver)
                {
                    return ResName.Substring(start, end_index - start + 1);
                }
                return "";
            }
        }
        /// <summary>
        /// 比较版本号
        /// </summary>
        /// <param name="_old_softVer"></param>
        /// <param name="_old_reVer"></param>
        /// <param name="_new_softVer"></param>
        /// <param name="_new_reVer"></param>
        /// <returns>old小于new,返回1,大于则返回-1,相等返回0</returns>
        public static int CompareVer(double _old_softVer,double _old_reVer, double _new_softVer, double _new_reVer)
        {
            if (_old_softVer < _new_softVer) { return 1; }
            if (_old_softVer == _new_softVer)
            {
                if (_old_reVer < _new_reVer) { return 1; }
                else if (_old_reVer == _new_reVer) { return 0; }
                else { return -1; }
            }
            else { return -1; }
        }
        /// <summary>
        /// 判断是否是管理员方式运行
        /// </summary>
        /// <returns></returns>
        public static bool IsAdministrator()
        {
            System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
            System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
            return principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
        }
    }
}
