using BrewMonster;
using BrewMonster.Network;
using BrewMonster.Scripts;
using BrewMonster.Scripts.Managers;
using BrewMonster.UI;
using CSNetwork.GPDataType;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace BrewMonster.Scripts.Managers
{
///
/// Warehouse UI (C++ CDlgStorage / Win_Storage). Works with for pack↔storage transfer.
/// Assign storage slot buttons in Inspector (e.g. clone from inventory grid); dialog name: EC_StorageUI.
///
public class EC_StorageUI : AUIDialog
{
[SerializeField] private List storageSlotButtons = new List();
[SerializeField] private Button closeButton;
[SerializeField] private List trashMoneyTextsLegacy = new List();
[SerializeField] private List trashMoneyTextsTMP = new List();
[Header("Storage actions (C++ CDlgStorage)")]
[Tooltip("Gửi tiền vào kho — C++ INPUTNO_STORAGE_IVTR_MONEY (túi → kho).")]
[SerializeField] private Button depositMoneyButton;
[Tooltip("Rút tiền ra — C++ INPUTNO_STORAGE_TRASH_MONEY (kho → túi).")]
[SerializeField] private Button withdrawMoneyButton;
[Tooltip("Sắp xếp kho (IVTRTYPE_TRASHBOX) — C++ CDlgStorage::OnCommand_arrange.")]
[SerializeField] private Button sortPackButton;
private const byte TrashWhere = InventoryConst.IVTRTYPE_TRASHBOX;
private int _selectedTrashSlot = -1;
private static EC_StorageUI s_instance;
private readonly Dictionary _defaultSlotSprites = new Dictionary();
public override void Awake()
{
base.Awake();
s_instance = this;
if (closeButton != null)
{
closeButton.onClick.RemoveAllListeners();
closeButton.onClick.AddListener(() => CECHostPlayer.PopupStorageDialog(true));
}
ResolveStorageActionButtons();
WireStorageActionButtons();
}
private void OnDestroy()
{
if (s_instance == this)
s_instance = null;
}
public static void RefreshAllStatic()
{
s_instance?.RefreshAll();
}
public static void RefreshMoneyStatic()
{
s_instance?.RefreshMoney();
}
public static void ClearSelectionStatic()
{
if (s_instance != null)
s_instance._selectedTrashSlot = -1;
}
public static bool TryGetSelectedTrashSlot(out int slot)
{
slot = s_instance?._selectedTrashSlot ?? -1;
return s_instance != null && slot >= 0;
}
public static Button GetSlotButtonStatic(int slot)
{
if (s_instance == null || slot < 0 || slot >= s_instance.storageSlotButtons.Count)
return null;
return s_instance.storageSlotButtons[slot];
}
public void RefreshAll()
{
var host = CECGameRun.Instance?.GetHostPlayer();
var trash = host?.GetTrashBox();
if (trash == null)
return;
int size = trash.GetSize();
EnsureSlotButtons(size);
for (int i = 0; i < storageSlotButtons.Count; i++)
{
var btn = storageSlotButtons[i];
if (btn == null)
continue;
bool active = i < size;
btn.gameObject.SetActive(active);
if (!active)
continue;
btn.onClick.RemoveAllListeners();
int captured = i;
btn.onClick.AddListener(() => OnStorageSlotClicked(captured));
var item = trash.GetItem(i, false);
ApplySlotIcon(btn, item);
EC_SlotStackCountDisplay.UpdateButton(btn, item);
}
RefreshMoney();
}
void RefreshMoney()
{
var host = CECGameRun.Instance?.GetHostPlayer();
if (host == null)
return;
string text = host.GetTrashBoxMoneyCnt().ToString();
foreach (var t in trashMoneyTextsLegacy)
if (t != null) t.text = text;
foreach (var t in trashMoneyTextsTMP)
if (t != null) t.text = text;
}
void EnsureSlotButtons(int required)
{
if (required <= 0 || storageSlotButtons.Count == 0)
return;
var template = storageSlotButtons[0];
var parent = template.transform.parent;
while (storageSlotButtons.Count < required && template != null && parent != null)
{
var clone = Instantiate(template, parent);
storageSlotButtons.Add(clone);
}
}
void ApplySlotIcon(Button btn, EC_IvtrItem item)
{
var image = GetSlotIconImage(btn);
if (image == null)
return;
if (!_defaultSlotSprites.ContainsKey(image))
_defaultSlotSprites[image] = image.sprite;
bool hasItem = item != null && item.m_iCount > 0;
if (hasItem)
{
image.sprite = EC_IvtrItemUtils.Instance.ResolveItemIconSprite(item.m_tid);
image.enabled = true;
image.color = Color.white;
}
else
{
image.sprite = _defaultSlotSprites[image];
image.enabled = true;
}
}
static Image GetSlotIconImage(Button btn)
{
if (btn == null)
return null;
var icon = btn.transform.Find("Icon");
if (icon != null && icon.TryGetComponent(out var iconImg))
return iconImg;
icon = btn.transform.Find("Img_Icon");
if (icon != null && icon.TryGetComponent(out iconImg))
return iconImg;
return btn.GetComponent();
}
void OnStorageSlotClicked(int slot)
{
var host = CECGameRun.Instance?.GetHostPlayer();
if (host == null || !host.IsUsingTrashBox())
return;
_selectedTrashSlot = -1;
var invUi = UnityEngine.Object.FindFirstObjectByType();
invUi?.ShowItemDetailForPackage(TrashWhere, slot);
}
void ResolveStorageActionButtons()
{
var buttons = GetComponentsInChildren(true);
for (int i = 0; i < buttons.Length; i++)
{
var btn = buttons[i];
if (btn == null || btn == closeButton)
continue;
string n = btn.name.ToLowerInvariant();
if (depositMoneyButton == null && (n.Contains("deposit") || n.Contains("put_money") || n.Contains("gui_tien")
|| n.Contains("send_money") || n == "choosemoney" || n.Contains("btn_put")))
depositMoneyButton = btn;
if (withdrawMoneyButton == null && (n.Contains("withdraw") || n.Contains("get_money") || n.Contains("rut")
|| n.Contains("take_money") || n.Contains("btn_get")))
withdrawMoneyButton = btn;
if (sortPackButton == null && (n.Contains("arrange") || n.Contains("sort") || n.Contains("sap_xep")
|| n == "btn_arrange"))
sortPackButton = btn;
}
}
void WireStorageActionButtons()
{
if (depositMoneyButton != null)
{
depositMoneyButton.onClick.RemoveAllListeners();
depositMoneyButton.onClick.AddListener(OnDepositMoneyClicked);
}
if (withdrawMoneyButton != null)
{
withdrawMoneyButton.onClick.RemoveAllListeners();
withdrawMoneyButton.onClick.AddListener(OnWithdrawMoneyClicked);
}
if (sortPackButton != null)
{
sortPackButton.onClick.RemoveAllListeners();
sortPackButton.onClick.AddListener(OnSortPackClicked);
}
}
/// Gửi tiền vào kho (túi → trash).
void OnDepositMoneyClicked()
{
var host = CECGameRun.Instance?.GetHostPlayer();
if (host == null || !host.IsUsingTrashBox())
return;
int max = (int)Mathf.Min(int.MaxValue, host.GetMoneyAmount());
if (max <= 0)
return;
DlgQuantity.Show(DlgQuantity.QuantityMode.StorageDepositMoney, max, Mathf.Min(1, max));
}
/// Rút tiền ra (trash → túi).
void OnWithdrawMoneyClicked()
{
var host = CECGameRun.Instance?.GetHostPlayer();
if (host == null || !host.IsUsingTrashBox())
return;
int max = host.GetTrashBoxMoneyCnt();
if (max <= 0)
return;
DlgQuantity.Show(DlgQuantity.QuantityMode.StorageWithdrawMoney, max, Mathf.Min(1, max));
}
/// Sắp xếp ô kho (IVTRTYPE_TRASHBOX). C++ CDlgStorage::OnCommand_arrange on Win_Storage.
void OnSortPackClicked()
{
var host = CECGameRun.Instance?.GetHostPlayer();
if (host == null || !host.IsUsingTrashBox())
return;
int cool = host.GetCoolTime((int)CoolTimeIndex.GP_CT_MULTI_EXCHANGE_ITEM, out _);
if (cool > 0)
{
EC_Game.GetGameRun()?.AddFixedMessage((int)FixedMsg.FIXMSG_CMD_INCOOLTIME);
return;
}
host.SortPack(TrashWhere);
RefreshAll();
}
}
}