local toc, privateData = ...
local AddonId = toc.identifier

EM.UI.Inventory = {}
EM.UI.Inventory.Bags = {}
EM.UI.Inventory.Bags.Loaded = {}

EM.Context.Inventory = UI.CreateContext("emInventoryContext")
EM.Context.Inventory:SetStrata("tutorial")

local tableAdd = table.insert

function EM.UI.Inventory.CreateSlots(bag, totalSlots)

	local bagWindow = EM.UI.Inventory.Bags[bag]
	local slots = totalSlots
	local slotPosition = {col = 0, row = 0}
	local bagScale = tonumber(Inspect.Setting.Detail('displayBagUiScale').value)

	for i=1, slots, 1 do
		bagWindow[i] = UI.CreateFrame("Frame", "emInventoryBag" .. bag .. "_Slot" .. i, bagWindow)
			if i==1 then
				bagWindow[i]:SetPoint("TOPLEFT", bagWindow, "TOPLEFT", (15*bagScale), (33*bagScale))
			else
				bagWindow[i]:SetPoint("TOPLEFT", bagWindow[1], "TOPLEFT", slotPosition.col*(52*bagScale), slotPosition.row*(52*bagScale))
			end
			bagWindow[i]:SetHeight((50*bagScale))
			bagWindow[i]:SetWidth((50*bagScale))
			bagWindow[i]:SetBackgroundColor(0,0,0,0)
			bagWindow[i]:SetMouseMasking("limited")

		if slotPosition.col < 3 then
			slotPosition.col = slotPosition.col + 1
		else
			slotPosition.col = 0
			slotPosition.row = slotPosition.row + 1
		end
		if i < 10 then
			bagWindow[i].name = "si0" .. bag ..".00" .. i
		else
			bagWindow[i].name = "si0" .. bag ..".0" .. i
		end

		bagWindow[i]:EventAttach(Event.UI.Input.Mouse.Right.Click, function()
			local item = Inspect.Item.Detail(bagWindow[i].name)
			if bagWindow[i].attached then return end
			if item then
				if item.bound == nil then
					tableAdd(EM.UI.Send.Attachments, { { key = item.id, value = item.icon, typeID = item.type, slot = {bag,i} }, { value = item.name, color = 'FFFFFF' }, { value = (item.stack or 1), color = 'FFFFFF' } })
					bagWindow[i]:SetBackgroundColor(0,0,0,0.4)
				elseif item.bind == "account" then
					tableAdd(EM.UI.Send.Attachments, { { key = item.id, value = item.icon, typeID = item.type, slot = {bag,i} }, { value = item.name, color = 'FFFFFF' }, { value = (item.stack or 1), color = 'FFFFFF' } })
					bagWindow[i]:SetBackgroundColor(0,0,0,0.4)
				end

				EM.Send.Fill()

				bagWindow[i].attached = true

				if EM.UI.Send.SubjectField:GetText() == (nil or "") then
					EM.UI.Send.SubjectField:SetText(EM.UI.Send.Attachments[1][2].value .. " x" .. EM.UI.Send.Attachments[1][3].value)
					EM.Utility.SendBtnCheck()
				end
			end
		end, "emInventoryBag" .. bag .. "_Slot" .. i .. "_RightClick")

		bagWindow[i]:EventAttach(Event.UI.Input.Mouse.Cursor.In, function()
			local item = Inspect.Item.Detail(bagWindow[i].name)
			if item then
				if bagWindow[i].attached then
					bagWindow[i]:SetBackgroundColor(0.18,0.36,0.41,0.4)
				else
					bagWindow[i]:SetBackgroundColor(0.36,0.65,0.72,0.4)
				end
				Command.Tooltip(item.id)
			end
		end, "emInventoryBag" .. bag .. "_Slot" .. i .. "_MouseIn")

		bagWindow[i]:EventAttach(Event.UI.Input.Mouse.Cursor.Out, function()
			if bagWindow[i].attached then
				bagWindow[i]:SetBackgroundColor(0,0,0,0.4)
			else
				bagWindow[i]:SetBackgroundColor(0,0,0,0)
			end
			Command.Tooltip(nil)
		end, "emInventoryBag" .. bag .. "_Slot" .. i .. "_MouseOut")

		bagWindow[i]:EventAttach(Event.UI.Input.Mouse.Cursor.Move, function()
			local type, unitID, buff = Inspect.Tooltip()
			if type == nil then
				local item = Inspect.Item.Detail(bagWindow[i].name)
				if item then
					Command.Tooltip(item.id)
				end
			end
			local mouse = Inspect.Mouse()
			local hold,id = Inspect.Cursor()
			if mouse.Left == true and hold == nil then
				local item = Inspect.Item.Detail(bagWindow[i].name)
				if item then
					Command.Cursor(item.id)
				end
			end
		end, "emInventoryBag" .. bag .. "_Slot" .. i .. "_MouseMove")

		bagWindow[i]:EventAttach(Event.UI.Input.Mouse.Left.Click, function()
			Command.Item.Standard.Left(bagWindow[i].name)
		end, "emInventoryBag" .. bag .. "_Slot" .. i .. "_LeftClick")

		bagWindow[i]:EventAttach(Event.UI.Input.Mouse.Left.Upoutside, function()
			local held,id = Inspect.Cursor()
			for slotnum, data in pairs(EM.UI.Send.Attachments) do
				if data[1].key == id then
					bagWindow[i]:SetBackgroundColor(0,0,0,0.4)
					bagWindow[i].attached = true
					EM.UI.Send.Attachments[slotnum][1].slot = {bag,i}
				end
			end
		end, "emInventoryBag" .. bag .. "_Slot" .. i .. "_LeftUpoutside")
	end

	return bagWindow

end

function EM.UI.Inventory.ImhobagsRight(handle, params)
	if EM.UI.MailWindow and EM.UI.MailWindow:GetVisible() and EM.UI.Display.Tabs.Visible == "Send" then
		local item = Inspect.Item.Detail(params.id)
		if item then
			params.cancel = true
			for row, data in ipairs(EM.UI.Send.Attachments) do
				if data[1].key == item.id then
					print(item.name .. " is already Attached!")
					return
				end
			end
			if item.bound == nil then
				tableAdd(EM.UI.Send.Attachments, { { key = item.id, value = item.icon, typeID = item.type, slot = nil }, { value = item.name, color = 'FFFFFF' }, { value = (item.stack or 1), color = 'FFFFFF' } })
			elseif item.bind == "account" then
				tableAdd(EM.UI.Send.Attachments, { { key = item.id, value = item.icon, typeID = item.type, slot = nil }, { value = item.name, color = 'FFFFFF' }, { value = (item.stack or 1), color = 'FFFFFF' } })
			end
			if #EM.UI.Send.Attachments == 0 then return end
			EM.Send.Fill()
			if EM.UI.Send.SubjectField:GetText() == (nil or "") then
				EM.UI.Send.SubjectField:SetText(EM.UI.Send.Attachments[1][2].value .. " x" .. EM.UI.Send.Attachments[1][3].value)
				EM.Utility.SendBtnCheck()
			end
		end
	end
end

function EM.UI.Inventory.Loaded(bagNumber)
	if EasyMailCharacterTable and EasyMailCharacterTable.Inventory and EasyMailCharacterTable.Inventory.Disable.status then
		return
	else
		bagNative = "BagInventory"..bagNumber
		if not EM.UI.Inventory.Bags[bagNumber] then
			tableAdd(EM.UI.Inventory.Bags, bagNumber)
		else
			EM.UI.Inventory.Bags[bagNumber] = {}
		end
		local bagWindow = UI.CreateFrame("Frame", "emInventoryBag"..bagNumber, EM.Context.Inventory)
			bagWindow:SetPoint("TOPLEFT", UI.Native[bagNative], 0.01688, 0)
			bagWindow:SetPoint("BOTTOMRIGHT", UI.Native[bagNative], 0.96625, 0.975)
			bagWindow:SetVisible(true)

		EM.UI.Inventory.Bags[bagNumber] = bagWindow
		local slots = Inspect.Item.Detail("sibg.00"..bagNumber).slots
		bagWindow = EM.UI.Inventory.CreateSlots(bagNumber, slots)
		EM.UI.Inventory.Bags[bagNumber] = bagWindow
	end
end

function EM.UI.Inventory.Bags.Loaded.Bag1(handle)
	if EM.UI.MailWindow and EM.UI.MailWindow:GetVisible() and EM.UI.Send and EM.UI.Send:GetVisible() then
		if type(EM.UI.Inventory.Bags[1]) == "table" and EM.UI.Inventory.Bags[1] and not EM.UI.Inventory.Bags[1]:GetVisible() and UI.Native.BagInventory1:GetLoaded() then
			EM.UI.Inventory.Bags[1]:SetVisible(true)
		elseif type(EM.UI.Inventory.Bags[1]) == "table" and EM.UI.Inventory.Bags[1] and EM.UI.Inventory.Bags[1]:GetVisible() and UI.Native.BagInventory1:GetLoaded() then
			EM.UI.Inventory.Bags[1]:SetVisible(true)
		elseif type(EM.UI.Inventory.Bags[1]) == "table" and EM.UI.Inventory.Bags[1] and EM.UI.Inventory.Bags[1]:GetVisible() and not UI.Native.BagInventory1:GetLoaded() then
			EM.UI.Inventory.Bags[1]:SetVisible(false)
		elseif UI.Native.BagInventory1:GetLoaded() then
			EM.UI.Inventory.Loaded(1)
		end
	end
end

function EM.UI.Inventory.Bags.Loaded.Bag2(handle)
	if EM.UI.MailWindow and EM.UI.MailWindow:GetVisible() and EM.UI.Send and EM.UI.Send:GetVisible() then
		if type(EM.UI.Inventory.Bags[2]) == "table" and EM.UI.Inventory.Bags[2] and not EM.UI.Inventory.Bags[2]:GetVisible() and UI.Native.BagInventory2:GetLoaded() then
			EM.UI.Inventory.Bags[2]:SetVisible(true)
		elseif type(EM.UI.Inventory.Bags[2]) == "table" and EM.UI.Inventory.Bags[2] and EM.UI.Inventory.Bags[2]:GetVisible() and UI.Native.BagInventory2:GetLoaded() then
			EM.UI.Inventory.Bags[2]:SetVisible(true)
		elseif type(EM.UI.Inventory.Bags[2]) == "table" and EM.UI.Inventory.Bags[2] and EM.UI.Inventory.Bags[2]:GetVisible() and not UI.Native.BagInventory2:GetLoaded() then
			EM.UI.Inventory.Bags[2]:SetVisible(false)
		elseif UI.Native.BagInventory2:GetLoaded() then
			EM.UI.Inventory.Loaded(2)
		end
	end
end

function EM.UI.Inventory.Bags.Loaded.Bag3(handle)
	if EM.UI.MailWindow and EM.UI.MailWindow:GetVisible() and EM.UI.Send and EM.UI.Send:GetVisible() then
		if type(EM.UI.Inventory.Bags[3]) == "table" and EM.UI.Inventory.Bags[3] and not EM.UI.Inventory.Bags[3]:GetVisible() and UI.Native.BagInventory3:GetLoaded() then
			EM.UI.Inventory.Bags[3]:SetVisible(true)
		elseif type(EM.UI.Inventory.Bags[3]) == "table" and EM.UI.Inventory.Bags[3] and EM.UI.Inventory.Bags[3]:GetVisible() and UI.Native.BagInventory3:GetLoaded() then
			EM.UI.Inventory.Bags[3]:SetVisible(true)
		elseif type(EM.UI.Inventory.Bags[3]) == "table" and EM.UI.Inventory.Bags[3] and EM.UI.Inventory.Bags[3]:GetVisible() and not UI.Native.BagInventory3:GetLoaded() then
			EM.UI.Inventory.Bags[3]:SetVisible(false)
		elseif UI.Native.BagInventory3:GetLoaded() then
			EM.UI.Inventory.Loaded(3)
		end
	end
end

function EM.UI.Inventory.Bags.Loaded.Bag4(handle)
	if EM.UI.MailWindow and EM.UI.MailWindow:GetVisible() and EM.UI.Send and EM.UI.Send:GetVisible() then
		if type(EM.UI.Inventory.Bags[4]) == "table" and EM.UI.Inventory.Bags[4] and not EM.UI.Inventory.Bags[4]:GetVisible() and UI.Native.BagInventory4:GetLoaded() then
			EM.UI.Inventory.Bags[4]:SetVisible(true)
		elseif type(EM.UI.Inventory.Bags[4]) == "table" and EM.UI.Inventory.Bags[4] and EM.UI.Inventory.Bags[4]:GetVisible() and UI.Native.BagInventory4:GetLoaded() then
			EM.UI.Inventory.Bags[4]:SetVisible(true)
		elseif type(EM.UI.Inventory.Bags[4]) == "table" and EM.UI.Inventory.Bags[4] and EM.UI.Inventory.Bags[4]:GetVisible() and not UI.Native.BagInventory4:GetLoaded() then
			EM.UI.Inventory.Bags[4]:SetVisible(false)
		elseif UI.Native.BagInventory4:GetLoaded() then
			EM.UI.Inventory.Loaded(4)
		end
	end
end

function EM.UI.Inventory.Bags.Loaded.Bag5(handle)
	if EM.UI.MailWindow and EM.UI.MailWindow:GetVisible() and EM.UI.Send and EM.UI.Send:GetVisible() then
		if type(EM.UI.Inventory.Bags[5]) == "table" and EM.UI.Inventory.Bags[5] and not EM.UI.Inventory.Bags[5]:GetVisible() and  UI.Native.BagInventory5:GetLoaded() then
			EM.UI.Inventory.Bags[5]:SetVisible(true)
		elseif type(EM.UI.Inventory.Bags[5]) == "table" and EM.UI.Inventory.Bags[5] and EM.UI.Inventory.Bags[5]:GetVisible() and  UI.Native.BagInventory5:GetLoaded() then
			EM.UI.Inventory.Bags[5]:SetVisible(true)
		elseif type(EM.UI.Inventory.Bags[5]) == "table" and EM.UI.Inventory.Bags[5] and EM.UI.Inventory.Bags[5]:GetVisible() and not UI.Native.BagInventory5:GetLoaded() then
			EM.UI.Inventory.Bags[5]:SetVisible(false)
		elseif UI.Native.BagInventory5:GetLoaded() then
			EM.UI.Inventory.Loaded(5)
		end
	end
end

function EM.UI.Inventory.Bags.Loaded.Bag6(handle)
	if EM.UI.MailWindow and EM.UI.MailWindow:GetVisible() and EM.UI.Send and EM.UI.Send:GetVisible() then
		if type(EM.UI.Inventory.Bags[6]) == "table" and EM.UI.Inventory.Bags[6] and not EM.UI.Inventory.Bags[6]:GetVisible() and  UI.Native.BagInventory6:GetLoaded() then
			EM.UI.Inventory.Bags[6]:SetVisible(true)
		elseif type(EM.UI.Inventory.Bags[6]) == "table" and EM.UI.Inventory.Bags[6] and EM.UI.Inventory.Bags[6]:GetVisible() and  UI.Native.BagInventory6:GetLoaded() then
			EM.UI.Inventory.Bags[6]:SetVisible(true)
		elseif type(EM.UI.Inventory.Bags[6]) == "table" and EM.UI.Inventory.Bags[6] and EM.UI.Inventory.Bags[6]:GetVisible() and not UI.Native.BagInventory6:GetLoaded() then
			EM.UI.Inventory.Bags[6]:SetVisible(false)
		elseif UI.Native.BagInventory6:GetLoaded() then
			EM.UI.Inventory.Loaded(6)
		end
	end
end

function EM.UI.Inventory.Bags.Loaded.Bag7(handle)
	if EM.UI.MailWindow and EM.UI.MailWindow:GetVisible() and EM.UI.Send and EM.UI.Send:GetVisible() then
		if type(EM.UI.Inventory.Bags[7]) == "table" and EM.UI.Inventory.Bags[7] and not EM.UI.Inventory.Bags[7]:GetVisible() and  UI.Native.BagInventory7:GetLoaded() then
			EM.UI.Inventory.Bags[7]:SetVisible(true)
		elseif type(EM.UI.Inventory.Bags[7]) == "table" and EM.UI.Inventory.Bags[7] and EM.UI.Inventory.Bags[7]:GetVisible() and  UI.Native.BagInventory7:GetLoaded() then
			EM.UI.Inventory.Bags[7]:SetVisible(true)
		elseif type(EM.UI.Inventory.Bags[7]) == "table" and EM.UI.Inventory.Bags[7] and EM.UI.Inventory.Bags[7]:GetVisible() and not UI.Native.BagInventory7:GetLoaded() then
			EM.UI.Inventory.Bags[7]:SetVisible(false)
		elseif UI.Native.BagInventory7:GetLoaded() then
			EM.UI.Inventory.Loaded(7)
		end
	end
end

UI.Native.BagInventory1:EventAttach(Event.UI.Native.Loaded, EM.UI.Inventory.Bags.Loaded.Bag1, AddonId .. "_Native_Inventory1_Loaded")
UI.Native.BagInventory2:EventAttach(Event.UI.Native.Loaded, EM.UI.Inventory.Bags.Loaded.Bag2, AddonId .. "_Native_Inventory2_Loaded")
UI.Native.BagInventory3:EventAttach(Event.UI.Native.Loaded, EM.UI.Inventory.Bags.Loaded.Bag3, AddonId .. "_Native_Inventory3_Loaded")
UI.Native.BagInventory4:EventAttach(Event.UI.Native.Loaded, EM.UI.Inventory.Bags.Loaded.Bag4, AddonId .. "_Native_Inventory4_Loaded")
UI.Native.BagInventory5:EventAttach(Event.UI.Native.Loaded, EM.UI.Inventory.Bags.Loaded.Bag5, AddonId .. "_Native_Inventory5_Loaded")
UI.Native.BagInventory6:EventAttach(Event.UI.Native.Loaded, EM.UI.Inventory.Bags.Loaded.Bag6, AddonId .. "_Native_Inventory6_Loaded")
UI.Native.BagInventory7:EventAttach(Event.UI.Native.Loaded, EM.UI.Inventory.Bags.Loaded.Bag7, AddonId .. "_Native_Inventory7_Loaded")
local info = Inspect.Addon.Detail("ImhoBags")
if(info and info.toc.publicAPI) then
	Command.Event.Attach(ImhoBags.Event.Item.Standard.Right, EM.UI.Inventory.ImhobagsRight, AddonId .. "_ImhobagsRight")
end