using Microsoft.AspNetCore.Components;
using MudBlazor;
using Netina.AdminPanel.PWA.Services.RestServices;
using Netina.Domain.Entities.Warehouses;

namespace Netina.AdminPanel.PWA.Dialogs;

public class PageActionDialogBoxViewModel : BaseViewModel<BasePageLDto>
{
    private readonly ISnackbar _snackbar;
    private readonly IRestWrapper _restWrapper;
    private readonly IUserUtility _userUtility;
    private readonly IDialogService _dialogService;
    private readonly MudDialogInstance _mudDialog;


    public PageActionDialogBoxViewModel(ISnackbar snackbar,
        IRestWrapper restWrapper,
        IUserUtility userUtility,
        IDialogService dialogService,
        MudDialogInstance mudDialog) : base(userUtility)
    {
        _snackbar = snackbar;
        _restWrapper = restWrapper;
        _userUtility = userUtility;
        _dialogService = dialogService;
        _mudDialog = mudDialog;
    }

    public PageActionDialogBoxViewModel(ISnackbar snackbar,
        IRestWrapper restWrapper,
        IUserUtility userUtility,
        IDialogService dialogService,
        MudDialogInstance mudDialog,
        BasePageSDto page) : base(userUtility)
    {
        _snackbar = snackbar;
        _restWrapper = restWrapper;
        _userUtility = userUtility;
        _dialogService = dialogService;
        _mudDialog = mudDialog;
        IsEditing = true;
        PageId = page.Id;
    }
    public override async Task InitializeAsync()
    {
        try
        {
            IsProcessing = true;
            var token = await _userUtility.GetBearerTokenAsync();
            if (token == null)
                throw new Exception("Token is null");
            var pages = await _restWrapper.PageRestApi.ReadById(PageId,token);
            PageDto = pages;
        }
        catch (ApiException e)
        {
            var exe = await e.GetContentAsAsync<ApiResult>();
            _snackbar.Add(exe != null ? exe.Message : e.Content, Severity.Error);
        }
        catch (Exception ex)
        {
            _snackbar.Add(ex.Message, Severity.Error);
        }
        finally
        {
            IsProcessing = false;
        }

        await base.InitializeAsync();
    }


    public bool IsEditing = false;
    public Guid PageId { get; set; }

    public void Cancel() => _mudDialog.Cancel();

    public BasePageSection Section { get; set; } = new();
    public void AddSection()
    {
        try
        {
            if (Section.Title.IsNullOrEmpty())
                throw new Exception("نام سکشن را وارد کنید");
            PageDto.Sections.Add(Section);
        }
        catch (Exception ex)
        {
            _snackbar.Add(ex.Message, Severity.Error);
        }
    }

    public async Task SubmitEditAsync()
    {
        try
        {
            IsProcessing = true;
            if (PageDto.Id == default)
                throw new Exception("Id is null !");

            var token = await _userUtility.GetBearerTokenAsync();
            if (token == null)
                throw new Exception("Token is null");

            if (PageDto.Title.IsNullOrEmpty())
                throw new Exception("نام را وارد کنید");

            var request = new PageActionRequestDto
            {
                Id = PageDto.Id,
                Title = PageDto.Title,
                Description = PageDto.Description,
                Content = PageDto.Content,
                IsCustomPage = PageDto.IsCustomPage,
                IsHtmlBasePage = PageDto.IsHtmlBasePage,
                Slug = PageDto.Slug,
                Data = PageDto.Data,
                Indexing = PageDto.Indexing,
                Sections = PageDto.Sections
            };
            await _restWrapper.CrudApiRest<Shipping, Guid>(Address.PageController).Update<PageActionRequestDto>(request, token);
            _snackbar.Add($"ویرایش {PageDto.Title} با موفقیت انجام شد", Severity.Success);
            _mudDialog.Close(DialogResult.Ok(true));
        }
        catch (ApiException ex)
        {
            var exe = await ex.GetContentAsAsync<ApiResult>();
            _snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
            _mudDialog.Cancel();
        }
        catch (Exception e)
        {
            _snackbar.Add(e.Message, Severity.Error);
            _mudDialog.Cancel();
        }
        finally
        {

            IsProcessing = false;
        }
    }

    public async Task SubmitCreateAsync()
    {
        try
        {
            IsProcessing = true;

            var token = await _userUtility.GetBearerTokenAsync();
            if (token == null)
                throw new Exception("Token is null");

            if (PageDto.Title.IsNullOrEmpty())
                throw new Exception("نام را وارد کنید");

            var request = new PageActionRequestDto
            {
                Title = PageDto.Title,
                Description = PageDto.Description,
                Content = PageDto.Content,
                IsCustomPage = PageDto.IsCustomPage,
                IsHtmlBasePage = PageDto.IsHtmlBasePage,
                Slug = PageDto.Slug,
                Data = PageDto.Data,
                Indexing = PageDto.Indexing,
                Sections = PageDto.Sections
            };
            await _restWrapper.CrudApiRest<Shipping, Guid>(Address.PageController).Create<PageActionRequestDto>(request, token);
            _snackbar.Add($"ساخت {PageDto.Title} با موفقیت انجام شد", Severity.Success);
            _mudDialog.Close(DialogResult.Ok(true));
        }
        catch (ApiException ex)
        {
            var exe = await ex.GetContentAsAsync<ApiResult>();
            _snackbar.Add(exe != null ? exe.Message : ex.Content, Severity.Error);
            _mudDialog.Cancel();
        }
        catch (Exception e)
        {
            _snackbar.Add(e.Message, Severity.Error);
            _mudDialog.Cancel();
        }
        finally
        {

            IsProcessing = false;
        }
    }
}
