local addOnName = GetAddOnDependencies(...)
local BII3 = _G[addOnName]

function BII3:CloseDropDownMenu()
	if UIDROPDOWNMENU_OPEN_MENU == BII3.listDropdown1 or UIDROPDOWNMENU_OPEN_MENU == BII3.listDropdown2 or UIDROPDOWNMENU_OPEN_MENU == BII3.listDropdown3 or UIDROPDOWNMENU_OPEN_MENU == BII3.itemDropdown then
		UIDROPDOWNMENU_MENU_VALUE = nil
		CloseDropDownMenus(1)
	end
end

local listInfo = {
	isNotRadio = true,
	notCheckable = true,
	func = function(self, code)
		CloseDropDownMenus(1)
		BII3.db.code = code
		BII3:Update()
	end,
}

local levels = {
	[1] = function()
		listInfo.hasArrow = true
		for j = BII3.codeListZero, BII3.codeListLen do
			if BII3.itemList[listInfo.value..BII3.codeList:sub(j, j).."*"] then
				for k = BII3.codeListZero, BII3.codeListLen do
					if BII3.itemList[listInfo.value..BII3.codeList:sub(j, j)..BII3.codeList:sub(k, k)] then
						listInfo.arg1 = listInfo.value..BII3.codeList:sub(j, j)..BII3.codeList:sub(k, k)
						return
					end
				end
			end
		end
		listInfo.arg1 = nil
	end,
	[2] = function()
		listInfo.hasArrow = BII3:GetCategory(listInfo.value.."0"):find("(.+)_(.+)_(.+)")
		if listInfo.hasArrow then
			for j = BII3.codeListZero, BII3.codeListLen do
				if BII3.itemList[listInfo.value..BII3.codeList:sub(j, j)] then
					listInfo.arg1 = listInfo.value..BII3.codeList:sub(j, j)
					return
				end
			end
			listInfo.arg1 = nil
		else
			listInfo.arg1 = listInfo.value.."0"
		end
	end,
	[3] = function()
		listInfo.hasArrow = nil
		listInfo.arg1 = listInfo.value
	end,
}

local n, star, zero

local function initializeListDropDown(self, level)
	if type(UIDROPDOWNMENU_MENU_VALUE) == "string" then
		n = UIDROPDOWNMENU_MENU_VALUE:len() + 1
		star = ("*"):rep(3 - n)
		zero = ("0"):rep(3 - n)
		for i = n == 1 and 1 or BII3.codeListZero, BII3.codeListLen do
			listInfo.value = UIDROPDOWNMENU_MENU_VALUE..BII3.codeList:sub(i, i)
			if BII3.itemList[listInfo.value..star] then
				listInfo.text = BII3:ClearColor(select(n, BII3:GetCategoryName(listInfo.value..zero)))
				levels[n]()
				UIDropDownMenu_AddButton(listInfo, level)
			end
		end
		n, star, zero, listInfo.text, listInfo.hasArrow, listInfo.value, listInfo.arg1 = nil
	end
end

BII3.listDropdown1 = CreateFrame("Frame", addOnName.."ListDropDown1", BII3, "UIDropDownMenuTemplate")
UIDropDownMenu_Initialize(BII3.listDropdown1, initializeListDropDown, "MENU")
BII3.listDropdown2 = CreateFrame("Frame", addOnName.."ListDropDown2", BII3, "UIDropDownMenuTemplate")
UIDropDownMenu_Initialize(BII3.listDropdown2, initializeListDropDown, "MENU")
BII3.listDropdown3 = CreateFrame("Frame", addOnName.."ListDropDown3", BII3, "UIDropDownMenuTemplate")
UIDropDownMenu_Initialize(BII3.listDropdown3, initializeListDropDown, "MENU")

local itemInfo = {
	isNotRadio = true,
	notCheckable = true,
	tooltipOnButton = true,
	func = function(self, id, process)
		if process == 1 then
			if ChatEdit_GetActiveWindow() then
				ChatEdit_GetActiveWindow():SetFocus(true)
				ChatEdit_GetActiveWindow():Insert(BII3:GetItemLink(id))
			else
				DEFAULT_CHAT_FRAME.editBox:Show()
				DEFAULT_CHAT_FRAME.editBox:SetFocus(true)
				DEFAULT_CHAT_FRAME.editBox:Insert(BII3:GetItemLink(id))
			end
		elseif process == 2 then
			DressUpItemLink(BII3:GetItemSimpleLink(id))
		elseif process == 3 then
			BII3:AddFavorite(id)
		elseif process == 4 then
			BII3:AddMemo(id)
		end
	end,
}

BII3.itemDropdown = CreateFrame("Frame", addOnName.."ItemDropDown", BII3, "UIDropDownMenuTemplate")
UIDropDownMenu_Initialize(BII3.itemDropdown, function(self, level)
	if type(UIDROPDOWNMENU_MENU_VALUE) == "number" then
		itemInfo.arg1 = UIDROPDOWNMENU_MENU_VALUE
		itemInfo.text = "채팅창에 링크하기"
		itemInfo.tooltipTitle = "아이템을 채팅창에 링크합니다."
		itemInfo.tooltipText = "Shift+클릭"
		itemInfo.arg2 = 1
		UIDropDownMenu_AddButton(itemInfo, level)
		if itemInfo.arg1 > 0 then
			itemInfo.text = "미리보기"
			itemInfo.tooltipTitle = "아이템을 미리 입어봅니다."
			itemInfo.tooltipText = "Ctrl+클릭"
			itemInfo.arg2 = 2
			UIDropDownMenu_AddButton(itemInfo, level)
		end
		if BII3:IsFavorite(itemInfo.arg1) then
			itemInfo.text = "즐겨찾기에서 삭제하기"
			itemInfo.tooltipTitle = "아이템을 즐겨찾기에서 삭제합니다."
		else
			itemInfo.text = "즐겨찾기에 추가하기"
			itemInfo.tooltipTitle = "아이템을 즐겨찾기에 추가합니다."
		end
		itemInfo.tooltipText = "Alt+클릭"
		itemInfo.arg2 = 3
		UIDropDownMenu_AddButton(itemInfo, level)
		itemInfo.text = "메모"
		itemInfo.tooltipTitle = "아이템에 메모를 작성합니다."
		itemInfo.tooltipText = "Shift+Ctrl+클릭"
		itemInfo.arg2 = 4
		UIDropDownMenu_AddButton(itemInfo, level)
		itemInfo.tooltipText, itemInfo.tooltipTitle, itemInfo.arg1, itemInfo.arg2 = nil
		itemInfo.text = CLOSE
		UIDropDownMenu_AddButton(itemInfo, level)
		itemInfo.text = nil
	end
end, "MENU")