--[[
	Sisterhelpers 
	version:	$ 2010-02-09 | v.3.3a
	author:		자매 / 노르간논
--]]

local addon = CreateFrame("Frame");

-- events 등록
for _, v in ipairs({"VARIABLES_LOADED", "AUCTION_HOUSE_SHOW", "AUCTION_HOUSE_CLOSED", "MERCHANT_SHOW", "MERCHANT_CLOSED", "BANKFRAME_OPENED", "BANKFRAME_CLOSED", "MAIL_SHOW", "MAIL_CLOSED", "GUILDBANKFRAME_OPENED", "GUILDBANKFRAME_CLOSED", "PLAYER_DEAD", "DUEL_REQUESTED", "PLAYER_XP_UPDATE", "PLAYER_LEVEL_UP", "PLAYER_ENTERING_WORLD", "ZONE_CHANGED_NEW_AREA"}) do
	addon:RegisterEvent(v);
end;

-- addon vars
addon.OnUpdate = 0.5;


----------------------
-- 가시 거리 최대화
----------------------
SetCVar("screenshotQuality", 6)
SetCVar("cameraDistanceMax", 50)
SetCVar("cameraDistanceMaxFactor", 3.4)

----------------------
-- ALT+Click 한 묶음씩 구매
----------------------
local savedMerchantItemButton_OnModifiedClick = MerchantItemButton_OnModifiedClick;

function MerchantItemButton_OnModifiedClick(self, ...)
	if ( IsAltKeyDown() ) then
		local maxStack = select(8, GetItemInfo(GetMerchantItemLink(self:GetID())));
		local name, texture, price, quantity, numAvailable, isUsable, extendedCost = GetMerchantItemInfo(self:GetID());
		if ( maxStack and maxStack > 1 ) then
			BuyMerchantItem(self:GetID(), floor(maxStack / quantity));
		end;
	end;
	savedMerchantItemButton_OnModifiedClick(self, ...);
end;

local addon = CreateFrame('Frame')
addon:SetScript('OnEvent', OnEvent)
addon:RegisterEvent('PLAYER_LOGIN')

----------------------
-- /rl 애드온 리로드, /준비 레이드 전투 준비
		SLASH_LHRELOADUI1 = "/rl";
		SlashCmdList["LHRELOADUI"] = function() ReloadUI(); end;

		SlashCmdList["RCSLASH"] = function() DoReadyCheck() end
		SLASH_RCSLASH1 = "/rc"
		SLASH_RCSLASH2 = "/준비"

----------------------		
-- GM 요청
	SlashCmdList["TICKET"] = function()
	ToggleHelpFrame()
	end
	SLASH_TICKET1 = "/지엠"
	SLASH_TICKET2 = "/gm"




---------------------------------------------------------------
-- 빠른 전정실 링크
-- Credits: Tekkub, AnduinLothar
-- stitching together bits and pieces from tekPlayerMenu, ReURL

local Armories = {
	[1] = {label = "1: Blizzard", base = ".wowarmory.com/character-sheet.xml?r="},
}

ArmoryQL_Config = {};

UnitPopupButtons["AQL"]={text = "|CFF32f1ff전정실 링크", dist = 0};
table.insert(UnitPopupMenus["FRIEND"], #(UnitPopupMenus["FRIEND"])-1, "AQL");

local function urlEscape(url)
	return string.gsub(url, "([^A-Za-z0-9_:/?&=.-])",
		function(ch)
			return string.format("%%%02x", string.byte(ch))
		end)
end

local function constructUrl(name)
	if not name or name == "" then return end

	local zone, server, tempurl, url;

	zone = GetCVar("realmList");
	if zone then
		zone = string.split(".", zone);
		if not zone or zone == "" then
			DEFAULT_CHAT_FRAME:AddMessage("|CFFCC33FFArmory QuickLink:|r Couldn't find realm list!");
			return;
		end
	end

	server = GetCVar("realmName");
	if not server or server == "" then
		DEFAULT_CHAT_FRAME:AddMessage("|CFFCC33FFArmory QuickLink:|r Couldn't find server!");
		return;
	end

	if not (string.lower(zone) == "eu" or string.lower(zone) == "us" or string.lower(zone) == "kr") then
		DEFAULT_CHAT_FRAME:AddMessage("|CFFCC33FFArmory QuickLink:|r ["..string.upper(zone).."] is not a supported Realm List.");
		return;
	end

	if not ArmoryQL_Config.ArmoryID then
		ArmoryQL_Config.ArmoryID = 1;
	end

	if ArmoryQL_Config.ArmoryID == 1 then -- Blizzard
		if zone == "us" then
			tempurl = "http://www"..Armories[ArmoryQL_Config.ArmoryID].base..server.."&n=";
		elseif zone == "eu" then
			tempurl = "http://eu"..Armories[ArmoryQL_Config.ArmoryID].base..server.."&n=";
		elseif zone == "kr" then
			tempurl = "http://kr"..Armories[ArmoryQL_Config.ArmoryID].base..server.."&n=";
		else
			tempurl = nil;
		end
		if tempurl then
			url = tempurl..name.."&rhtml=true";
		end
	end

	if url then
		url = urlEscape(url);
	end

	return url;
end

local function ShowUrl(name)
	if not name then return end
	local url = constructUrl(name);
	if url then
		local edit_box = ChatEdit_ChooseBoxForSend()
		ChatEdit_ActivateChat(edit_box)
		if url then
			edit_box:Insert(url);
			edit_box:HighlightText();
		end
	end
end

hooksecurefunc("UnitPopup_HideButtons", function(self)
	local dropdownMenu = UIDROPDOWNMENU_INIT_MENU
	for k,v in pairs(UnitPopupMenus[dropdownMenu.which]) do
		if v == "AQL" then
			UnitPopupShown[k] = (dropdownMenu.name == UnitName("player") and 0) or 1
		end
	end
end)

hooksecurefunc("UnitPopup_OnClick", function(self)
	local dropdownFrame = UIDROPDOWNMENU_INIT_MENU
	local button = self.value
	if button == "AQL" then
		ShowUrl(dropdownFrame.name);
	end
end)

local function ArmoryQuickLink_Help()
	DEFAULT_CHAT_FRAME:AddMessage("|CFFCC33FFArmory QuickLink|r: /aql x (selects profiler)");
	for i=1,#(Armories) do
		DEFAULT_CHAT_FRAME:AddMessage(Armories[i].label);
	end
end

local function ArmoryQuickLink_Slash(cmd)
	if not cmd or cmd == "" then
		ArmoryQuickLink_Help();
		return;
	end
	local id = tonumber(cmd);
	if not id then
		ArmoryQuickLink_Help();
		return;
	end
	if not Armories[id] then
		ArmoryQuickLink_Help();
		return;
	end
	ArmoryQL_Config.ArmoryID = id;
	DEFAULT_CHAT_FRAME:AddMessage("|CFFCC33FFArmory QuickLink|r: "..Armories[id].label);
end

SLASH_ARMORYQUICKLINK1 = "/armoryquicklink";
SLASH_ARMORYQUICKLINK2 = "/aql";
SlashCmdList["ARMORYQUICKLINK"] = ArmoryQuickLink_Slash;

