dofile( "mods/spellbound_bundle/files/scripts/utils.lua" )
dofile( "mods/spellbound_bundle/files/scripts/gui_utils.lua" )
dofile_once("mods/spellbound_bundle/files/scripts/coroutines.lua")
dofile( "data/scripts/gun/gun_actions.lua" )
local card = GetUpdatedEntityID()

gui = gui or GuiCreate()

GuiStartFrame( gui )
local mx, my = GuiGetMousePosition(gui)

current_id = 3261524

local this = GetUpdatedComponentID()

function new_id()
    current_id = current_id + 1
    return current_id
end

function get_inventory_position(entity)
	local item_component = EntityGetFirstComponentIncludingDisabled(entity, "ItemComponent")
	return ComponentGetValue2(item_component, "inventory_slot")
end

function get_wand_sprite_size(gui, entity)
	local ability_component = EntityGetFirstComponentIncludingDisabled(entity, "AbilityComponent")
	local sprite = ComponentGetValue2(ability_component, "sprite_file")

	return GuiGetImageDimensions( gui, sprite )
end


function is_inventory_open()
	local player = EntityGetWithTag("player_unit")[1]
	if player then
		local inventory_gui_component = EntityGetFirstComponentIncludingDisabled(player, "InventoryGuiComponent")
		if inventory_gui_component then
			return ComponentGetValue2(inventory_gui_component, "mActive")
		end
	end
	return false
end

function inventory_ui_position()
	local player = EntityGetWithTag("player_unit")[1]
	if player then
		local inventory_component = EntityGetFirstComponentIncludingDisabled(player, "InventoryComponent")
		if inventory_component then
			return ComponentGetValue2(inventory_component, "ui_position_on_screen")
		end
	end
	return nil
end


if(is_inventory_open())then
	local parent = EntityGetParent( card )

	if(parent ~= nil and parent ~= 0)then
		local slot_x, slot_y = get_inventory_position(card)
		if(EntityGetName(parent) == "inventory_full")then
			-- Is in the spell inventory
			local offset_x = 191
			local offset_y = 22

			local extra_offset = 16

			local slot_width, slot_height = 16, 16
			local slot_margin = 4

			--GamePrint("yo")

			x = offset_x + (slot_x * slot_width) + (slot_x * slot_margin)
			y = offset_y + (slot_y * slot_height) + (slot_y * slot_margin)

			if(mx > x - 1 and mx < x + slot_width + 3 and my > y - 1 and my < y + slot_height + 3)then

				local stored_spells = EntityGetVariable(card, "storedspells", "string")

				local stored_actions = {}


				for spell_id in string.gmatch(stored_spells, '([^,]+)') do
					for k, v in pairs(actions)do
						if(v.id == spell_id)then
							table.insert(stored_actions, v.sprite)
						end
					end
				end
				GuiZSetForNextWidget( gui, -1000 )
				GuiBeginScrollContainer( gui, new_id(), x - 95, y + 110, 201, 120 )
				Grid(gui, {items=stored_actions, x= 0, y= 0, size=10, padding_y = 18, ui_Scale = true}, function(item) 
					GuiZSetForNextWidget( gui, -1100 )
					GuiImage( gui, new_id(), 0, 0, item, 1,1,1)
				end)
				GuiEndScrollContainer( gui )


			end
			
			--GuiImage( gui, new_id(), x, y, "mods/spellbound_bundle/files/gfx/magic_effect.png", 1,1,1)


		else

			local parent_slot_x, parent_slot_y = get_inventory_position(parent)

			--GamePrint(tostring(parent_slot_x))

			local wands = EntityGetAllChildren( EntityGetParent(parent) )

			if(parent_slot_x ~= nil)then

				local extra_offset = 0

				for k, v in pairs(wands)do
					wand_x, wand_y = get_inventory_position(v)

					im_x, im_y = get_wand_sprite_size(gui, v)

					if(wand_x <= parent_slot_x)then
						if(im_y > 8)then
							extra_offset = extra_offset + ((im_y - 8) * 2)
						end
					end
				end

				local offset_x = 24
				local offset_y = 80 + (parent_slot_x * 63) + extra_offset

				local slot_width, slot_height = 16, 16
				local slot_margin = 4

				x = offset_x + (slot_x * slot_width) + (slot_x * slot_margin)
				y = offset_y + (slot_y * slot_height) + (slot_y * slot_margin)

				--GuiImage( gui, new_id(), x, y, "data/ui_gfx/debug_marker.png", 1,1,1)

				--GuiImage( gui, new_id(), x, y, "data/ui_gfx/debug_marker.png", 1,1,1)

				if(mx > x + 1 and mx < x + slot_width + 5 and my > y and my < y + slot_height + 4)then

					local stored_spells = EntityGetVariable(card, "storedspells", "string")

					local stored_actions = {}


					for spell_id in string.gmatch(stored_spells, '([^,]+)') do
						for k, v in pairs(actions)do
							if(v.id == spell_id)then
								table.insert(stored_actions, v.sprite)
							end
						end
					end

					local name = EntityGetVariable(card, "module_name", "string")

					local x_add = x + 135

					if(slot_x < 4)then
						x_add = 210
					end

					local reduction = string.len(name) - string.len("Spell Rune")

					x_add = x_add + (reduction * 6)

					local y_add = (y + 33)

					if(parent_slot_x == 2)then
						y_add = y_add - 55

					end

					if(parent_slot_x == 3)then
						y_add = y_add - 161

					end

					GuiZSetForNextWidget( gui, -1000 )
					GuiBeginScrollContainer( gui, new_id(), x_add, y_add, 201, 120 )
					Grid(gui, {items=stored_actions, x= 0, y= 0, size=10, padding_y = 18, ui_Scale = true}, function(item) 
						GuiZSetForNextWidget( gui, -1100 )
						GuiImage( gui, new_id(), 0, 0, item, 1,1,1)
					end)
					GuiEndScrollContainer( gui )


				end
			end
			-- Is in a wand
		end
	end
end