using EnotaryoPH.Data;
using Exadel.Compreface.Clients.CompreFaceClient;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace EnotaryoPH.Web.Pages.Principal.NotaryoSteps
{
    public class TakeSelfieModel : BaseTakeSelfiePageModel
    {
        public TakeSelfieModel(NotaryoDBContext notaryoDBContext, ICurrentUserService currentUserService, ICompreFaceClient compreFaceClient, IConfiguration configuration)
            : base(notaryoDBContext, currentUserService, compreFaceClient, configuration)
        { }

        public void OnGet()
        {
        }

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            var isMatchSuccess = await PostAsync();
            if (isMatchSuccess)
            {
                return Redirect($"/Principal/NotaryoSteps/UploadDocument/{CurrentTransaction.Transaction_UID}");
            }
            ModelState.AddModelError("", "Face Verification Failed");

            return Page();
        }

        //private string GetSelfieUrl()
        //{
        //    var qb = new QueryBuilder
        //    {
        //        { "handler", "ViewImage" },
        //        { "TransactionSelfie_UID", TransactionSelfie_UID.ToString() }
        //    };
        //    var url = $"{Request.Scheme}://{Request.Host}/Principal/NotaryoSteps/TakeSelfie" + qb;
        //    return url;
        //}

        //private string GetIdentificationDocumentUrl(Guid document_UID)
        //{
        //    var qb = new QueryBuilder
        //    {
        //        { "handler", "ViewImage" },
        //        { "IdentificationDocument_UID", document_UID.ToString() }
        //    };
        //    var url = $"{Request.Scheme}://{Request.Host}/Principal/IdentificationDocument/IdentificationDocument" + qb;
        //    return url;
        //}

        [AllowAnonymous]
        public IActionResult OnGetViewImage()
        {
            if (TransactionSelfie_UID == Guid.Empty)
            {
                return NotFound();
            }

            var selfie = _notaryoDBContext.TransactionSelfies.FirstOrDefault(e => e.TransactionSelfie_UID == TransactionSelfie_UID);
            if (selfie == null)
            {
                return NotFound();
            }

            return File(selfie.File, "image/png");
        }
    }
}