-------------------------------------------------------------------------------
--
--  Mod Name : BlinkSwap v3.01
--  Author   : Blink
--  Date     : 2004/10/11
--  LastUpdate : 2009/12/15
--
-------------------------------------------------------------------------------

local addonName = "BlinkSwap"
local addon = CreateFrame("Frame", addonName)
addon:RegisterEvent("ADDON_LOADED")
addon:SetScript("OnEvent", function(this, event, ...)
	if this[event] then
		this[event](this, ...)
	end

	if this.events and this.events[event] and type(this.events[event])=='table' then
		for name, func in pairs(this.events[event]) do
			if BlinkSwapDB and BlinkSwapDB.set and BlinkSwapDB.set[name] and func(...) then
				if GetEquipmentSetInfoByName(BlinkSwapDB.set[name]) then
					EquipmentManager_EquipSet(BlinkSwapDB.set[name])
					break
				end
			end
		end
	end
end)

_G[addonName] = addon

local version = "v3.01"
local version_n = 3.01

local MainHandSlotID = 16
local OffHandSlotID = 17
local RangedSlotID = 18
local AmmoSlotID = 0

local tinsert = table.insert
local tremove = table.remove
local tsort = table.sort
local wipe = table.wipe

local function print(msg)
	ChatFrame1:AddMessage(msg, 1.0, 1.0, 0.2)
end
local new, del, deepDel, doAll
do
	local list = setmetatable({}, {__mode='k'})
	function new(...)
		local t = next(list)
		if t then
			list[t] = nil
			for i = 1, select('#', ...) do
				t[i] = select(i, ...)
			end
			return t
		else
			return { ... }
		end
	end
	function del(t)
		setmetatable(t, nil)
		table.wipe(t)
		list[t] = true
		return nil
	end
	function deepDel(t)
		if type(t) ~= "table" then
			return nil
		end
		for k,v in pairs(t) do
			t[k] = deepDel(v)
		end
		return del(t)
	end
	function doAll(func, ...)
		for i=1, select('#', ...) do
			func(select(i, ...))
		end
	end
end

local function getLinkToID(link)
	if link then
		local _, _, id = string.find(link, "(%d+):")
		return id and tonumber(id)
	end
	return 0
end

addon.eventList = {
	{
		name = "전투 태세",
		class = "WARRIOR",
		event = "UPDATE_BONUS_ACTIONBAR",
		icon = "Interface\\Icons\\Ability_Warrior_OffensiveStance",
		func = function (...)
			local form
			for i=1, GetNumShapeshiftForms() do
				local texture, name, isActive, isCastable = GetShapeshiftFormInfo(i)
				if(isActive) then
					if name == "전투 태세" then
						return true
					end
				end
			end
		end,
	},
	{
		name = "방어 태세",
		class = "WARRIOR",
		icon = "Interface\\Icons\\Ability_Warrior_DefensiveStance",
		event = "UPDATE_BONUS_ACTIONBAR",
		func = function (...)
			local form
			for i=1, GetNumShapeshiftForms() do
				local texture, name, isActive, isCastable = GetShapeshiftFormInfo(i)
				if(isActive) then
					if name == "방어 태세" then
						return true
					end
				end
			end
		end,
	},
	{
		name = "광폭 태세",
		class = "WARRIOR",
		icon = "Interface\\Icons\\Ability_Racial_Avatar",
		event = "UPDATE_BONUS_ACTIONBAR",
		func = function (...)
			local form
			for i=1, GetNumShapeshiftForms() do
				local texture, name, isActive, isCastable = GetShapeshiftFormInfo(i)
				if(isActive) then
					if name == "광폭 태세" then
						return true
					end
				end
			end
		end,
	},
	{
		name = "곰 변신",
		class = "DRUID",
		icon = "Interface\\Icons\\Ability_Racial_BearForm",
		event = "UPDATE_BONUS_ACTIONBAR",
		func = function (...)
			local form
			for i=1, GetNumShapeshiftForms() do
				local texture, name, isActive, isCastable = GetShapeshiftFormInfo(i)
				if(isActive) then
					if name == "곰 변신" or name == "광포한 곰 변신" then
						return true
					end
				end
			end
		end,
	},
	{
		name = "표범 변신",
		class = "DRUID",
		icon = "Interface\\Icons\\Ability_Druid_CatForm",
		event = "UPDATE_BONUS_ACTIONBAR",
		func = function (...)
			local form
			for i=1, GetNumShapeshiftForms() do
				local texture, name, isActive, isCastable = GetShapeshiftFormInfo(i)
				if(isActive) then
					if name == "표범 변신" then
						return true
					end
				end
			end
		end,
	},
	{
		name = "생명의 나무 변신",
		class = "DRUID",
		icon = "Interface\\Icons\\Ability_Druid_TreeofLife",
		event = "UPDATE_BONUS_ACTIONBAR",
		func = function (...)
			local form
			for i=1, GetNumShapeshiftForms() do
				local texture, name, isActive, isCastable = GetShapeshiftFormInfo(i)
				if(isActive) then
					if name == "생명의 나무" then
						return true
					end
				end
			end
		end,
	},
	{
		name = "달빛야수 변신",
		class = "DRUID",
		icon = "Interface\\Icons\\Spell_Nature_ForceOfNature",
		event = "UPDATE_BONUS_ACTIONBAR",
		func = function (...)
			local form
			for i=1, GetNumShapeshiftForms() do
				local texture, name, isActive, isCastable = GetShapeshiftFormInfo(i)
				if(isActive) then
					if name == "달빛야수 변신" then
						return true
					end
				end
			end
		end,
	},
	{
		name = "치타 변신",
		class = "DRUID",
		icon = "Interface\\Icons\\Ability_Druid_TravelForm",
		event = "UPDATE_BONUS_ACTIONBAR",
		func = function (...)
			local form
			for i=1, GetNumShapeshiftForms() do
				local texture, name, isActive, isCastable = GetShapeshiftFormInfo(i)
				if(isActive) then
					if name == "치타 변신" then
						return true
					end
				end
			end
		end,
	},
	{
		name = "폭풍까마귀 변신",
		class = "DRUID",
		icon = "Interface\\Icons\\Ability_Druid_FlightForm",
		event = "UPDATE_BONUS_ACTIONBAR",
		func = function (...)
			local form
			for i=1, GetNumShapeshiftForms() do
				local texture, name, isActive, isCastable = GetShapeshiftFormInfo(i)
				if(isActive) then
					if name == "폭풍까마귀 변신" then
						return true
					end
				end
			end
		end,
	},
	{
		name = "바다표범 변신",
		class = "DRUID",
		icon = "Interface\\Icons\\Ability_Druid_AquaticForm",
		event = "UPDATE_BONUS_ACTIONBAR",
		func = function (...)
			local form
			for i=1, GetNumShapeshiftForms() do
				local texture, name, isActive, isCastable = GetShapeshiftFormInfo(i)
				if(isActive) then
					if name == "바다표범 변신" then
						return true
					end
				end
			end
		end,
	},
	{
		name = "은신",
		class = "ROGUE",
		icon = "Interface\\Icons\\Ability_Stealth",
		event = "UPDATE_BONUS_ACTIONBAR",
		func = function (...)
			local form
			for i=1, GetNumShapeshiftForms() do
				local texture, name, isActive, isCastable = GetShapeshiftFormInfo(i)
				if(isActive) then
					if name == "은신" then
						return true
					end
				end
			end
		end,
	},
	{
		name = "변신/은신 풀림",
		class = {
			["ROGUE"] = true,
			["DRUID"] = true,
		},
		icon = "Interface\\Icons\\Spell_Nature_Invisibilty",
		event = "UPDATE_BONUS_ACTIONBAR",
		func = function (...)
			local form
			for i=1, GetNumShapeshiftForms() do
				local texture, name, isActive, isCastable = GetShapeshiftFormInfo(i)
				if(isActive) then
					form = name
				end
			end
			return not form and true or nil
		end,
	},
	--[[{
		name = "검과 방패",
		class = "WARRIOR",
		icon = "Interface\\Icons\\Ability_Warrior_SwordandBoard",
		event = "COMBAT_LOG_EVENT_UNFILTERED",
		func = function (...)
			local timestamp, event, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, spellId, spellName, spellSchool, auraType = ...
			if event == "SPELL_AURA_APPLIED" or event == "SPELL_AURA_APPLIED_DOSE" then
				if srcGUID == UnitGUID("player") and auraType == "BUFF" then
					print(spellName)
				end
				if srcGUID == UnitGUID("player") and auraType == "BUFF" and spellName == "검과 방패" then
					return true
				end
			end
		end,
	},]]
	{
		name = "혈기의 형상",
		class = "DEATHKNIGHT",
		icon = "Interface\\Icons\\Spell_Deathknight_BloodPresence",
		event = "COMBAT_LOG_EVENT_UNFILTERED",
		func = function (...)
			local timestamp, event, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, spellId, spellName, spellSchool, auraType = ...
			if event == "SPELL_AURA_APPLIED" or event == "SPELL_AURA_APPLIED_DOSE" then
				if srcGUID == UnitGUID("player") and auraType == "BUFF" and spellName == "혈기의 형상" then
					return true
				end
			end
		end,
	},
	{
		name = "냉기의 형상",
		class = "DEATHKNIGHT",
		icon = "Interface\\Icons\\Spell_Deathknight_FrostPresence",
		event = "COMBAT_LOG_EVENT_UNFILTERED",
		func = function (...)
			local timestamp, event, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, spellId, spellName, spellSchool, auraType = ...
			if event == "SPELL_AURA_APPLIED" or event == "SPELL_AURA_APPLIED_DOSE" then
				if srcGUID == UnitGUID("player") and auraType == "BUFF" and spellName == "냉기의 형상" then
					return true
				end
			end
		end,
	},
	{
		name = "부정의 형상",
		class = "DEATHKNIGHT",
		icon = "Interface\\Icons\\Spell_Deathknight_UnholyPresence",
		event = "COMBAT_LOG_EVENT_UNFILTERED",
		func = function (...)
			local timestamp, event, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, spellId, spellName, spellSchool, auraType = ...
			if event == "SPELL_AURA_APPLIED" or event == "SPELL_AURA_APPLIED_DOSE" then
				if srcGUID == UnitGUID("player") and auraType == "BUFF" and spellName == "부정의 형상" then
					return true
				end
			end
		end,
	},
	--[[{
		name = "탈것",
		event = "UNIT_SPELLCAST_SUCCEEDED",
		func = function (...)
			print( IsMounted() and "탓어" or "안탓어" )
			return select(1, ...)=="player" and not UnitAffectingCombat("player") and IsMounted()
		end,
	},]]
}

function addon:ADDON_LOADED(loadedName)
	if loadedName ~= addonName then
		return nil
	end
	self:UnregisterEvent("ADDON_LOADED")

	-- 블리자드 장비 관리자 강제 실행
	--BlizzardOptionsPanel_SetCVarSafe("equipmentManager", "1")

	UIErrorsFrame:AddMessage("블링크의 무기스왑 애드온 "..version.." 로딩완료!", 1.0, 1.0, 0.0, 1.0, UIERRORS_HOLD_TIME)

	if not BlinkSwapDB or not BlinkSwapDB.version or BlinkSwapDB.version < version_n then
		BlinkSwapDB = {
			version = version_n,
			swapSet = {"SET1", "SET2"},
			set = {},
		}
	end

	local _, class = UnitClass("player")

	self:RegisterCustomEvents()
	self:HookEquipSetButtonScript()

	MainHandSlotID = GetInventorySlotInfo("MainHandSlot")
	OffHandSlotID = GetInventorySlotInfo("SecondaryHandSlot")
	RangedSlotID = GetInventorySlotInfo("RangedSlot")
	AmmoSlotID = GetInventorySlotInfo("AmmoSlot")

	self.dropdown = CreateFrame("FRAME", "BlinkSwapDropDown", GearManagerDialog, "UIDropDownMenuTemplate")

	SlashCmdList["BLINKSWAPSAVE"] = function(msg) addon:Save() end
	for i=1,getn(BLINK_SWAP_SAVE_COMMAND),1 do
		setglobal("SLASH_BLINKSWAPSAVE"..i,BLINK_SWAP_SAVE_COMMAND[i]);
	end
	
	SlashCmdList["BLINKSWAPSWAP"] = function(msg) addon:Swap() end
	for i=1,getn(BLINK_SWAP_COMMAND),1 do
		setglobal("SLASH_BLINKSWAPSWAP"..i,BLINK_SWAP_COMMAND[i]);
	end

	SlashCmdList["BLINKSWAPLIST"] = function(msg) addon:ViewStauts() end
	for i=1,getn(BLINK_SWAP_LIST_COMMAND),1 do
		setglobal("SLASH_BLINKSWAPLIST"..i,BLINK_SWAP_LIST_COMMAND[i]);
	end

	SlashCmdList["BLINKSWAPRESET"] = function(msg) addon:Reset() end
	for i=1,getn(BLINK_SWAP_RESET_COMMAND),1 do
		setglobal("SLASH_BLINKSWAPRESET"..i,BLINK_SWAP_RESET_COMMAND[i]);
	end

	SlashCmdList["BLINKSWAPHELP"] = function(msg) addon:Help() end
	for i=1,getn(BLINK_SWAP_HELP_COMMAND),1 do
		setglobal("SLASH_BLINKSWAPHELP"..i,BLINK_SWAP_HELP_COMMAND[i]);
	end

	if ( BlinkMod_RegisterFunc ) then
		BS_BM_Register();
	end

end

function addon:RegisterCustomEvents()
	self.events = {}
	local _, class = UnitClass("player")
	for k, v in ipairs(self.eventList) do
		if v.class then
			if type(v.class)=="table" and v.class[class] then
				if v.class[class] then
					if not self.events[v.event] then
						self.events[v.event] = {}
					end
					self.events[v.event][v.name] = v.func
				end
			elseif type(v.class)=="string" and v.class==class then
				if not self.events[v.event] then
					self.events[v.event] = {}
				end
				self.events[v.event][v.name] = v.func
			end
		else
			if not self.events[v.event] then
				self.events[v.event] = {}
			end
			self.events[v.event][v.name] = v.func
		end
	end
	for k, v in pairs(self.events) do
		self:RegisterEvent(k)
	end
end

function addon:HookEquipSetButtonScript()
	local b
	for i = 1, MAX_EQUIPMENT_SETS_PER_PLAYER do
		b = _G["GearSetButton" .. i]
		b:RegisterForClicks("LeftButtonUp", "RightButtonUp")
		b:HookScript("OnClick", function (this, button, down)
			HideDropDownMenu(1)
			if this.text:GetText()~=nil and this.text:GetText()~="" and button == "RightButton" then
				addon:ShowDropdown(this, this.text:GetText())
			end
		end)
	end
end

function addon:ShowDropdown(button, name)
	HideDropDownMenu(1)
	UIDropDownMenu_Initialize(self.dropdown, function ()
		local _, class = UnitClass("player")
		local info = UIDropDownMenu_CreateInfo()
		info.isTitle = 1
		info.notCheckable = 1
		info.text = "블링크 스왑"
		UIDropDownMenu_AddButton(info)

		local checked
		for i=1, #BlinkSwapDB.swapSet do
			if BlinkSwapDB.swapSet[i]==name then
				checked = true
				break
			end
		end

		info = UIDropDownMenu_CreateInfo()
		info.text = "스왑장비 지정"
		info.icon = button.icon:GetTexture()
		info.tooltipTitle = "스왑장비 지정"
		info.tooltipText = "현재 장비세트를 스왑목록에 추가하거나 제외시킵니다.|n스왑목록에 추가된 장비세트는 \"스왑하기\"키를 누를때 순차적으로 장비를 교체합니다.|n스왑장비는 최소 2개이상 지정해야합니다."
		info.checked = checked
		info.func = function (this)
			if checked then
				if #BlinkSwapDB.swapSet <= 2 then
					UIErrorsFrame:AddMessage("스왑장비는 2개 이상 지정해야합니다.", 1.0, 0.1, 0.1, 1.0)
					return
				end
				for i=1, #BlinkSwapDB.swapSet do
					if BlinkSwapDB.swapSet[i]==name then
						tremove(BlinkSwapDB.swapSet, i)
						break
					end
				end
			else
				tinsert(BlinkSwapDB.swapSet, name)
			end
		end
		UIDropDownMenu_AddButton(info)

		for k, v in ipairs(addon.eventList) do
			local rc = false
			if v.class then
				if type(v.class)=="table" and v.class[class] then
					rc = true
				elseif type(v.class)=="string" and v.class==class then
					rc = true
				end
			else
				rc = true
			end
			if rc then
				info = UIDropDownMenu_CreateInfo()
				info.text = v.name
				info.checked = BlinkSwapDB.set[v.name]==name and 1 or nil
				info.icon = v.icon
				info.func = function (this)
					if BlinkSwapDB.set[v.name] == name then
						BlinkSwapDB.set[v.name] = nil
					else
						BlinkSwapDB.set[v.name] = name
					end
				end
				UIDropDownMenu_AddButton(info)
			end
		end

		info = UIDropDownMenu_CreateInfo()
		info.text = "닫기"
		info.func = function () end
		info.notCheckable = 1
		info.tooltipTitle = "닫기"
		info.tooltipText = nil
		UIDropDownMenu_AddButton(info)

	end ,"MENU")
	ToggleDropDownMenu(1, nil, self.dropdown, "cursor")
end

function addon:GetEquippedItemIds()
	return getLinkToID(GetInventoryItemLink("player", MainHandSlotID)), getLinkToID(GetInventoryItemLink("player", OffHandSlotID))
end

function addon:GetEquippedSetName()
	local set
	local mainItemId, offItemId, mainCorrect, offCorrect
	for i=1, #BlinkSwapDB.swapSet do
		set = GetEquipmentSetItemIDs(BlinkSwapDB.swapSet[i])
		if set then
			mainItemId, offItemId = self:GetEquippedItemIds()
			mainItemId = mainItemId or 0
			offItemId = offItemId or 0

			if set[MainHandSlotID] == mainItemId then
				mainCorrect = true
			end
			if set[OffHandSlotID] == offItemId then
				offCorrect = true
			end
			if mainCorrect and offCorrect then
				return BlinkSwapDB.swapSet[i], set[MainHandSlotID], set[OffHandSlotID]
			end
			mainCorrect = nil
			offCorrect = nil
		end
	end
end

function addon:GetSavedItemIds(setName)
	local set = GetEquipmentSetItemIDs(setName)
	if set then
		return set[MainHandSlotID], set[OffHandSlotID]
	end
	return nil, nil
end

function addon:GetNextSwapSetName(currentSetName)
	local nextSetName = BlinkSwapDB.swapSet[1]
	for i=#BlinkSwapDB.swapSet, 1, -1 do
		if BlinkSwapDB.swapSet[i] ~= currentSetName then
			nextSetName = BlinkSwapDB.swapSet[i]
		else
			return nextSetName
		end
	end
	return nextSetName
end

function addon:FindEmptyEquipmentSlot(mainId, offId)
	local nextSetName = BlinkSwapDB.swapSet[1]
	for i=1, #BlinkSwapDB.swapSet do
		m, o = self:GetSavedItemIds(BlinkSwapDB.swapSet[i])
		if m==nil and o==nil then
			return BlinkSwapDB.swapSet[i]
		elseif m~=nil and o~=nil and m == mainId and o == offId then
			return nil
		end
	end

	local totalCount = GetNumEquipmentSets()
	local setName
	for i=1, totalCount do
		setName = GetEquipmentSetInfo(i)
		for j=1, #BlinkSwapDB.swapSet do
			if BlinkSwapDB.swapSet[j] == setName then
				DeleteEquipmentSet(BlinkSwapDB.swapSet[j])
				nextSetName = BlinkSwapDB.swapSet[j]
			end
		end
	end
	return nextSetName
end

function addon:GetIcon(set)
	local totalCount = GetNumEquipmentSets()
	local setName, icon
	for i=1, totalCount do
		setName, icon = GetEquipmentSetInfo(i)
		if setName == set then
			return icon
		end
	end
end

function addon:Swap(set)
	local currentSetName, mainItemId, offItemId = self:GetEquippedSetName()
	local nextSetName = self:GetNextSwapSetName(currentSetName)

	if not GetEquipmentSetInfoByName(nextSetName) and not GetEquipmentSetInfoByName(set) and not GetEquipmentSetInfoByName(BlinkSwapDB.swapSet[set]) then
		UIErrorsFrame:AddMessage(BS_NOT_SET_WEAPONS_DESC, 1.0, 0.1, 0.1, 1.0)
		return
	end

	if set then
		if type(set)=="number" and BlinkSwapDB.swapSet[set] and GetEquipmentSetInfoByName(BlinkSwapDB.swapSet[set]) then
			EquipmentManager_EquipSet(BlinkSwapDB.swapSet[set])
			PlaySound("igCharacterInfoTab")
		elseif type(set)=="string" and GetEquipmentSetInfoByName(set) then
			EquipmentManager_EquipSet(set)
			PlaySound("igCharacterInfoTab")
		end
	else
		EquipmentManager_EquipSet(nextSetName)
		PlaySound("igCharacterInfoTab")
	end
end

function addon:Save()

	local usedSlot = #BlinkSwapDB.swapSet
	for i=1, #BlinkSwapDB.swapSet do
		if GetEquipmentSetInfoByName(BlinkSwapDB.swapSet[i]) then
			usedSlot = usedSlot - 1
		end
	end

	if GetNumEquipmentSets()+usedSlot > MAX_EQUIPMENT_SETS_PER_PLAYER then
		UIErrorsFrame:AddMessage(EQUIPMENT_SETS_TOO_MANY, 1.0, 0.1, 0.1, 1.0)
		return
	end

	local mainItemId, offItemId = self:GetEquippedItemIds()
	local nextSetName = self:FindEmptyEquipmentSlot(mainItemId, offItemId)

	if not nextSetName then
		self:ViewStauts()
		return
	end

	local iconIndex = 0
	if mainItemId and mainItemId > 0 then
		iconIndex = -MainHandSlotID
	elseif offItemId and offItemId > 0 then
		iconIndex = -OffHandSlotID
	end
	if iconIndex == 0 then
		for i = INVSLOT_FIRST_EQUIPPED, INVSLOT_LAST_EQUIPPED do
			local t = GetInventoryItemTexture("player", i)
			if t then
				iconIndex = -i
				break
			end
		end
	end
	if not iconIndex or iconIndex > -1 then
		iconIndex = 1
	end
	for i = INVSLOT_FIRST_EQUIPPED, INVSLOT_LAST_EQUIPPED do
		if not INVSLOTS_EQUIPABLE_IN_COMBAT[i] then
			EquipmentManagerIgnoreSlotForSave(i)
		end
	end

	SaveEquipmentSet(nextSetName, iconIndex)
	self:ViewStauts()
end

function addon:ViewStauts()
	local mainId, offId
	local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount,itemEquipLoc, itemTexture, itemSellPrice

	print(BLINK_WEAPON_SWAP_LIST1)
	print(BLINK_WEAPON_SWAP_LIST2)
	for i=1, #BlinkSwapDB.swapSet do
		mainId, offId = self:GetSavedItemIds(BlinkSwapDB.swapSet[i])
		if mainId and mainId>0 then
			itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount,itemEquipLoc, itemTexture, itemSellPrice = GetItemInfo(mainId)
			print(BLINK_WEAPON_SWAP_LIST:format(BlinkSwapDB.swapSet[i], INVTYPE_WEAPONMAINHAND, itemName))
		else
			print(BLINK_WEAPON_SWAP_LIST:format(BlinkSwapDB.swapSet[i], INVTYPE_WEAPONMAINHAND, NONE))
		end
		
		if offId and offId>0 then
			itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount,itemEquipLoc, itemTexture, itemSellPrice = GetItemInfo(offId)
			print(BLINK_WEAPON_SWAP_LIST:format(BlinkSwapDB.swapSet[i], INVTYPE_WEAPONOFFHAND, itemName))
		else
			print(BLINK_WEAPON_SWAP_LIST:format(BlinkSwapDB.swapSet[i], INVTYPE_WEAPONOFFHAND, NONE))
		end
	end
	print(BLINK_WEAPON_SWAP_LIST2)
end

function addon:Reset()
	local cnt = #BlinkSwapDB.swapSet
	local set
	for i=1, cnt do
		set = tremove(BlinkSwapDB.swapSet)
		DeleteEquipmentSet(set)
	end
	tinsert(BlinkSwapDB.swapSet, "SET1")
	tinsert(BlinkSwapDB.swapSet, "SET2")
	print(BS_RESET_DONE)
end

-------------------------------------------------------------------------------
-- BlinkSwapHelp
-------------------------------------------------------------------------------
function addon:Help()
	print("블링크의 무기스왑 애드온 "..version.." 도움말:");
	print(" /스왑도움말   : 도움말 메세지를 볼수 있습니다.");
	print(" /스왑설정     : 현재 장착하고있는 무기를 스왑할 무기로 저장합니다.");
	print(" /스왑         : /스왑설정 명령어로 저장한 무기들을 스왑합니다.");
	print(" /스왑목록     : /스왑설정 명령어로 저장한 무기들의 목록을 확인합니다.");
	print(" /스왑초기화   : /스왑설정 명령어와 /태세스왑저장 명령어로 저장한 무기들을 초기화합니다.");
	print("단축설정에서 [무기스왑], [스왑무기저장]키를 설정하시면 /스왑, /스왑설정 명령어를 쉽게 사용하실 수 있습니다.");
end

-- BlinkMod GUI Configuratioin ------------------------------------------------

function BS_BM_MenuInitialize(owner)
	local info, count
	count = 0
	if owner == "SELF" then
		info = UIDropDownMenu_CreateInfo()
		info.owner = "SELF"
		info.value = "BSMAIN"
		info.text = "무기스왑"
		info.hasArrow = 1
		info.tooltipTitle = "무기스왑 애드온"
		info.notCheckable = 1
		UIDropDownMenu_AddButton(info)
		count = count + 1
	end

	if owner == "BSMAIN" then
		info = UIDropDownMenu_CreateInfo()
		info.owner = "BSMAIN"
		info.value = "BSTITLE"
		info.text = "무기스왑"
		info.isTitle = 1
		info.notCheckable = 1
		UIDropDownMenu_AddButton(info, 2)
		count = count + 1

		info = UIDropDownMenu_CreateInfo()
		info.owner = "BSMAIN"
		info.value = "BSSWAP"
		info.text = BS_BM_SwapName()
		info.tooltipTitle = "무기스왑하기"
		info.tooltipText = "설정되어있는 세트를 교체합니다."
		info.func = function () addon:Swap() end
		UIDropDownMenu_AddButton(info, 2)
		count = count + 1

		info = UIDropDownMenu_CreateInfo()
		info.owner = "BSMAIN"
		info.value = "BSSAVECURRENTSET"
		info.text = BS_BM_SaveCurrentWeaponsName()
		info.tooltipTitle = "무기저장"
		info.tooltipText = "현재 장착하고있는 장비를 저장합니다."
		info.func = function () addon:Save() end
		UIDropDownMenu_AddButton(info, 2)
		count = count + 1

		info = UIDropDownMenu_CreateInfo()
		info.owner = "BSMAIN"
		info.value = "BSRESET"
		info.text = "무기스왑 초기화"
		info.tooltipTitle = "무기스왑 초기화"
		info.tooltipText = "무기스왑 애드온의 설정을 초기화합니다."
		info.func = function () addon:Reset() end
		UIDropDownMenu_AddButton(info, 2)
		count = count + 1

		info = UIDropDownMenu_CreateInfo()
		info.owner = "BSMAIN"
		info.value = "BSLIST"
		info.isTitle = 1
		info.notCheckable = 1
		info.text = "스왑 목록"
		info.tooltipTitle = "스왑 목록"
		info.tooltipText = "설정되어있는 스왑세트를 확인합니다."
		UIDropDownMenu_AddButton(info, 2)
		count = count + 1

		for i=1, #BlinkSwapDB.swapSet do
			info = UIDropDownMenu_CreateInfo()
			info.owner = "BSMAIN"
			info.value = "BSLISTSET" .. i
			info.text = BS_BM_GetSetItems(i)
			info.tooltipTitle = BlinkSwapDB.swapSet[i]
			info.tooltipText = "클릭하시면 해당무기를 장착합니다."
			info.func = function() addon:Swap(i) end
			info.checked = addon:GetEquippedSetName() == BlinkSwapDB.swapSet[i] and 1 or nil
			info.icon = addon:GetIcon(BlinkSwapDB.swapSet[i])
			UIDropDownMenu_AddButton(info, 2)
			count = count + 1
		end
	end

	return count
end

function BS_BM_Register()
	BlinkMod_RegisterFunc(BS_BM_MenuInitialize, "BSMAIN")
end

function BS_BM_GetSetItems(i)
	local mainhand, offhand
	local mainId, offId = addon:GetSavedItemIds(BlinkSwapDB.swapSet[i])
	if mainId and mainId>0 then
		itemName = GetItemInfo(mainId)
		mainhand = (BLINK_WEAPON_SWAP_LIST:format("|cffffd100"..BlinkSwapDB.swapSet[i].."|r", INVTYPE_WEAPONMAINHAND, itemName))
	else
		mainhand = (BLINK_WEAPON_SWAP_LIST:format("|cffffd100SET"..i.."|r", INVTYPE_WEAPONMAINHAND, NONE))
	end
	if offId and offId>0 then
		itemName = GetItemInfo(offId)
		offhand = (BLINK_WEAPON_SWAP_LIST:format("", INVTYPE_WEAPONOFFHAND, itemName))
	else
		offhand = (BLINK_WEAPON_SWAP_LIST:format("", INVTYPE_WEAPONOFFHAND, NONE))
	end
	return mainhand..offhand
end

-- 스왑하기 -------------------------------------------------------------------
function BS_BM_SwapName()
	local bind = GetBindingText(GetBindingKey("BNK_SWAPWEAPON"), "KEY_");
	if ( bind ) then
		return "무기스왑하기|cffffd100("..bind..")|r";
	else
		return "무기스왑하기";
	end
end
function BS_BM_SaveCurrentWeaponsName()
	local bind = GetBindingText(GetBindingKey("BNK_SAVESWAP"), "KEY_");
	if ( bind ) then
		return "무기저장|cffffd100("..bind..")|r";
	else
		return "무기저장";
	end	
end

--function BS_BM_MenuInitialize()
--	local menuList = {
--		{
--			text = "무기스왑",
--			hasArrow = true,
--			tooltipTitle = "무기스왑 애드온",
--			notCheckable = 1,
--			owner = "SELF",
--			value = "BSMAIN",
--			menuList = {
--				{
--					owner = "BSMAIN",
--					text = "무기스왑",
--					isTitle = 1,
--					notCheckable = 1,
--				},
--				{
--					owner = "BSMAIN",
--					text = BS_BM_SwapName(),
--					tooltipTitle = "무기스왑하기",
--					tooltipText = "설정되어있는 세트를 교체합니다.",
--					func = function () addon:Swap() end,
--				},
--				{
--					owner = "BSMAIN",
--					text = BS_BM_SaveCurrentWeaponsName(),
--					tooltipTitle = "무기저장",
--					tooltipText = "현재 장착하고있는 장비를 저장합니다.",
--					func = function () addon:Save() end,
--				},
--				{
--					owner = "BSMAIN",
--					text = "무기스왑 초기화",
--					tooltipTitle = "무기스왑 초기화",
--					tooltipText = "무기스왑 애드온의 설정을 초기화합니다.",
--					func = function () addon:Reset() end,
--				},
--				{
--					owner = "BSMAIN",
--					text = "스왑 목록",
--					isTitle = 1,
--					tooltipTitle = "스왑 목록",
--					tooltipText = "설정되어있는 스왑세트를 확인합니다.",
--				},
--			},
--		},
--	}
--
--	for i=1, #BlinkSwapDB.swapSet do
--		tinsert(menuList[1].menuList, {
--			owner = "BSMAIN",
--			text = BS_BM_GetSetItems(i),
--			tooltipTitle = BlinkSwapDB.swapSet[i],
--			tooltipText = "클릭하시면 해당무기를 장착합니다.",
--			func = function() addon:Swap(i) end,
--			checked = addon:GetEquippedSetName() == BlinkSwapDB.swapSet[i] and 1 or nil,
--			icon = addon:GetIcon(BlinkSwapDB.swapSet[i]),
--		})
--	end
--	return menuList
--end