using NetinaShop.Domain.Entities.Pages;

namespace NetinaShop.AdminPanel.PWA.Pages;

public class FaqManagementPageViewModel : BaseViewModel<FAQPage>
{
    private readonly NavigationManager _navigationManager;
    private readonly ISnackbar _snackbar;
    private readonly IUserUtility _userUtility;
    private readonly IDialogService _dialogService;
    private readonly IRestWrapper _restWrapper;

    public FaqManagementPageViewModel(NavigationManager navigationManager, ISnackbar snackbar, IUserUtility userUtility, IRestWrapper restWrapper, IDialogService dialogService)
    {
        _navigationManager = navigationManager;
        _snackbar = snackbar;
        _userUtility = userUtility;
        _restWrapper = restWrapper;
        _dialogService = dialogService;
    }


    public string Question { get; set; } = string.Empty;
    public string Answer { get; set; } = string.Empty;
    private PageActionRequestDto? _request;
    public override async Task InitializeAsync()
    {
        try
        {
            var token = await _userUtility.GetBearerTokenAsync();
            if (token == null)
                throw new Exception("Token is null");
            IsProcessing = true;
            PageDto.Faqs.Clear();
            var typeName = typeof(FAQPage).FullName;
            var dto = await _restWrapper.PageRestApi.ReadByType(typeName, token);
            if (!dto.Data.IsNullOrEmpty())
            {

                PageDto = dto.GetData<FAQPage>();
                _request = new PageActionRequestDto
                {
                    Name = dto.Name,
                    Content = dto.Content,
                    Description = dto.Description,
                    Id = dto.Id,
                    IsCustomPage = dto.IsCustomPage,
                    IsHtmlBasePage = dto.IsHtmlBasePage,
                    Slug = dto.Slug,
                    Type = typeof(FAQPage).FullName
                };
            }
        }
        catch (ApiException ex)
        {
            var exe = await ex.GetContentAsAsync<ApiResult>();
            _snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
        }
        catch (Exception e)
        {
            _snackbar.Add(e.Message, Severity.Error);
        }
        finally
        {

            IsProcessing = false;
        }
        await base.InitializeAsync();
    }

    public async Task SaveAsync()
    {
        try
        {
            var token = await _userUtility.GetBearerTokenAsync();
            if (token == null)
                throw new Exception("Token is null");
            IsProcessing = true;
            var request = new PageActionRequestDto
            {
                Name = "سوالات متداول",
                Content = string.Empty,
                Description = string.Empty,
                Data = PageDto,
                IsCustomPage = true,
                IsHtmlBasePage = false,
                Slug = "faq",
                Type = typeof(FAQPage).FullName
            };
            if (_request != null)
            {
                request = _request;
            }
            request.Data = PageDto;
            await _restWrapper.PageRestApi.CreatePage(request, token);
        }
        catch (ApiException ex)
        {
            var exe = await ex.GetContentAsAsync<ApiResult>();
            _snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
        }
        catch (Exception e)
        {
            _snackbar.Add(e.Message, Severity.Error);
        }
        finally
        {

            IsProcessing = false;
        }
    }
    public void AddNewQuestion()
    {
        try
        {
            if (Question.IsNullOrEmpty())
                throw new Exception("سوال را وارد کنید");
            if (Answer.IsNullOrEmpty())
                throw new Exception("پاسخ را وارد کنید");

            PageDto.Faqs.Add(Question,Answer);
        }
        catch (Exception e)
        {
            _snackbar.Add(e.Message, Severity.Error);
        }
    }
    public void RemoveQuestion(string question)
    {
        try
        {
            PageDto.Faqs.Remove(question);
        }
        catch (Exception e)
        {
            _snackbar.Add(e.Message, Severity.Error);
        }
    }
}