using ASHermed.SinoInterface.IFramework.SingleSignOn;
using ASHermed.SinoInterface.Api.ContractModel.Framework.Login;
using ASHermed.SinoInterface.ContractModel;
using ASHermed.SinoInterface.Tools;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Ashermed.BaseFramework;

namespace ASHermed.SingleSignOnFramework
{
    /// <summary>
	/// 登录服务
	/// 创建人：杨江军
	/// 创建时间：2020/4/8 16:42:28
	/// </summary>
	public class LoginService: ILoginService
    {
        #region 初始化

        /// <summary>
        /// HttpClientHelp
        /// </summary>
        private readonly IHttpClientHelp httpClient;

        /// <summary>
        /// 登录服务
        /// </summary>
        /// <param name="_httpClient"></param>
        public LoginService(IHttpClientHelp _httpClient)
        {
            httpClient = _httpClient;
        }

        #endregion

        #region 发送短信验证码

        /// <summary>
        /// 发送短信验证码
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<BaseResponse> SendSMSVerifyCode(SendSMSVerifyCodeParam param)
        {
            param.SystemId = SysUtil.GetIsLogs() ? SingleSignOnConfig.GetInstance().LogsSystemId : SingleSignOnConfig.GetInstance().SystemId;
            var result = await httpClient.HttpPostAsync<BaseResponse>(SingleSignOnConfig.GetInstance().ApiHost, "/api/v1/Login/SendSMSVerifyCode", parameters: new List<(string Name, string FileName, object Value)>() { (Name: "json", FileName: "", Value: Newtonsoft.Json.JsonConvert.SerializeObject(param)) }, isMultilevelNestingJson: true);
            return result.Data;
        }

        #endregion

        #region 通过手机号码和验证码进行单点登录

        /// <summary>
        /// 通过手机号码和验证码进行单点登录
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<BaseResponse<SingleSignOnModel>> SingleSignOnByPhones(SingleSignOnByPhonesParam param)
        {
            param.SystemId = SysUtil.GetIsLogs() ? SingleSignOnConfig.GetInstance().LogsSystemId : SingleSignOnConfig.GetInstance().SystemId;
            var result = await httpClient.HttpPostAsync<BaseResponse<SingleSignOnModel>>(SingleSignOnConfig.GetInstance().ApiHost, "/api/v1/Login/SingleSignOnByPhones", parameters: new List<(string Name, string FileName, object Value)>() { (Name: "json", FileName: "", Value: Newtonsoft.Json.JsonConvert.SerializeObject(param)) }, isMultilevelNestingJson: true);
            return result.Data;
        }

        #endregion

        #region 通过登录名和密码进行单点登录

        /// <summary>
        /// 通过登录名和密码进行单点登录
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<BaseResponse<SingleSignOnModel>> SingleSignOnByLoginId(SingleSignOnByLoginIdParam param)
        {
            param.SystemId = SysUtil.GetIsLogs() ? SingleSignOnConfig.GetInstance().LogsSystemId : SingleSignOnConfig.GetInstance().SystemId;
            var result = await httpClient.HttpPostAsync<BaseResponse<SingleSignOnModel>>(SingleSignOnConfig.GetInstance().ApiHost, "/api/v1/Login/SingleSignOnByLoginId", parameters: new List<(string Name, string FileName, object Value)>() { (Name: "json", FileName: "", Value: Newtonsoft.Json.JsonConvert.SerializeObject(param)) }, isMultilevelNestingJson: true);
            return result.Data;
        }

        #endregion

        #region 刷新Token

        /// <summary>
        /// 刷新Token
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<BaseResponse<RefreshTokenModel>> RefreshToken(RefreshTokenParam param)
        {
            param.SystemId = SysUtil.GetIsLogs() ? SingleSignOnConfig.GetInstance().LogsSystemId : SingleSignOnConfig.GetInstance().SystemId;
            var result = await httpClient.HttpPostAsync<BaseResponse<RefreshTokenModel>>(SingleSignOnConfig.GetInstance().ApiHost, "/api/v1/Login/RefreshToken", parameters: new List<(string Name, string FileName, object Value)>() { (Name: "json", FileName: "", Value: Newtonsoft.Json.JsonConvert.SerializeObject(param)) }, isMultilevelNestingJson: true);
            return result.Data;
        }

        #endregion

        #region 退出登录

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<BaseResponse> SignOut(SignOutParam param)
        {
            param.SystemId = SysUtil.GetIsLogs() ? SingleSignOnConfig.GetInstance().LogsSystemId : SingleSignOnConfig.GetInstance().SystemId;
            var result = await httpClient.HttpPostAsync<BaseResponse>(SingleSignOnConfig.GetInstance().ApiHost, "/api/v1/Login/SignOut", parameters: new List<(string Name, string FileName, object Value)>() { (Name: "json", FileName: "", Value: Newtonsoft.Json.JsonConvert.SerializeObject(param)) }, isMultilevelNestingJson: true);
            return result.Data;
        }

        #endregion
    }
}
