using System; using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; namespace DDD { public class InventoryView : MonoBehaviour, IEventHandler, IEventHandler, IEventHandler { [SerializeField] private Transform _slotParent; private RestaurantManagementSo _restaurantManagementSo; private InventoryCategoryType _currenInventoryCategoryType = InventoryCategoryType.None; private readonly Dictionary _slotLookup = new(); private GameObject _firstSlot; private const string ItemSlotUiName = "ItemSlotUi_"; private void OnEnable() { EventBus.Register(this); EventBus.Register(this); EventBus.Register(this); } private void OnDisable() { EventBus.Unregister(this); EventBus.Unregister(this); EventBus.Unregister(this); } public GameObject GetInitialSelected() => _firstSlot; public async Task Initialize() { _restaurantManagementSo = await AssetManager.LoadAsset(DataConstants.RestaurantManagementSo); Debug.Assert(_restaurantManagementSo != null, "_todayMenuDataSo != null"); Clear(); var models = ItemViewModelFactory.CreateRestaurantManagementInventoryItem(); _slotLookup.Clear(); foreach (var model in models) { var itemSlotUi = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _slotParent); var slot = itemSlotUi.GetComponent(); slot.Initialize(model, new InventorySlotUiStrategy()); itemSlotUi.name = ItemSlotUiName + model.Id; var interactor = itemSlotUi.GetComponent(); interactor.Initialize(TodayMenuEventType.Add); _slotLookup[model.Id] = slot; } } public void UpdateCategoryView(InventoryCategoryType category) { _currenInventoryCategoryType = category; _firstSlot = null; foreach (var kvp in _slotLookup) { var id = kvp.Key; var slot = kvp.Value; var model = slot.Model; // 1. 오늘의 메뉴에 등록된 경우 필터링 bool isRegisteredTodayMenu = model.ItemType == ItemType.Recipe && _restaurantManagementSo.IsContainTodayMenu(id); // 2. 현재 선택된 카테고리에 맞는지 필터링 bool matchCategory = MatchesCategory(model, _currenInventoryCategoryType); // 3. 조건을 모두 만족할 경우만 활성화 bool shouldShow = !isRegisteredTodayMenu && matchCategory; slot.SetActive(shouldShow); if (shouldShow && _firstSlot == null) { _firstSlot = slot.gameObject; } } } private bool MatchesCategory(ItemViewModel model, InventoryCategoryType category) { switch (category) { case InventoryCategoryType.Food: if (model.ItemType != ItemType.Recipe) return false; return DataManager.Instance.RecipeDataSo.TryGetDataById(model.Id, out var foodRecipe) && foodRecipe.RecipeType == RecipeType.FoodRecipe; case InventoryCategoryType.Drink: if (model.ItemType != ItemType.Recipe) return false; return DataManager.Instance.RecipeDataSo.TryGetDataById(model.Id, out var drinkRecipe) && drinkRecipe.RecipeType == RecipeType.DrinkRecipe; case InventoryCategoryType.Ingredient: return model.ItemType == ItemType.Ingredient; default: return false; } } public void UpdateView() { UpdateCategoryView(_currenInventoryCategoryType); } private void Clear() { foreach (Transform child in _slotParent) { Destroy(child.gameObject); } } public void Invoke(TodayMenuAddedEvent evt) { UpdateView(); } public void Invoke(TodayMenuRemovedEvent evt) { UpdateView(); } public void Invoke(InventoryChangedEvent evt) { foreach (var slot in _slotLookup.Values) { if (slot.Strategy is InventorySlotUiStrategy inventorySlotUiStrategy) { inventorySlotUiStrategy.OnInventoryChanged(slot); } } } } }