using Hotline.Application.Identity;
using Hotline.Caching.Interfaces;
using Hotline.Share.Dtos.Identity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Cryptography;
using System.Text;
using Hotline.Api.Filter;
using Hotline.Application.Systems;
using Hotline.Settings;
using XC.RSAUtil;
using XF.Domain.Constants;
using XF.Domain.Exceptions;

namespace Hotline.Api.Controllers;

public class IdentityController : BaseController
{
    private readonly IIdentityAppService _identityAppService;
    private readonly ISystemSettingCacheManager _systemSettingCacheManager;
    private readonly ISystemLogApplication _iSystemLogApplication;

	private const string PublicKey = @"-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgw+/x6IQPkH0A4eoF63j
kLThsOXWyNBdcL9LATGy/G1yTHOr1RyKJB//iNug+V8DIoIHuFTlhgLHDbSqxvRW
MONxIIF289riS6bDI4Ox/pFmOfmElFRk0lKGihaTE2Aefd6g/N+RfLLaHWztY+/v
oVeDTiOIw9y3tokIxjKwuJ/mQ66MkKh78AqQjjSD/3jcBP8ZhMyCJOK9XQcqvhD6
WBFWkxlAqKOWggDU7YohfrbNkg3bd0oGE6zCE2EHhkcQbzGCh3lu1zf4TfKMXD+P
Prr5JWDNYQTXFQklqgae+Puge7xxZGYRoi5YpIUnkQGm6zpPxhIOdxlz+Yb5geSJ
UQIDAQAB
-----END PUBLIC KEY-----";
    private const string PrivateKey = @"-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAgw+/x6IQPkH0A4eoF63jkLThsOXWyNBdcL9LATGy/G1yTHOr
1RyKJB//iNug+V8DIoIHuFTlhgLHDbSqxvRWMONxIIF289riS6bDI4Ox/pFmOfmE
lFRk0lKGihaTE2Aefd6g/N+RfLLaHWztY+/voVeDTiOIw9y3tokIxjKwuJ/mQ66M
kKh78AqQjjSD/3jcBP8ZhMyCJOK9XQcqvhD6WBFWkxlAqKOWggDU7YohfrbNkg3b
d0oGE6zCE2EHhk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wfr3hS4uYaxpjK
qAsDkLg3nhCLvvyWAsDyVdNiZDL1J6ZBA3Qoi8P2xFWSApB+ryDPs3YOtiH0QZui
9UBfAoGBAIQ2uv4J7ql++EpNNtLzOhTEpyjWS+qRSZRRXfKN2lm7e7czOsSpKIJx
Q9PP8NTEmKqdI3WVFYqW/OlOFC6sjiscTOOn9Tc5Mrcn8ocCjAPjkhkCCVRMiJnv
jxrWXHbT1FB6DqkdOnBbQqS1Azqz5HxLlSyEK3F60e3SgB5iZsDZ
-----END RSA PRIVATE KEY-----";

    public IdentityController(IIdentityAppService identityAppService, ISystemSettingCacheManager systemSettingCacheManager, ISystemLogApplication iSystemLogApplication)
    {
        _identityAppService = identityAppService;
        _systemSettingCacheManager = systemSettingCacheManager;
        _iSystemLogApplication = iSystemLogApplication;
	}

    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [AllowAnonymous]
    [HttpPost("login")]
    [LogFilter("",false)]
	public async Task<string> Login([FromBody] LoginDto dto)
    {
        dto = Decrypt(dto);
        var res = await _identityAppService.LoginAsync(dto, HttpContext.RequestAborted);
        dto.Password = string.Empty;
        await _iSystemLogApplication.AddLog("账号登录", res, dto, HttpContext,dto.Username);
		return res ;
    }

    [AllowAnonymous]
    [ApiExplorerSettings(IgnoreApi = true)]
    [HttpPost("token")]
    public async Task<string> LoginWithoutCrypt([FromBody] LoginDto dto)
    {
        return await _identityAppService.LoginAsync(dto, HttpContext.RequestAborted);
    }

    /// <summary>
    /// 登录页系统配置
    /// </summary>
    /// <returns></returns>
    [AllowAnonymous]
    [HttpGet("login-page-info")]
    public async Task<LoginPageInfoDto> LoginPageInfo()
    {
        var SysName = _systemSettingCacheManager.GetSetting(SettingConstants.SysName).SettingValue;
        var LoginImage = _systemSettingCacheManager.GetSetting(SettingConstants.LoginImage).SettingValue;
        var IsLoginMessageCode = bool.Parse(_systemSettingCacheManager.GetSetting(SettingConstants.IsLoginMessageCode).SettingValue[0]);

        return new LoginPageInfoDto() { SysName = SysName, LoginImage = LoginImage, IsLoginMessageCode = IsLoginMessageCode };
    }

    private LoginDto Decrypt(LoginDto dto)
    {
        if (string.IsNullOrEmpty(dto.Username) || string.IsNullOrEmpty(dto.Password))
            throw UserFriendlyException.SameMessage("非法参数");

        var pkcs1 = new RsaPkcs1Util(Encoding.UTF8, PublicKey, PrivateKey);
        try
        {
            var uname = pkcs1.Decrypt(dto.Username, RSAEncryptionPadding.Pkcs1);
            var pwd = pkcs1.Decrypt(dto.Password, RSAEncryptionPadding.Pkcs1);
            var msgCode = pkcs1.Decrypt(dto.MsgCode, RSAEncryptionPadding.Pkcs1);
            return new LoginDto { Username = uname, Password = pwd,MsgCode=msgCode };
        }
        catch (Exception e)
        {
            throw new UserFriendlyException($"解密失败：{e.Message}", "无效参数");
        }
    }
}