ProjectDDD/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuView.cs
2025-08-04 08:09:01 +09:00

122 lines
4.6 KiB
C#

using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
namespace DDD
{
public class TodayMenuView : MonoBehaviour, IEventHandler<TodayMenuAddedEvent>, IEventHandler<TodayMenuRemovedEvent>
{
[SerializeField] private Transform _todayFoodContent;
[SerializeField] private Transform _todayDrinkContent;
private List<ItemSlotUi> _foodSlots;
private List<ItemSlotUi> _drinkSlots;
private RestaurantManagementSo _restaurantManagementSo;
private void Start()
{
_ = Initialize();
}
private void OnDestroy()
{
EventBus.Unregister<TodayMenuAddedEvent>(this);
EventBus.Unregister<TodayMenuRemovedEvent>(this);
}
private async Task Initialize()
{
_restaurantManagementSo = await AssetManager.LoadAsset<RestaurantManagementSo>(DataConstants.RestaurantManagementSo);
Debug.Assert(_restaurantManagementSo != null, "_restaurantManagementSo != null");
foreach (Transform child in _todayFoodContent)
{
Destroy(child.gameObject);
}
int maxFoodCount = _restaurantManagementSo!.MaxFoodCount;
_foodSlots = new List<ItemSlotUi>(maxFoodCount);
for (int i = 0; i < _restaurantManagementSo.MaxFoodCount; i++)
{
var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayFoodContent);
var slot = go.GetComponent<ItemSlotUi>();
await slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe));
var itemSlotInteractor = go.GetComponent<ItemSlotInteractor>();
await itemSlotInteractor.Initialize(TodayMenuEventType.Remove, new TodayMenuInteractorStrategy());
_foodSlots.Add(slot);
}
foreach (Transform child in _todayDrinkContent)
{
Destroy(child.gameObject);
}
int maxDrinkCount = _restaurantManagementSo.MaxDrinkCount;
_drinkSlots = new List<ItemSlotUi>(maxDrinkCount);
for (int i = 0; i < _restaurantManagementSo.MaxDrinkCount; i++)
{
var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayDrinkContent);
var slot = go.GetComponent<ItemSlotUi>();
await slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe));
var itemSlotInteractor = go.GetComponent<ItemSlotInteractor>();
await itemSlotInteractor.Initialize(TodayMenuEventType.Remove, new TodayMenuInteractorStrategy());
_drinkSlots.Add(slot);
}
UpdateView();
EventBus.Register<TodayMenuAddedEvent>(this);
EventBus.Register<TodayMenuRemovedEvent>(this);
}
public void Invoke(TodayMenuAddedEvent evt)
{
UpdateView();
}
public void Invoke(TodayMenuRemovedEvent evt)
{
UpdateView();
}
private void UpdateView()
{
int foodIndex = 0;
foreach (var foodRecipeIdCountPair in _restaurantManagementSo.TodayFoodRecipeIds)
{
if (foodIndex >= _foodSlots.Count) break;
var model = ItemViewModelFactory.CreateByItemId(foodRecipeIdCountPair.Key);
var foodSlot = _foodSlots[foodIndex];
_ = foodSlot.Initialize(model, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe));
foodSlot.Model.SetCount(foodRecipeIdCountPair.Value);
foodIndex++;
}
for (int i = foodIndex; i < _foodSlots.Count; i++)
{
_ = _foodSlots[i].Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe));
}
int drinkIndex = 0;
foreach (var drinkRecipeIdCountPair in _restaurantManagementSo.TodayDrinkRecipeIds)
{
if (drinkIndex >= _drinkSlots.Count) break;
var model = ItemViewModelFactory.CreateByItemId(drinkRecipeIdCountPair.Key);
var drinkSlot = _drinkSlots[drinkIndex];
_ = drinkSlot.Initialize(model, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe));
drinkSlot.Model.SetCount(drinkRecipeIdCountPair.Value);
drinkIndex++;
}
for (int i = drinkIndex; i < _drinkSlots.Count; i++)
{
_ = _drinkSlots[i].Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe));
}
}
}
}