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

121 lines
4.5 KiB
C#

using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
namespace DDD
{
public class TodayRestaurantStateView : MonoBehaviour, IEventHandler<TodayMenuAddedEvent>, IEventHandler<TodayMenuRemovedEvent>
{
[SerializeField] private Transform _todayWorkerContent;
[SerializeField] private Transform _todayCookwareContent;
private List<ItemSlotUi> _workerSlots;
private List<ItemSlotUi> _cookwareSlots;
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 _todayWorkerContent)
{
Destroy(child.gameObject);
}
int maxCookwareCount = _restaurantManagementSo!.MaxCookwareCount;
_workerSlots = new List<ItemSlotUi>(maxCookwareCount);
for (int i = 0; i < _restaurantManagementSo.MaxCookwareCount; i++)
{
var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayWorkerContent);
var slot = go.GetComponent<ItemSlotUi>();
await slot.Initialize(null, new TodayWorkerSlotUiStrategy());
var itemSlotInteractor = go.GetComponent<ItemSlotInteractor>();
await itemSlotInteractor.Initialize(TodayMenuEventType.Remove, new TodayCookwareInteractorStrategy());
_workerSlots.Add(slot);
}
foreach (Transform child in _todayCookwareContent)
{
Destroy(child.gameObject);
}
_cookwareSlots = new List<ItemSlotUi>(maxCookwareCount);
for (int i = 0; i < _restaurantManagementSo.MaxCookwareCount; i++)
{
var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayCookwareContent);
var slot = go.GetComponent<ItemSlotUi>();
await slot.Initialize(null, new TodayCookwareSlotUiStrategy());
var itemSlotInteractor = go.GetComponent<ItemSlotInteractor>();
await itemSlotInteractor.Initialize(TodayMenuEventType.Remove, new TodayCookwareInteractorStrategy());
_cookwareSlots.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 workerIndex = 0;
foreach (var workerKey in _restaurantManagementSo.TodayWorkerIds)
{
if (workerIndex >= _workerSlots.Count) break;
var model = ItemViewModelFactory.CreateByItemId(workerKey);
var newWorkerSlot = _workerSlots[workerIndex];
_ = newWorkerSlot.Initialize(model, new TodayWorkerSlotUiStrategy());
newWorkerSlot.Model.SetCount(1);
workerIndex++;
}
for (int i = workerIndex; i < _workerSlots.Count; i++)
{
_ = _workerSlots[i].Initialize(null, new TodayWorkerSlotUiStrategy());
}
int cookwareIndex = 0;
foreach (var cookwareKey in _restaurantManagementSo.TodayCookwareIds)
{
if (cookwareIndex >= _cookwareSlots.Count) break;
var model = ItemViewModelFactory.CreateByItemId(cookwareKey);
var newCookwareSlot = _cookwareSlots[cookwareIndex];
_ = newCookwareSlot.Initialize(model, new TodayCookwareSlotUiStrategy());
newCookwareSlot.Model.SetCount(1);
cookwareIndex++;
}
for (int i = cookwareIndex; i < _cookwareSlots.Count; i++)
{
_ = _cookwareSlots[i].Initialize(null, new TodayCookwareSlotUiStrategy());
}
}
}
}