
using System.ComponentModel;
using Front.Program.ViewModels;
using Front.Program.Services;
using Microsoft.AspNetCore.Components;
using System.Net.Http.Json;
using System.Runtime.InteropServices.JavaScript;
using System.Text.Json;
using Front.Program.Models;

namespace Front.Program.Views.Academy.Common;

public partial class TopNavAcademy : ComponentBase, IDisposable
{
    [Inject] UserStateService UserStateService { get; set; } = default!;
    [Inject] NavigationManager NavigationManager { get; set; } = default!;
    
    
    protected bool isOpen = false;
    protected bool isAcademyDropdownOpen = false;
    
    // 계산된 속성으로 변경
    protected Models.SimpleAcademy[] academyItems => UserStateService.academyItems;
    protected string currentAcademyName => UserStateService.CurrentAcademy?.name ?? "학원을 선택하세요";

    protected override void OnInitialized()
    {
        UserStateService.PropertyChanged += OnUserStateServicePropertyChanged;
    }

    public void Dispose()
    {
        UserStateService.PropertyChanged -= OnUserStateServicePropertyChanged;
    }

    private void OnUserStateServicePropertyChanged(object? sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == nameof(UserStateService.CurrentAcademy))
        {
            StateHasChanged();
        }
    }

    protected void ToggleAcademyDropdown() {
        isAcademyDropdownOpen = !isAcademyDropdownOpen;
    }

    private void SelectAcademy(SimpleAcademy academy)
    {
        UserStateService.CurrentAcademy = academy; // 현재 선택된 학원 업데이트
        isAcademyDropdownOpen = false;
        NavigationManager.NavigateTo($"/am/main?bid={academy.bid}");
        
    }

    private void OnClickOutside()
    {
        if (isAcademyDropdownOpen) isAcademyDropdownOpen = false;
        StateHasChanged();
    }
}
