local STYLE_NAME     = 'game_stash'
local TOGGLE_HOTKEY  = 'Ctrl+Shift+Y'

-- State
local stashWindow    = nil
local toggleContext  = nil
local testPopup      = nil   -- <— keep the popup here (not local to init)

-- Stubs used by OTUI
function renderItems() end

function onSupplyStashClose()
  -- called by the Close button in OTUI
  if testPopup then testPopup:destroy(); testPopup = nil end
  if stashWindow then stashWindow:hide() end
  local rootPanel = modules and modules.game_interface and modules.game_interface.getRootPanel and modules.game_interface.getRootPanel()
  if rootPanel then rootPanel:focus() end
end

-- Helpers
local function showStash()
  if stashWindow then stashWindow:show() end
end

local function hideStash()
  if stashWindow then stashWindow:hide() end
end

local function toggleStash()
  if not stashWindow then return end
  if stashWindow:isHidden() then showStash() else hideStash() end
end

-- Lifecycle
function init()
  local ok, err = pcall(function() g_ui.importStyle(STYLE_NAME) end)
  if not ok then
    print(string.format('[Suppliy Stash] ERROR importing style "%s": %s', STYLE_NAME, tostring(err)))
    return
  end
  if not rootWidget then
    print('[Suppliy Stash] ERROR: rootWidget missing.')
    return
  end

  stashWindow = g_ui.createWidget('StashWindow', rootWidget)
  if not stashWindow then
    print('[Suppliy Stash] ERROR: createWidget("StashWindow") failed.')
    return
  end

  -- optional lookups
  stashWindow:recursiveGetChildById('stashItemsPanel')
  stashWindow:recursiveGetChildById('stashSearchEdit')

  showStash()

  toggleContext = modules and modules.game_interface and modules.game_interface.getRootPanel and modules.game_interface.getRootPanel() or rootWidget
  if toggleContext then
    g_keyboard.bindKeyDown(TOGGLE_HOTKEY, toggleStash, toggleContext)
    print(string.format('[Suppliy Stash] Toggle with %s', TOGGLE_HOTKEY))
  end

  -- Create the test withdraw popup and keep it in 'testPopup'
  SupplyStashWithdrawWindow = g_ui.createWidget('StashSelectAmount', rootWidget)
  if SupplyStashWithdrawWindow then
    SupplyStashWithdrawWindow:show()
    local it = SupplyStashWithdrawWindow:getChildById('stashSelectItem')
    if it then
      it:setItemId(3031)   -- example item
      it:setItemCount(42)
    end
  end

  -- Ensure popup is cleaned up if the main window is destroyed
  stashWindow.onDestroy = function()
    if SupplyStashWithdrawWindow then SupplyStashWithdrawWindow:destroy(); SupplyStashWithdrawWindow = nil end
  end

  print('[Suppliy Stash] Initialized.')
end

function terminate()
	if toggleContext then
		g_keyboard.unbindKeyDown(TOGGLE_HOTKEY, toggleContext)
		toggleContext = nil
	end
	if SupplyStashWithdrawWindow then
		SupplyStashWithdrawWindow:destroy()
		SupplyStashWithdrawWindow = nil
	end
	if stashWindow then
		stashWindow:destroy()
		stashWindow = nil
	end
	print('[Suppliy Stash] Terminated.')
end

function onWithdrawClose()
	if SupplyStashWithdrawWindow then
		SupplyStashWithdrawWindow:destroy()
		SupplyStashWithdrawWindow = nil
		print('[Suppliy Stash] Withdraw window closed.')
	end
end

function hide()
	if stashWindow then
		print("[Stash] hide() called") 
		stashWindow:unlock()
		stashWindow:hide()
		if SupplyStashWithdrawWindow then
			SupplyStashWithdrawWindow:destroy()
			SupplyStashWithdrawWindow = nil
			print('[Suppliy Stash] Withdraw window closed.')
		end
	end
end
