using CsharpHttpHelper;
using DeviceRepair.DataAccess;
using DeviceRepair.Models;
using Newtonsoft.Json;
using System;

namespace DeviceRepairAndOptimization.Biz
{
    /// <summary>
    /// 用户相关
    /// </summary>
    public class UserManager
    {
        private static UserManager manager;
        public static UserManager Instance
        {
            get
            {
                if (manager == null)
                    manager = new UserManager();
                return manager;
            }
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public APIResponseData GetDataByCodeAndPwd(UserInfoModel model)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "登录失败！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.UserLogin,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(model)
                        });
                        break;

                    default:
                        apiResponseData = UserAccess.Instance.GetLoginEntity(model.LoginCode, model.PassWord, GlobalInfo.OperationInfo);
                        break;
                }
                return apiResponseData;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 用户登出
        /// </summary>
        public void LogOut()
        {
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.LogOut,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                        });
                        break;

                    default:
                        UserAccess.Instance.LogOut(GlobalInfo.OperationInfo);
                        break;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }


        /// <summary>
        /// 验证账户密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public APIResponseData ValidePassWord(string LoginCode, string PassWord)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "账户或密码不正确！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.ValidePassWord}?LoginCode={LoginCode}&PassWord={PassWord}",
                            Method = "Post",
                            ContentType = "application/x-www-form-urlencoded",
                        });

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        apiResponseData = UserAccess.Instance.ValidePassWord(LoginCode, PassWord);

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }


        /// <summary>
        /// 验证账户密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public APIResponseData UpdateUserPassword(string LoginCode, string PassWord)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "账户或密码不正确！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.UpdateUserPassword}?LoginCode={LoginCode}&PassWord={PassWord}",
                            Method = "Post",
                            ContentType = "application/x-www-form-urlencoded",
                        });

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        apiResponseData = UserAccess.Instance.UpdateUserPassword(LoginCode, PassWord, GlobalInfo.OperationInfo);

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 查询最后修改密码时间
        /// </summary>
        /// <param name="LoginCode"></param>
        /// <returns></returns>
        public APIResponseData CheckChangePwdDate(string LoginCode)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "账户或密码不正确！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.CheckChangePwdDate}?LoginCode={LoginCode}",
                            Method = "Get",
                            ContentType = "application/x-www-form-urlencoded",
                        });

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        apiResponseData = UserAccess.Instance.CheckChangePwdDate(LoginCode);

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetAllUsers()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "没有查询到数据！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.GetAllUsers,
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });

                        #endregion
                        break;
                    case "sql":
                        #region sql

                        apiResponseData = UserAccess.Instance.GetAllUsers();

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取用户名称及用户编号映射
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetUserMapping()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "没有查询到数据！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.GetUserMapping,
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });

                        #endregion
                        break;
                    case "sql":
                        #region sql

                        apiResponseData = UserAccess.Instance.GetUserMapping();

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public APIResponseData Update(UserInfoModel Item)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                Item.ModifyBy = GlobalInfo.CurrentUser.AutoID;

                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.EditUser,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(Item)
                        });

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        apiResponseData = UserAccess.Instance.Update(Item, GlobalInfo.OperationInfo);

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 根据主键编号是插入/更新
        /// </summary>
        /// <param name="Item"></param>
        /// <returns></returns>
        public APIResponseData InsertOrEdit(UserInfoModel Item)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败！" };
            try
            {
                Item.ModifyBy = GlobalInfo.CurrentUser.AutoID;

                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.InsertOrEdit,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(Item)
                        });

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        apiResponseData = UserAccess.Instance.InsertOrEdit(Item, GlobalInfo.OperationInfo);

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 判断用户名是否存在
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public APIResponseData ExistLoginCode(string LoginCode)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "账户或密码不正确！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.ExistLoginCode}?LoginCode={LoginCode}",
                            Method = "Get",
                            ContentType = "application/x-www-form-urlencoded",
                        });

                        #endregion

                        break;
                    case "sql":
                        #region sql

                        apiResponseData = UserAccess.Instance.ExistLoginCode(LoginCode);

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }
    }
}
