ProjectDDD/Assets/_DDD/_Scripts/Restaurant/Ui/RestaurantManagementUi/RestaurantManagementUi.cs
2025-08-27 17:25:11 +09:00

295 lines
9.6 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
namespace DDD.Restaurant
{
[RequireComponent(typeof(RestaurantManagementViewModel))]
public class RestaurantManagementUi : PopupUi<RestaurantUiActions, RestaurantManagementViewModel>
{
[Header("Sub Views")]
[SerializeField] private ChecklistView _checklistView;
[SerializeField] private InventoryView _inventoryView;
[SerializeField] private ItemDetailView _itemDetailView;
[SerializeField] private TodayMenuView _todayMenuView;
[SerializeField] private TodayRestaurantStateView _todayRestaurantStateView;
[Header("Tab Groups")]
[SerializeField] private TabGroupUi _sectionTabs;
[SerializeField] private TabGroupUi _menuCategoryTabs;
[SerializeField] private TabGroupUi _cookwareCategoryTabs;
[Header("Hold Progress UI")]
[SerializeField] private Image _completeBatchFilledImage;
private List<IUiView<RestaurantManagementViewModel>> _subViews;
protected override void Update()
{
base.Update();
if (_viewModel)
{
_viewModel.UpdateHoldProgress();
}
}
protected override void OnCreatedInitialize()
{
base.OnCreatedInitialize();
InitializeViews();
InitializeTabGroups();
SetupCategoryTabs();
}
protected override void OnOpenedEvents()
{
base.OnOpenedEvents();
_sectionTabs.SelectFirstTab();
_menuCategoryTabs.SelectFirstTab();
if (_viewModel)
{
_viewModel.OnBatchCompleted += HandleBatchCompleted;
_viewModel.OnChecklistFailed += HandleChecklistFailed;
_viewModel.OnMenuSectionSelected += HandleMenuSectionSelected;
_viewModel.OnCookwareSectionSelected += HandleCookwareSectionSelected;
_viewModel.OnTabMoved += HandleTabMoved;
_viewModel.OnInteractRequested += HandleInteractRequested;
_viewModel.OnCloseRequested += HandleCloseRequested;
_viewModel.OnMenuCategorySelected += HandleMenuCategorySelected;
}
foreach (var view in _subViews)
{
view.OnOpenedEvents();
}
IsInitialized = true;
}
protected override void OnClosedEvents()
{
base.OnClosedEvents();
if (_viewModel)
{
_viewModel.OnBatchCompleted -= HandleBatchCompleted;
_viewModel.OnChecklistFailed -= HandleChecklistFailed;
_viewModel.OnMenuSectionSelected -= HandleMenuSectionSelected;
_viewModel.OnCookwareSectionSelected -= HandleCookwareSectionSelected;
_viewModel.OnTabMoved -= HandleTabMoved;
_viewModel.OnInteractRequested -= HandleInteractRequested;
_viewModel.OnCloseRequested -= HandleCloseRequested;
_viewModel.OnMenuCategorySelected -= HandleMenuCategorySelected;
}
foreach (var view in _subViews)
{
view.OnClosedEvents();
}
}
protected override GameObject GetInitialSelected()
{
if (IsInitialized == false) return null;
var inventoryInitialSelected = _inventoryView.GetInitialSelected();
if (inventoryInitialSelected)
{
return inventoryInitialSelected;
}
var menuCategoryButton = _menuCategoryTabs.GetFirstInteractableButton();
if (menuCategoryButton != null && menuCategoryButton.activeInHierarchy)
{
return menuCategoryButton;
}
var cookwareCategoryButton = _cookwareCategoryTabs.GetFirstInteractableButton();
if (cookwareCategoryButton != null && cookwareCategoryButton.activeInHierarchy)
{
return cookwareCategoryButton;
}
return null;
}
protected override void SetupBindings()
{
base.SetupBindings();
BindingHelper.BindImageFilled(_bindingContext, _completeBatchFilledImage, nameof(RestaurantManagementViewModel.NormalizedHoldProgress));
_itemDetailView.SetupBindings(_bindingContext);
}
private void InitializeViews()
{
_subViews = new List<IUiView<RestaurantManagementViewModel>>
{
_checklistView,
_inventoryView,
_itemDetailView,
_todayMenuView,
_todayRestaurantStateView
};
foreach (var uiView in _subViews)
{
uiView.Initialize(_viewModel);
}
}
private void SetupCategoryTabs()
{
_menuCategoryTabs.UseDefaultAllowedValues();
_cookwareCategoryTabs.UseDefaultAllowedValues();
}
private void InitializeTabGroups()
{
_sectionTabs.Initialize(OnSectionTabSelected);
_menuCategoryTabs.Initialize(OnCategoryTabSelected);
_cookwareCategoryTabs.Initialize(OnCategoryTabSelected);
}
private void UpdateSectionTabs()
{
if (_viewModel == null) return;
_sectionTabs.SelectTab((int)_viewModel.CurrentSection);
}
private void UpdateCategoryTabs()
{
if (_viewModel == null) return;
switch (_viewModel.CurrentSection)
{
case SectionButtonType.Menu:
_menuCategoryTabs.SelectTab((int)_viewModel.CurrentCategory);
break;
case SectionButtonType.Cookware:
_cookwareCategoryTabs.SelectTab((int)_viewModel.CurrentCategory);
break;
}
}
protected override void HandleCustomPropertyChanged(string propertyName)
{
switch (propertyName)
{
case nameof(RestaurantManagementViewModel.CurrentSection):
UpdateSectionTabs();
break;
case nameof(RestaurantManagementViewModel.CurrentCategory):
UpdateCategoryTabs();
break;
}
}
// ViewModel 이벤트 핸들러들
private void HandleBatchCompleted()
{
Close();
}
private void HandleChecklistFailed()
{
var evt = GameEvents.OpenPopupUiEvent;
evt.UiType = typeof(ConfirmUi);
evt.IsCancelButtonVisible = true;
evt.NewMessageKey = "checklist_failed_message";
evt.OnConfirm = ClosePanel;
EventBus.Broadcast(evt);
}
private void HandleMenuSectionSelected()
{
_menuCategoryTabs.SelectFirstTab();
}
private void HandleCookwareSectionSelected()
{
_cookwareCategoryTabs.SelectFirstTab();
}
private void HandleTabMoved(int direction)
{
_sectionTabs.Move(direction);
}
private void HandleInteractRequested()
{
var selected = EventSystem.current.currentSelectedGameObject;
var interactable = selected?.GetComponent<IInteractableUi>();
interactable?.OnInteract();
}
private void HandleCloseRequested()
{
Close();
}
private void HandleMenuCategorySelected(InventoryCategoryType category)
{
_menuCategoryTabs.SelectTab((int)category);
}
// UI 이벤트 핸들러들 (TabGroupUi 콜백)
private void OnSectionTabSelected(int sectionValue)
{
_viewModel?.SetSection((SectionButtonType)sectionValue);
}
private void OnCategoryTabSelected(int categoryValue)
{
_viewModel?.SetCategory((InventoryCategoryType)categoryValue);
}
// 입력 처리 - ViewModel로 위임
protected override bool OnInputPerformed(RestaurantUiActions actionEnum, InputAction.CallbackContext context)
{
if (base.OnInputPerformed(actionEnum, context) == false) return false;
switch (actionEnum)
{
case RestaurantUiActions.Cancel:
_viewModel.CloseUi();
break;
case RestaurantUiActions.PreviousTab:
_viewModel.MoveTab(-1);
break;
case RestaurantUiActions.NextTab:
_viewModel.MoveTab(1);
break;
case RestaurantUiActions.Interact1:
_viewModel.InteractWithSelected();
break;
case RestaurantUiActions.Interact2:
_viewModel.StartHold();
break;
}
return true;
}
protected override bool OnInputCanceled(RestaurantUiActions actionEnum, InputAction.CallbackContext context)
{
if (base.OnInputPerformed(actionEnum, context) == false) return false;
switch (actionEnum)
{
case RestaurantUiActions.Interact2:
_viewModel.CancelHold();
break;
}
return true;
}
}
}