using System; using System.Collections.Generic; using Code.Calendar.DataStructures; using Code.Infrastructure.EventBus; using Code.Infrastructure.Loadings; using Code.Infrastructure.UI; using Reflex.Attributes; using UnityEngine; namespace Code.Calendar.Panels { public class PlantsListPanel : UIPanel, IUIPanelLoaded { [SerializeField] private Transform contentParent; private IPlantService _plantService; private IUIService _uiService; private EventBus _eventBus; private List _plantsPanels = new(); private Stack _plantsToCreate; [Inject] private void Construct(EventBus eventBus, IPlantService plantService, IUIService uiService) { _eventBus = eventBus; _plantService = plantService; _uiService = uiService; eventBus.Subscribe(this); } public override void Open(Action callback = null) { _plantsToCreate = new(_plantService.GetPlants()); for (var i = _plantsToCreate.Count; i > 0 ; i--) _uiService.TryLoadCreatePanel(UIPanelType.PlantAsList, contentParent); callback?.Invoke(); } public override void Close(Action callback = null) { ClearPlants(callback); } private Loading _panelClosureLoading; private void ClearPlants(Action callback = null) { if(_panelClosureLoading != null) _panelClosureLoading = new(_plantsPanels.Count + _panelClosureLoading.CurrentProgress, callback); else _panelClosureLoading = new(_plantsPanels.Count, callback); foreach (var plantPanel in _plantsPanels) _uiService.TryRemovePanel(plantPanel); } public void UISinglePanelLoaded(UIPanelType uiPanelType) { } public void UIMultiplePanelLoaded(UIPanelType uiPanelType, UIPanel panel) { if (uiPanelType != UIPanelType.PlantAsList) return; _plantsPanels.Add((PlantAsListElement)panel); ((PlantAsListElement)panel).SetPlant(_plantsToCreate.Pop()); if(_plantsToCreate.Count == 0) PlantPanelsLoaded(); } private void PlantPanelsLoaded() { foreach (var panel in _plantsPanels) { panel.Open(); } Debug.LogWarning("PlantPanelsLoaded"); } } }