using CsharpHttpHelper;
using DeviceRepairAndOptimization.Data;
using DeviceRepairAndOptimization.Models;
using DeviceRepairAndOptimization.Utils;
using Newtonsoft.Json;
using System;

namespace DeviceRepairAndOptimization.Business
{
    public class LoginManager
    {
        private static LoginManager manager;
        public static LoginManager Instance
        {
            get
            {
                if (manager == null)
                    manager = new LoginManager();
                return manager;
            }
        }

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

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