using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Traffic.Data.ViewModels;
using Traffic.Service.Helpers;
using Traffic.Service.Interfaces;
namespace Traffic.Api.Controllers
{
    [Route("api/login")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private readonly ILoginService _service;
        private readonly JwtHelpers _jwt;
        private IHttpContextAccessor _accessor;
        private readonly IConfiguration _configuration;
        public LoginController(ILoginService service, JwtHelpers jwt, IHttpContextAccessor accessor, IConfiguration configuration)
        {
            _service = service;
            _jwt = jwt;
            _accessor = accessor;
            _configuration = configuration;
        }
        /// 
        /// 登入
        /// 
        /// 
        /// 
        [HttpPost("PostLogin")]
        public LoginResultModel PostLogin(LoginViewModel loginData)
        {
            var ip = _accessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
            var result = new LoginResultModel();
            var user = this._service.Login(loginData.Account, loginData.Password, ip);
            if (user == null)
            {
                result.Success = false;
                result.Message = "查無此使用者,請洽管理員。";
                return result;
            }
            if (user.Status == 0)
            {
                result.Success = false;
                result.Message = "此使用者已被禁用,請洽管理員。";
                return result;
            }
            if ((DateTime.Now - user.ChangePwdOn).Days > Convert.ToInt16(_configuration["User:NeedChangePwdDay"]))
            {
                result.Success = false;
                result.Message = $"已超過{_configuration["User:NeedChangePwdDay"]}天未更改密碼,請洽管理員。";
                return result;
            }
            var errorCount = Convert.ToInt16(_configuration["User:LoginErrorCount"]);
            if (user.ErrorCount > errorCount)
            {
                result.Success = false;
                result.Message = $"輸入密碼已累積超過錯誤{errorCount}次,請洽管理員。";
                return result;
            }
            if (user.ErrorCount != 0)
            {
                result.Success = false;
                result.Message = $"輸入密碼已累積錯誤{user.ErrorCount}次。";
                return result;
            }
            if ((DateTime.Now - user.ChangePwdOn).Days > Convert.ToInt16(_configuration["User:AlarmChangePwdDay"]))
            {
                result.Success = true;
                result.Message = $"已超過{_configuration["User:AlarmChangePwdDay"]}天未更改密碼,請立即更改密碼。";
                result.Token = this._jwt.GenerateToken(user);
                result.Role = user.Role;
                result.Name = user.Name;
                result.Id = user.Id;
                return result;
            }
            if (loginData.Password == "000000")
            {
                result.Success = true;
                result.Message = $"請立即更改密碼。";
                result.Token = this._jwt.GenerateToken(user);
                result.Role = user.Role;
                result.Name = user.Name;
                result.Id = user.Id;
                return result;
            }
            result.Success = true;
            result.Message = "success!";
            result.Token = this._jwt.GenerateToken(user);
            result.Role = user.Role;
            result.Name = user.Name;
            result.Id = user.Id;
            return result;
        }
    }
}