// using Front.Program.Views.Project.Common;

using Front.Program.Models;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;

namespace Front.Program.Services;
// 뷰를 참조 안하고도 로딩 상태를 알 수 있게 바꾸기

public class LoadingService
{
    private readonly IJSRuntime _jsRuntime;
    public bool IsLoading { get; private set; }
    private bool isNavigationLoading { get; set; }

    public event Action? OnChange;

    public void ShowLoading(bool isNavigation = false)
    {
        IsLoading = true;
        isNavigationLoading = isNavigation;
        NotifyStateChanged();
    }

    public void HideLoading()
    {
        if (!isNavigationLoading)
        {
            IsLoading = false;
            NotifyStateChanged();
        }
    }

    public void HideNavigationLoading()
    {
        if (isNavigationLoading)
        {
            IsLoading = false;
            isNavigationLoading = false;
            NotifyStateChanged();
        }
    }

    private void NotifyStateChanged() => OnChange?.Invoke();
    
    
    public LoadingService(IJSRuntime jsRuntime)
    {
        _jsRuntime = jsRuntime;
    }

    public async Task ShowLoadingAsync(bool isNavigation = false)
    {
        IsLoading = true;
        isNavigationLoading = isNavigation;
        NotifyStateChanged();
        await _jsRuntime.InvokeVoidAsync("setBodyOverflowHidden", true);
    }

    public async Task HideLoadingAsync()
    {
        if (!isNavigationLoading)
        {
            IsLoading = false;
            NotifyStateChanged();
            await _jsRuntime.InvokeVoidAsync("setBodyOverflowHidden", false);
        }
    }
    
} 