using Blazorise;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Server.BaseClasses;
using FoodsharingSiegen.Server.Data.Service;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;

namespace FoodsharingSiegen.Server.Dialogs
{
    public partial class VerificationSettingsDialog : FsBase
    {
        [Inject]
        public ProspectService ProspectService { get; set; } = null!;

        [Inject]
        public IJSRuntime JS { get; set; } = null!;

        [Parameter]
        public Prospect? Prospect { get; set; }
        
        [Parameter]
        public Func<Task>? OnDataChanged { get; set; }
        
        private int ImageCount { get; set; } = 0;
        private bool ShowLinkPanel { get; set; } = false;

        private bool CopySuccess { get; set; } = false;
        private string LinkUrl { get; set; } = string.Empty;

        protected override async Task OnInitializedAsync()
        {
            if (Prospect != null)
            {
                var result = await ProspectService.GetVerificationImagesAsync(Prospect.Id);
                if (result.Success && result.Data != null)
                {
                    ImageCount = result.Data.Count;
                }
            }
        }

        public static async Task ShowAsync(IModalService modalService, Prospect? prospect, Func<Task>? onDataChanged)
        {
            var title = "Identitätsprüfung";
            var action = new Action<ModalProviderParameterBuilder<VerificationSettingsDialog>>(b =>
            {
                b.Add(nameof(Prospect), prospect);
                b.Add(nameof(OnDataChanged), onDataChanged);
            });

            await modalService.Show(title, action, new ModalInstanceOptions { Size = ModalSize.Default });
        }

        private async Task GenerateLinkAsync()
        {
            if (Prospect == null) return;
            
            Guid token = Prospect.VerificationToken ?? Guid.Empty;
            
            if (token == Guid.Empty)
            {
                var result = await ProspectService.GenerateVerificationTokenAsync(Prospect.Id);
                if (result.Success)
                {
                    token = result.Data;
                    if (OnDataChanged != null) await OnDataChanged();
                }
                else
                {
                    await Notification.Error(result.Exception?.Message ?? "Ein Fehler ist aufgetreten.");
                    return;
                }
            }
            
            LinkUrl = NavigationManager.BaseUri + "verify/" + token.ToString();
            ShowLinkPanel = true;
        }

        private async Task CopyLink()
        {
            await JS.InvokeVoidAsync("navigator.clipboard.writeText", LinkUrl);
            CopySuccess = true;
        }

        private async Task ViewImagesAsync()
        {
            await ModalService.Hide();
            if (Prospect != null)
            {
                await ViewImagesDialog.ShowAsync(ModalService, Prospect);
            }
        }

        private async Task DeleteImagesAsync()
        {
            if (Prospect == null) return;
            
            await ConfirmDialog.ShowAsync(ModalService, "Bilder Löschen", $"Sollen alle Identitätsprüfungsbilder von {Prospect.Name} unwiderruflich gelöscht werden?", async () =>
            {
                var result = await ProspectService.DeleteVerificationImagesAsync(Prospect.Id);
                if (result.Success)
                {
                    ImageCount = 0;

                    if(OnDataChanged != null) await OnDataChanged();
                }
            });
        }
    }
}
