using FluentValidation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Net.NetworkInformation;
using System.Security.Claims;
using System.Text.Json;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.Web.Services.Interfaces;

namespace WorkFlowCheck.Web.Pages.Account
{
    [Authorize]
    [ValidateAntiForgeryToken]
    public class ChangePasswordModel : PageModel
    {
        private readonly IUserService _userService;

        [BindProperty]
        public UserChangePassword2FADTO UserChangePassword2FADTO { get; set; }

        public string ErrorMessage { get; set; }

        public ChangePasswordModel(IUserService userService)
        {
            _userService = userService;
        }

        public async Task OnGet()
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var user = await _userService.GetUser(int.Parse(userId));
            if (user != null && user.Id > 0)
            {

                UserChangePassword2FADTO = new UserChangePassword2FADTO()
                {
                    UserName = user.UserName,
                    UserId = user.Id,
                };
            }
        }
        public async Task<IActionResult> OnPostLogin2F1([FromBody] UserSimpleDTO userSimpleDTO)
        {

            if (userSimpleDTO != null)
            {
                var response = await _userService.Authenticate2F1(userSimpleDTO.UserName, userSimpleDTO.Password);

                if (response.IsSuccess)
                {
                    return new JsonResult(new { success = true, data = response.Data });
                }
                else
                {
                    ErrorMessage = "Hibás bejelentkezés!";
                    return new JsonResult(new { success = false });
                }
            }
            else
            {
                ErrorMessage = "Hibás bejelentkezés!";
                return new JsonResult(new { success = false });
            }
        }
        public async Task<IActionResult> OnPostLogin2F2([FromBody] UserChangePassword2FADTO userChangePassword2FADTO)
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var user = await _userService.GetUser(int.Parse(userId));
            userChangePassword2FADTO.UserId = user.Id;
            var response = await _userService.Authenticate2F2ChangePassword(userChangePassword2FADTO.UserName,
                                                                            userChangePassword2FADTO.OldPassword,
                                                                            userChangePassword2FADTO.NewPassword1,
                                                                            userChangePassword2FADTO.Code,
                                                                            userChangePassword2FADTO.Token2FA);

            if (response.IsSuccess)
            {

                var token = response.Data.JwtToken;

                Response.Cookies.Append("AuthToken", token, new CookieOptions
                {
                    HttpOnly = true,
                    Secure = true,
                    SameSite = SameSiteMode.Strict,
                    Expires = DateTimeOffset.UtcNow.AddDays(1)
                });

                return new JsonResult(new { success = true });
            }

            ErrorMessage = "Hibás bejelentkezés!";
            return new JsonResult(new { success = false });
        }
    }
}
