using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Install.Core.Common
{
    /// <summary>
    /// 给定 程序集其中一个类，需要获取程序集版本号
    /// </summary>
    public class Ver
    {
        /// <summary>
        /// 产品名
        /// </summary>
        public string Product { get; private set; }
        /// <summary>
        /// 版本号
        /// </summary>
        public string V { get; private set; }
        /// <summary>
        /// 编译时间
        /// </summary>
        public string BuildTime { get; private set; }

        public Ver()
        {

        }
        private Type srcType;
        /// <summary>
        /// 需要获取程序集版本号， 程序集其中一个类
        /// </summary>
        public Type SrcType
        {
            get { return srcType; }
            set
            {
                srcType = value;
                update();
            }
        }
        void update()
        {
            Assembly asm = Assembly.GetAssembly(SrcType);
            AssemblyProductAttribute asmproduct = (AssemblyProductAttribute)Attribute.GetCustomAttribute(asm, typeof(AssemblyProductAttribute));
            Version version = asm.GetName().Version;

            string version_str = string.Format("v{0}.{1}.{2}", version.Major, version.Minor, version.Build);
            string location = asm.Location;
            string buliddt;
            if (!string.IsNullOrEmpty(location))
            {
                buliddt = System.IO.File.GetLastWriteTime(location).ToString("yyyyMMdd");
            }
            else
            {
                buliddt = "";
            }
            Product = asmproduct.Product;
            V = version_str;
            BuildTime = buliddt;
        }
        public override string ToString()
        {
            if (!string.IsNullOrEmpty(BuildTime))
            {
                return Product + " " + V + " at " + BuildTime;
            }
            else
            {
                return Product + " " + V;
            }
        }
    }

    public static class VerExt 
    {
        public static int[] String2Version(string version)
        {
            int[] version_parts = new int[3];
            Regex regex = new Regex(@"(\d*).(\d*).(\d*)");
            var match = regex.Match(version);
            if (match.Success)
            {
                version_parts[0] = int.Parse(match.Groups[1].Value);
                version_parts[1] = int.Parse(match.Groups[2].Value);
                version_parts[2] = int.Parse(match.Groups[3].Value);
                return version_parts;
            }
            else {
                return null;
            }
        }

        public static int VersionCompare(string version1, string version2) 
        {
            var ver_parts1 = String2Version(version1);
            var ver_parts2 = String2Version(version2);
            if (ver_parts1 == null && ver_parts2 == null)
                return 0;
            if (ver_parts1 == null && ver_parts2 != null)
                return -1;
            if (ver_parts1 != null && ver_parts2 == null)
                return 1;

            for (int i = 0; i < 3; i++) {
                if (ver_parts1[i] < ver_parts2[i])
                    return -1;
                else if (ver_parts1[i] > ver_parts2[i])
                    return 1;
            }
            return 0;

        }
    }
}
