using Force.DeepCloner;

namespace Netina.AdminPanel.PWA.Pages;

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

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

    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.ReadAll(token);
            PageDto.Clear();
            pages.ForEach(p=>PageDto.Add(p));
        }
        catch (ApiException e)
        {
            var exe = await e.GetContentAsAsync<ApiResult>();
            if (e.StatusCode == HttpStatusCode.Unauthorized)
            {
                await _userUtility.LogoutAsync();
                _navigationManager.NavigateTo("login", true, true);
            }
            _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 PageActionRequestDto NewPageDto { get; set; } = new();

    public async Task AddPageAsync()
    {
        try
        {
            IsProcessing = true;
            var token = await _userUtility.GetBearerTokenAsync();
            if (token == null)
                throw new Exception("Token is null");

            if (NewPageDto.Title.IsNullOrEmpty())
                throw new AppException("عنوان صفحه را وارد کنید");
            if (NewPageDto.Slug.IsNullOrEmpty())
                throw new AppException("اسلاگ صفحه را وارد کنید");

            await _restWrapper.PageRestApi.CreatePage(NewPageDto.DeepClone(), token);
            NewPageDto = new PageActionRequestDto();
            _snackbar.Add("برگه مورد نظر با موفقیت افزوده شد", Severity.Success);
            await InitializeAsync();

        }
        catch (ApiException e)
        {
            var exe = await e.GetContentAsAsync<ApiResult>();
            if (e.StatusCode == HttpStatusCode.Unauthorized)
            {
                await _userUtility.LogoutAsync();
                _navigationManager.NavigateTo("login", true, true);
            }
            _snackbar.Add(exe != null ? exe.Message : e.Content, Severity.Error);
        }
        catch (Exception ex)
        {
            _snackbar.Add(ex.Message, Severity.Error);
        }
        finally
        {
            IsProcessing = false;
        }
    }

    public async Task RemovePageAsync(Guid pageId)
    {
        try
        {
            IsProcessing = true;
            var token = await _userUtility.GetBearerTokenAsync();
            if (token == null)
                throw new Exception("Token is null");

            await _restWrapper.PageRestApi.DeletePage(pageId, token);
            _snackbar.Add("برگه مورد نظر با موفقیت حذف شد", Severity.Success);
            await InitializeAsync();


        }
        catch (ApiException e)
        {
            var exe = await e.GetContentAsAsync<ApiResult>();
            if (e.StatusCode == HttpStatusCode.Unauthorized)
            {
                await _userUtility.LogoutAsync();
                _navigationManager.NavigateTo("login", true, true);
            }
            _snackbar.Add(exe != null ? exe.Message : e.Content, Severity.Error);
        }
        catch (Exception ex)
        {
            _snackbar.Add(ex.Message, Severity.Error);
        }
        finally
        {
            IsProcessing = false;
        }
    }

}