using System.Collections.Generic;
using Mapster;
using User;
using YGNT.Infrastructure.Grpc.Models;

namespace YGNT.Infrastructure.Grpc
{
    /// <summary>
    /// 用户
    /// </summary>
    public class GrpcUserInfo
    {
        ///// <summary>
        ///// 得到或者添加user编号
        ///// </summary>
        ///// <param name="userInfo"></param>
        ///// <returns></returns>
        //public static string GetOrAddUser(GrpcUserInfoRegistered userInfo)
        //{
        //    if (!StringHelp.IsPhone(userInfo.Phone))
        //        throw new Exception($"[{userInfo.Phone}]不是有效的手机号");
        //    if (!StringHelp.IsIdCard(userInfo.IdCard))
        //        throw new Exception($"[{userInfo.IdCard}]不是有效的身份证");

        //    var data1 = UserInfoMatch(userInfo.Phone, "")?.FirstOrDefault();
        //    var data2 = UserInfoMatch("", userInfo.IdCard)?.FirstOrDefault();
        //    if (data1 == null && data2 == null)
        //    {
        //        //注册
        //        return UserInfoRegistered(userInfo);
        //    }
        //    else if (data1 == null)
        //    {
        //        throw new Exception($"手机号[{userInfo.Phone}]没有找到匹配的信息，但是身份证[{userInfo.IdCard}]已找到匹配的人");
        //    }
        //    else if (data2 == null)
        //    {
        //        throw new Exception($"身份证[{userInfo.IdCard}]没有找到匹配的信息，但是手机号[{userInfo.Phone}]已找到匹配的人");
        //    }
        //    else if (data1.Id != data2.Id)
        //    {
        //        throw new Exception($"身份证[{userInfo.IdCard}]和手机[{userInfo.Phone}]筛选出来不是同一个用户");
        //    }
        //    else
        //    {
        //        return data1.Id;
        //    }
        //}

        /// <summary>
        /// 得到用户信息
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public static GrpcUserInfoBase GetUserInfo(string UserId)
        {
            var client = new User_Info.User_InfoClient(GrpcHost.userChannel);
            var reply = client.UserInfoBase(new UserInfoBaseParameter() { UserId = UserId ?? "" });
            var data = reply.Adapt<GrpcUserInfoBase>();
            if (string.IsNullOrEmpty(data.Id))
                data = null;
            return data;
        }

        /// <summary>
        /// 用户信息模糊查询（匹配前10条）
        /// </summary>
        /// <param name="Phone"></param>
        /// <param name="IdCard"></param>
        /// <returns></returns>
        public static List<GrpcUserInfoBase> UserInfoMatch(string Phone, string IdCard)
        {
            var client = new User_Info.User_InfoClient(GrpcHost.userChannel);
            var reply = client.UserInfoMatch(new UserInfoMatchParameter() { Phone = Phone ?? "", IdCard = IdCard ?? "" });
            return reply.Data.Adapt<List<GrpcUserInfoBase>>();
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="grpcUserInfoRegistered"></param>
        /// <returns>注册后的id</returns>
        public static string UserInfoRegistered(GrpcUserInfoRegistered grpcUserInfoRegistered)
        {
            var client = new User_Info.User_InfoClient(GrpcHost.userChannel);
            if (grpcUserInfoRegistered.Activate)
            {
                var reply = client.UserInfoRegistered(new UserInfoRegisteredParameter()
                {
                    Name = grpcUserInfoRegistered.Name ?? "",
                    IdCard = grpcUserInfoRegistered.IdCard ?? "",
                    Pwd = string.IsNullOrWhiteSpace(grpcUserInfoRegistered.Pwd) ? "abc123" : grpcUserInfoRegistered.Pwd,
                    Phone = grpcUserInfoRegistered.Phone ?? "",
                    Gender = grpcUserInfoRegistered.Gender ?? 0,
                });
                return reply.Id;
            }
            else
            {
                var reply = client.UserInfoRegisteredInactivated(new UserInfoRegisteredInactivatedParameter()
                {
                    Name = grpcUserInfoRegistered.Name ?? "",
                    IdCard = grpcUserInfoRegistered.IdCard ?? "",
                    Phone = grpcUserInfoRegistered.Phone ?? "",
                    Gender = grpcUserInfoRegistered.Gender ?? 0,
                });
                return reply.Id;
            }
        }

        /// <summary>
        /// 用户注册前验证
        /// </summary>
        /// <param name="IdCard">身份证</param>
        /// <param name="Phone">手机</param>
        /// <returns>用户id</returns>
        public static string UserInfoRegisteredVerification(string IdCard, string Phone)
        {
            var client = new User_Info.User_InfoClient(GrpcHost.userChannel);
            var reply = client.UserInfoRegisteredVerification(new UserInfoRegisteredVerificationParameter() { IdCard = IdCard, Phone = Phone });
            return reply.Id;
        }

    }
}
