using System.Linq;
using DDD.MVVM;
using UnityEngine;
namespace DDD
{
///
/// 레스토랑 관리 UI의 ViewModel
/// 기존 RestaurantManagementUi의 상태 관리와 비즈니스 로직을 담당
///
public class RestaurantManagementViewModel : SimpleViewModel, IEventHandler
{
// 홀드 진행 상태 관리
private bool _isHolding;
private float _elapsedTime;
private float _holdCompleteTime = 1f;
// 탭 상태 관리
private SectionButtonType _currentSection = SectionButtonType.Menu;
private InventoryCategoryType _currentCategory = InventoryCategoryType.Food;
///
/// 현재 홀드 상태
///
public bool IsHolding
{
get => _isHolding;
private set => SetField(ref _isHolding, value);
}
///
/// 홀드 진행 시간 (0.0 ~ 1.0)
///
public float HoldProgress
{
get => _elapsedTime;
private set => SetField(ref _elapsedTime, value);
}
///
/// 홀드 완료에 필요한 시간
///
public float HoldCompleteTime
{
get => _holdCompleteTime;
set => SetField(ref _holdCompleteTime, value);
}
///
/// 현재 선택된 섹션
///
public SectionButtonType CurrentSection
{
get => _currentSection;
set => SetField(ref _currentSection, value);
}
///
/// 현재 선택된 카테고리
///
public InventoryCategoryType CurrentCategory
{
get => _currentCategory;
set => SetField(ref _currentCategory, value);
}
///
/// 배치 완료 가능 여부 (체크리스트 완료 상태)
///
public bool CanCompleteBatch =>
RestaurantState.Instance.ManagementState.GetChecklistStates().All(state => state);
///
/// 홀드 진행률을 0~1 범위로 변환한 값
///
public float NormalizedHoldProgress => HoldCompleteTime <= 0f ? 1f : Mathf.Clamp01(HoldProgress / HoldCompleteTime);
public override void Initialize()
{
base.Initialize();
RegisterEvents();
ResetHoldState();
}
public override void Cleanup()
{
base.Cleanup();
UnregisterEvents();
}
private void RegisterEvents()
{
EventBus.Register(this);
}
private void UnregisterEvents()
{
EventBus.Unregister(this);
}
///
/// 홀드 진행 업데이트 (View에서 Update마다 호출)
///
public void UpdateHoldProgress()
{
if (!IsHolding) return;
if (HoldCompleteTime <= 0f)
{
ProcessCompleteBatch();
return;
}
var deltaTime = Time.deltaTime;
HoldProgress += deltaTime;
if (HoldProgress >= HoldCompleteTime)
{
ProcessCompleteBatch();
}
// UI 업데이트를 위한 정규화된 진행률 알림
OnPropertyChanged(nameof(NormalizedHoldProgress));
}
///
/// 홀드 시작
///
public void StartHold()
{
IsHolding = true;
HoldProgress = 0f;
OnPropertyChanged(nameof(NormalizedHoldProgress));
}
///
/// 홀드 취소
///
public void CancelHold()
{
ResetHoldState();
}
private void ResetHoldState()
{
IsHolding = false;
HoldProgress = 0f;
OnPropertyChanged(nameof(NormalizedHoldProgress));
}
///
/// 배치 완료 처리
///
private void ProcessCompleteBatch()
{
ResetHoldState();
if (CanCompleteBatch)
{
// 배치 완료 - UI 닫기 이벤트 발생
OnBatchCompleted?.Invoke();
}
else
{
// 체크리스트 미완료 - 실패 팝업 표시 이벤트 발생
OnChecklistFailed?.Invoke();
}
}
///
/// 섹션 탭 선택 처리
///
public void SetSection(SectionButtonType section)
{
CurrentSection = section;
// 섹션 변경 시 해당 섹션의 첫 번째 카테고리로 설정
switch (section)
{
case SectionButtonType.Menu:
OnMenuSectionSelected?.Invoke();
break;
case SectionButtonType.Cookware:
OnCookwareSectionSelected?.Invoke();
break;
}
}
///
/// 카테고리 탭 선택 처리
///
public void SetCategory(InventoryCategoryType category)
{
CurrentCategory = category;
OnCategoryChanged?.Invoke(category);
}
///
/// 탭 이동 (이전/다음)
///
public void MoveTab(int direction)
{
OnTabMoved?.Invoke(direction);
}
///
/// 현재 선택된 UI 요소와 상호작용
///
public void InteractWithSelected()
{
OnInteractRequested?.Invoke();
}
///
/// UI 닫기
///
public void CloseUi()
{
OnCloseRequested?.Invoke();
}
// View에서 구독할 이벤트들
public System.Action OnBatchCompleted;
public System.Action OnChecklistFailed;
public System.Action OnMenuSectionSelected;
public System.Action OnCookwareSectionSelected;
public System.Action OnCategoryChanged;
public System.Action OnTabMoved;
public System.Action OnInteractRequested;
public System.Action OnCloseRequested;
// 이벤트 핸들러
public void Invoke(TodayMenuRemovedEvent evt)
{
SetCategory(evt.InventoryCategoryType);
OnMenuCategorySelected?.Invoke(evt.InventoryCategoryType);
}
public System.Action OnMenuCategorySelected;
}
}