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

namespace PushPlatform.Common.Util
{
    public class VersionUtil
    {
        /// <summary>
        /// 比较版本号
        /// </summary>
        /// <param name="version1">1.0.1</param>
        /// <param name="version2">1.0.2</param>
        /// <returns>if version1 > version2, return 1, if equal, return 0, else return -1</returns>
        public int VersionCompare(String version1, String version2)
        {
            if (version1 == null || version1.Length == 0 || version2 == null || version2.Length == 0)
                throw new ArgumentException("Invalid parameter!");

            int index1 = 0;
            int index2 = 0;
            while (index1 < version1.Length && index2 < version2.Length)
            {
                int[] number1 = GetValue(version1, index1);
                int[] number2 = GetValue(version2, index2);

                if (number1[0] < number2[0]) return -1;
                else if (number1[0] > number2[0]) return 1;
                else
                {
                    index1 = number1[1] + 1;
                    index2 = number2[1] + 1;
                }
            }
            if (index1 == version1.Length && index2 == version2.Length) return 0;
            if (index1 < version1.Length)
                return 1;
            else
                return -1;
        }


        private static int[] GetValue(String version, int index)
        {
            int[] value_index = new int[2];
            StringBuilder sb = new StringBuilder();
            while (index < version.Length && version[index] != '.')
            {
                sb.Append(version[index]);
                index++;
            }
            value_index[0] = int.Parse(sb.ToString());
            value_index[1] = index;

            return value_index;
        }

        /// <summary>
        /// 生成版本号
        /// </summary>
        /// <param name="old_version_no">上一个版本号</param>
        /// <returns>新版本号</returns>
        public string GenerateVersionNo(string old_version_no = null)
        {
            if (string.IsNullOrEmpty(old_version_no))
            {
                return "1.0.0.0";
            }
            Version version = new Version(old_version_no);
            int ma = version.Major, mi = version.Minor, bu = version.Build, re = version.Revision;

            if (re < 100)
            {
                re += 1;
            }
            else
            {
                if (bu < 100)
                {
                    bu += 1;
                }
                else
                {
                    if (mi < 100)
                    {
                        mi += 1;
                    }
                    else
                    {
                        ma += 1;
                    }
                }
            }
            Version new_version = new Version(ma, mi, bu, re);
            return new_version.ToString();
        }

        /// <summary>
        /// 获取版本值
        /// </summary>
        /// <param name="version">版本号</param>
        /// <returns></returns>
        public static int GetVersionCode(string version)
        {
            if (string.IsNullOrEmpty(version))
                return 1;
            else
            {
                string[] v = version.Split('.');
                return Convert.ToInt32(v[0]) + Convert.ToInt32(v[1]) + Convert.ToInt32(v[2]) + Convert.ToInt32(v[3]);
            }
        }
    }
}
