using DAL;
using DAL._Shared.SharedModels;
using DAL._Shared.SharedRepositories;
using HRD.LDAPService.JWT;
using HRD.WebApi.Controllers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using System;
using System.Threading.Tasks;

namespace StaffDBServer.SharedControllers
{
    [JWTAuthorize]
    public class WebAppUserController : BaseMiniController
    {
        private readonly WebAppUserRepository webAppUserRepository;
        private readonly WebAppUserHelper webAppUserHelper;

        public WebAppUserController(WebApiContext context, WebAppUserRepository webAppUserRepository, WebAppUserHelper webAppUserHelper) : base(context)
        {
            this.webAppUserRepository = webAppUserRepository;
            this.webAppUserHelper = webAppUserHelper;
        }

        [HttpPost("Culture")]
        public async Task<IActionResult> UpdateCultureAsync([FromBody] WebAppUser userFromClient)
        {
            try
            {
                WebAppUser userFromDB = await webAppUserRepository.GetByAsync(u => u.LoginName == userFromClient.LoginName, false);

                if (userFromDB != default) //first login, get User from WebAppEmployeeInfo
                {
                    userFromDB.Language = userFromClient.Language;
                    userFromDB.Culture = userFromClient.Culture;
                    if (!await webAppUserRepository.UpdateAsync(userFromDB))
                    {
                        return StatusCode(StatusCodes.Status400BadRequest, $"Cannot set User's language/culture");
                    }
                }
                return new OkObjectResult(userFromClient);
            }
            catch (Exception ex)
            {
                WriteLogException(ex, ex.Message);
                return StatusCode(StatusCodes.Status400BadRequest, $"Cannot set User's language/culture");
            }
        }

        [HttpPost("LoginWithJWT")]
        public async Task<IActionResult> LoginWithAuthorizationAsync([FromBody] WebAppUser userFromClient)
        {
            try
            {
                var accessToken = Request.Headers[HeaderNames.Authorization];
                
                WebAppUser result = await webAppUserHelper.CheckLoginWithJWTAsync(accessToken, userFromClient.ClientVersion);
                return new OkObjectResult(result);
            }
            catch (Exception ex)
            {
                WriteLogException(ex, ex.Message);
                return Unauthorized(ex.Message);
            }
        }

        [HttpPost("LoginWithNameAndPassword/{webApiId}"),]
        [Microsoft.AspNetCore.Authorization.AllowAnonymous]
        public async Task<IActionResult> LoginWithNameAndPasswordAsync([FromBody] WebAppUser userFromClient, int webApiId)
        {
            try
            {                
                var result = await webAppUserHelper.CheckLoginWithNameAndPasswordAsync(userFromClient, webApiId);
                return new OkObjectResult(result);
            }
            catch (Exception ex)
            {
                WriteLogException(ex, ex.Message);
                return NotFound(ex.Message);
            }
        }

        [HttpPost("LoginWithNameAndPassword")]
        [Microsoft.AspNetCore.Authorization.AllowAnonymous]
        public async Task<IActionResult> LoginWithNameAndPasswordAsync([FromBody] WebAppUser userFromClient)
        {
            try
            {
                var result = await webAppUserHelper.CheckLoginWithNameAndPasswordAsync(userFromClient);
                return new OkObjectResult(result);
            }
            catch (Exception ex)
            {
                WriteLogException(ex, ex.Message);
                return NotFound(ex.Message);
            }
        }
    }
}