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

namespace HuanMeng.DotNetCore.Utility
{
    /// <summary>
    /// 其它扩展类
    /// </summary>
    public static class OtherExtensions
    {
        /// <summary>
        /// 验证身份证号是否正确
        /// </summary>
        /// <param name="idNumber"></param>
        /// <returns></returns>

        public static bool ValidateIDCard(string idNumber)
        {
            if (string.IsNullOrEmpty(idNumber))
            {
                return false;
            }

            // 验证长度
            if (idNumber.Length != 18)
            {
                return false;
            }

            // 验证格式
            Regex regex = new Regex(@"^\d{17}(\d|X)$");
            if (!regex.IsMatch(idNumber))
            {
                return false;
            }

            // 验证校验码
            return ValidateCheckDigit(idNumber);
        }
        /// <summary>
        /// 验证校验码
        /// </summary>
        /// <param name="idNumber"></param>
        /// <returns></returns>

        private static bool ValidateCheckDigit(string idNumber)
        {
            // 加权因子
            int[] weights = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            // 校验码
            char[] checkDigits = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };

            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += (idNumber[i] - '0') * weights[i];
            }

            int mod = sum % 11;
            return idNumber[17] == checkDigits[mod];
        }

        /// <summary>
        /// 根据身份证号计算年龄、性别、生日
        /// </summary>
        /// <param name="identityCard">身份证号</param>
        /// <returns></returns>
        public static (bool verify, int age, string sex, string birthday) GetIDCardBirthdayAgeSex(string identityCard)
        {
            var birthday = "";
            var age = 0;
            var sex = "";
            if (string.IsNullOrEmpty(identityCard))
            {
                return (false, 0, "", "");
            }
            else
            {
                if (identityCard.Length != 15 && identityCard.Length != 18)//身份证号码只能为15位或18位其它不合法
                {
                    return (false, 0, "", "");
                }
            }

            birthday = "";
            string strSex = string.Empty;
            if (identityCard.Length == 18)//处理18位的身份证号码从号码中得到生日和性别代码
            {
                birthday = identityCard.Substring(6, 4) + "-" + identityCard.Substring(10, 2) + "-" + identityCard.Substring(12, 2);
                strSex = identityCard.Substring(14, 3);
            }
            if (identityCard.Length == 15)
            {
                birthday = "19" + identityCard.Substring(6, 2) + "-" + identityCard.Substring(8, 2) + "-" + identityCard.Substring(10, 2);
                strSex = identityCard.Substring(12, 3);
            }

            age = CalculateAge(birthday);//根据生日计算年龄

            if (int.Parse(strSex) % 2 == 0)//性别代码为偶数是女性奇数为男性
            {
                sex = "女";
            }
            else
            {
                sex = "男";
            }
            return (true, age, sex, birthday);
        }

        /// <summary>
        /// 根据出生日期，计算精确的年龄
        /// </summary>
        /// <param name="birthDate">生日</param>
        /// <returns></returns>
        public static int CalculateAge(string birthDay)
        {
            DateTime birthDate = DateTime.Parse(birthDay);
            DateTime nowDateTime = DateTime.Now;
            int age = nowDateTime.Year - birthDate.Year;
            //再考虑月、天的因素
            if (nowDateTime.Month < birthDate.Month || (nowDateTime.Month == birthDate.Month && nowDateTime.Day < birthDate.Day))
            {
                age--;
            }
            return age;
        }
    }
}
