using BrewMonster.Managers;
using BrewMonster.Network;
using BrewMonster.Scripts;
using BrewMonster.Scripts.Managers;
using CSNetwork;
using CSNetwork.GPDataType;
using BrewMonster.Scripts.Managers;
using BrewMonster.UI;
using System;
using UnityEngine;
using static BrewMonster.Scripts.EC_Inventory;
namespace BrewMonster
{
///
/// Host player warehouse / trash box (C++ CECHostPlayer::OnMsgHstTrashBoxOperation, GetTrashBox*).
///
public partial class CECHostPlayer
{
private readonly EC_Inventory m_pTrashBoxPack = new EC_Inventory();
private readonly EC_Inventory m_pTrashBoxPack2 = new EC_Inventory();
private readonly EC_Inventory m_pTrashBoxPack3 = new EC_Inventory();
private bool m_bTrashPsw;
private bool m_bFirstTBOpen = true;
private bool m_bUsingAccountBox;
private bool m_bFirstAccountBoxOpen = true;
private int m_iTrashBoxMoneyCnt;
private int m_iAccountBoxMoneyCnt;
public bool TrashBoxHasPsw() => m_bTrashPsw;
public int GetTrashBoxMoneyCnt() => m_iTrashBoxMoneyCnt;
public bool IsUsingAccountBox() => m_bUsingAccountBox;
public int GetAccountBoxMoneyCnt() => m_iAccountBoxMoneyCnt;
public EC_Inventory GetTrashBox() => m_pTrashBoxPack;
public EC_Inventory GetTrashBox2() => m_pTrashBoxPack2;
public EC_Inventory GetTrashBox3() => m_pTrashBoxPack3;
void InitTrashBoxPacks()
{
if (m_pTrashBoxPack.GetSize() == 0)
m_pTrashBoxPack.Init(InventoryConst.IVTRSIZE_TRASHBOX);
if (m_pTrashBoxPack2.GetSize() == 0)
m_pTrashBoxPack2.Init(0);
if (m_pTrashBoxPack3.GetSize() == 0)
m_pTrashBoxPack3.Init(0);
}
public void OnMsgHstTrashBoxOperation(ECMSG Msg)
{
var data = Msg.dwParam1 as byte[];
if (data == null || data.Length == 0)
return;
int cmd = Convert.ToInt32(Msg.dwParam2);
switch (cmd)
{
case CommandID.TRASHBOX_OPEN:
{
var pCmd = GPDataTypeHelper.FromBytes(data);
if (pCmd.is_accountbox != 0)
{
m_bUsingAccountBox = true;
m_bUsingTrashBox = false;
if (m_bFirstAccountBoxOpen)
{
m_bFirstAccountBoxOpen = false;
UnityGameSession.c2s_CmdGetTrashBoxData(true, true);
}
PopupAccountBoxDialog();
break;
}
m_bUsingTrashBox = true;
m_bUsingAccountBox = false;
InitTrashBoxPacks();
if (m_bFirstTBOpen)
{
m_bFirstTBOpen = false;
UnityGameSession.c2s_CmdGetTrashBoxData(true);
}
else
{
if (m_pTrashBoxPack.GetSize() < pCmd.slot_size)
m_pTrashBoxPack.Resize(pCmd.slot_size);
if (m_pTrashBoxPack2.GetSize() < pCmd.slot_size2)
m_pTrashBoxPack2.Resize(pCmd.slot_size2);
if (m_pTrashBoxPack3.GetSize() < pCmd.slot_size3)
m_pTrashBoxPack3.Resize(pCmd.slot_size3);
PopupStorageDialog();
}
break;
}
case CommandID.TRASHBOX_CLOSE:
{
var pCmd = GPDataTypeHelper.FromBytes(data);
if (pCmd.is_accountbox != 0)
{
m_bUsingAccountBox = false;
PopupAccountBoxDialog(true);
break;
}
m_bUsingTrashBox = false;
PopupStorageDialog(true);
break;
}
case CommandID.TRASHBOX_WEALTH:
{
var pCmd = GPDataTypeHelper.FromBytes(data);
if (pCmd.is_accountbox == 0)
{
m_iTrashBoxMoneyCnt = (int)pCmd.money;
EC_StorageUI.RefreshMoneyStatic();
}
else
{
m_iAccountBoxMoneyCnt = (int)pCmd.money;
EC_AccountStorageUI.RefreshMoneyStatic();
}
break;
}
case CommandID.EXG_TRASH_MONEY:
{
var pCmd = GPDataTypeHelper.FromBytes(data);
AddMoneyAmount(pCmd.inv_delta);
if (pCmd.is_accountbox == 0)
{
m_iTrashBoxMoneyCnt += pCmd.tra_delta;
}
else
{
m_iAccountBoxMoneyCnt += pCmd.tra_delta;
}
EC_StorageUI.RefreshMoneyStatic();
EC_AccountStorageUI.RefreshMoneyStatic();
var invUi = UnityEngine.Object.FindFirstObjectByType();
invUi?.RefreshAll();
break;
}
case CommandID.EXG_TRASHBOX_ITEM:
{
var pCmd = GPDataTypeHelper.FromBytes(data);
var pTrash = GetPack(pCmd.where);
pTrash?.ExchangeItem(pCmd.idx1, pCmd.idx2);
EC_StorageUI.RefreshAllStatic();
break;
}
case CommandID.MOVE_TRASHBOX_ITEM:
{
var pCmd = GPDataTypeHelper.FromBytes(data);
var pTrash = GetPack(pCmd.where);
pTrash?.MoveItem(pCmd.src, pCmd.dest, (int)pCmd.amount);
EC_StorageUI.RefreshAllStatic();
break;
}
case CommandID.EXG_TRASHBOX_IVTR:
{
var pCmd = GPDataTypeHelper.FromBytes(data);
var pTrash = GetPack(pCmd.where);
if (pTrash == null || m_pPack == null)
break;
var pItem1 = m_pPack.GetItem(pCmd.idx_inv, true);
var pItem2 = pTrash.GetItem(pCmd.idx_tra, true);
m_pPack.SetItem(pCmd.idx_inv, pItem2);
pTrash.SetItem(pCmd.idx_tra, pItem1);
RefreshStorageAndInventoryUi();
break;
}
case CommandID.IVTR_ITEM_TO_TRASH:
{
var pCmd = GPDataTypeHelper.FromBytes(data);
var pTrash = GetPack(pCmd.where);
if (pTrash == null || m_pPack == null)
break;
var pItem1 = m_pPack.GetItem(pCmd.src);
var pItem2 = pTrash.GetItem(pCmd.dest);
if (pItem1 == null)
break;
if (pItem2 != null)
{
pItem2.AddAmount((int)pCmd.amount);
}
else
{
pItem2 = EC_IvtrItem.CreateItem(pItem1.m_tid, pItem1.m_expire_date, (int)pCmd.amount);
pTrash.SetItem(pCmd.dest, pItem2);
}
m_pPack.RemoveItem(pCmd.src, (int)pCmd.amount);
RefreshStorageAndInventoryUi();
break;
}
case CommandID.TRASH_ITEM_TO_IVTR:
{
var pCmd = GPDataTypeHelper.FromBytes(data);
var pTrash = GetPack(pCmd.where);
if (pTrash == null || m_pPack == null)
break;
var pItem1 = pTrash.GetItem(pCmd.src);
var pItem2 = m_pPack.GetItem(pCmd.dest);
if (pItem1 == null)
break;
if (pItem2 != null)
pItem2.AddAmount((int)pCmd.amount);
else
{
pItem2 = EC_IvtrItem.CreateItem(pItem1.m_tid, pItem1.m_expire_date, (int)pCmd.amount);
m_pPack.SetItem(pCmd.dest, pItem2);
}
pTrash.RemoveItem(pCmd.src, (int)pCmd.amount);
RefreshStorageAndInventoryUi();
break;
}
case CommandID.TRASHBOX_PWD_CHANGED:
case CommandID.TRASHBOX_PWD_STATE:
{
var pCmd = GPDataTypeHelper.FromBytes(data);
m_bTrashPsw = pCmd.has_passwd > 0;
break;
}
case CommandID.TRASHBOX_SIZE:
{
var pCmd = GPDataTypeHelper.FromBytes(data);
int msg = (int)FixedMsg.FIXMSG_TRASHBOX_EXPAND;
int iSize = pCmd.iNewSize;
if (pCmd.where == Inventory_type.IVTRTYPE_TRASHBOX2)
msg = (int)FixedMsg.FIXMSG_TRASHBOX2_EXPAND;
else if (pCmd.where == Inventory_type.IVTRTYPE_TRASHBOX3)
{
msg = (int)FixedMsg.FIXMSG_TRASHBOX3_EXPAND;
m_pTrashBoxPack3?.Resize(pCmd.iNewSize);
}
else if (pCmd.where == Inventory_type.IVTRTYPE_TRASHBOX)
m_pTrashBoxPack?.Resize(pCmd.iNewSize);
EC_Game.GetGameRun()?.AddFixedMessage(msg, iSize);
EC_StorageUI.RefreshAllStatic();
break;
}
}
}
static void RefreshStorageAndInventoryUi()
{
EC_StorageUI.RefreshAllStatic();
UnityEngine.Object.FindFirstObjectByType()?.RefreshAll();
}
/// C++ CECGameUIMan::PopupStorageDialog — show warehouse + inventory.
public static void PopupStorageDialog(bool close = false)
{
if (close)
{
var host = EC_Game.GetGameRun()?.GetHostPlayer();
bool wasUsingTrash = host != null && host.IsUsingTrashBox();
if (host != null)
host.m_bUsingTrashBox = false;
EC_StorageUI.ClearSelectionStatic();
var invUi = UnityEngine.Object.FindFirstObjectByType(FindObjectsInactive.Include);
invUi?.DismissItemDetail();
CECUIManager.Instance?.HideStorageDialogPair();
if (wasUsingTrash)
UnityGameSession.c2s_CmdCancelAction();
EC_Game.GetGameRun()?.GetUIManager()?.GetInGameUIMan()?.EndNPCService();
return;
}
CECUIManager.Instance?.ShowStorageDialogPair();
var storageDlg = EC_Game.GetGameRun()?.GetUIManager()?.GetInGameUIMan()?.GetDialog("EC_StorageUI") as EC_StorageUI;
storageDlg?.RefreshAll();
var invDlg = UnityEngine.Object.FindFirstObjectByType(FindObjectsInactive.Include);
invDlg?.RefreshAll();
}
/// C++ CECGameUIMan::PopupAccountBoxDialog — money-only account warehouse.
public static void PopupAccountBoxDialog(bool close = false)
{
if (close)
{
var host = EC_Game.GetGameRun()?.GetHostPlayer();
if (host != null)
host.m_bUsingAccountBox = false;
EC_StorageUI.ClearSelectionStatic();
var invUi = UnityEngine.Object.FindFirstObjectByType(FindObjectsInactive.Include);
invUi?.DismissItemDetail();
CECUIManager.Instance?.HideAccountStorageDialogPair();
EC_Game.GetGameRun()?.GetUIManager()?.GetInGameUIMan()?.EndNPCService();
return;
}
CECUIManager.Instance?.ShowAccountStorageDialogPair();
var accountDlg = EC_Game.GetGameRun()?.GetUIManager()?.GetInGameUIMan()?.GetDialog("EC_AccountStorageUI") as EC_AccountStorageUI;
accountDlg?.RefreshAll();
var invDlg = UnityEngine.Object.FindFirstObjectByType(FindObjectsInactive.Include);
invDlg?.RefreshAll();
}
/// Transfer between main pack and trash box (C++ CDlgStorage::OnItemDragDrop).
public bool TransferPackAndTrash(byte trashWhere, int trashSlot, int invSlot, int amount = -1)
{
if (trashWhere != InventoryConst.IVTRTYPE_TRASHBOX)
return false;
var trash = GetPack(trashWhere);
var pack = m_pPack;
if (trash == null || pack == null)
return false;
if (trashSlot < 0 || invSlot < 0)
return false;
var srcInv = pack.GetItem(invSlot);
var dstTrash = trash.GetItem(trashSlot);
if (srcInv != null && dstTrash == null)
{
UnityGameSession.c2s_CmdExgTrashBoxIvtrItem(trashWhere, (byte)trashSlot, (byte)invSlot);
return true;
}
var srcTrash = trash.GetItem(trashSlot);
var dstInv = pack.GetItem(invSlot);
if (srcTrash != null && dstInv == null)
{
UnityGameSession.c2s_CmdExgTrashBoxIvtrItem(trashWhere, (byte)trashSlot, (byte)invSlot);
return true;
}
if (srcInv != null && dstTrash != null &&
srcInv.m_tid == dstTrash.m_tid && srcInv.GetPileLimitInstance() > 1)
{
int moveAmt = amount > 0 ? amount : Math.Min(srcInv.m_iCount,
dstTrash.GetPileLimitInstance() - dstTrash.m_iCount);
if (moveAmt > 0)
{
UnityGameSession.c2s_CmdMoveIvtrToTrashBox(trashWhere, (byte)invSlot, (byte)trashSlot, (uint)moveAmt);
return true;
}
}
if (srcTrash != null && dstInv != null &&
srcTrash.m_tid == dstInv.m_tid && srcTrash.GetPileLimitInstance() > 1)
{
int moveAmt = amount > 0 ? amount : Math.Min(srcTrash.m_iCount,
dstInv.GetPileLimitInstance() - dstInv.m_iCount);
if (moveAmt > 0)
{
UnityGameSession.c2s_CmdMoveTrashBoxToIvtr(trashWhere, (byte)trashSlot, (byte)invSlot, (uint)moveAmt);
return true;
}
}
if (srcInv != null || srcTrash != null)
{
UnityGameSession.c2s_CmdExgTrashBoxIvtrItem(trashWhere, (byte)trashSlot, (byte)invSlot);
return true;
}
return false;
}
public bool TransferWithinTrash(byte where, int slotA, int slotB)
{
var trash = GetPack(where);
if (trash == null || slotA < 0 || slotB < 0 || slotA == slotB)
return false;
var a = trash.GetItem(slotA);
var b = trash.GetItem(slotB);
if (a != null && b != null && a.m_tid == b.m_tid && a.GetPileLimitInstance() > 1)
{
int moveAmt = Math.Min(a.m_iCount, b.GetPileLimitInstance() - b.m_iCount);
if (moveAmt > 0)
{
UnityGameSession.c2s_CmdMoveTrashBoxItem(where, (byte)slotA, (byte)slotB, (uint)moveAmt);
return true;
}
}
UnityGameSession.c2s_CmdExgTrashBoxItem(where, (byte)slotA, (byte)slotB);
return true;
}
}
}