
BINDING_HEADER_BLIMKWARRIORSTANCE = "전사전용 태세변환 단축키";
BINDING_NAME_TOGGLESTANCE1	= "전태<->방태 전환키";
BINDING_NAME_TOGGLESTANCE2	= "광태<->방태 전환키";
BINDING_NAME_TOGGLESTANCE3	= "전태<->광태 전환키";

BWS_BindButton1 = nil;
BWS_BindButton2 = nil;
BWS_BindButton3 = nil;

local function print(msg)
	DEFAULT_CHAT_FRAME:AddMessage(msg);
end

local function getButton(num,macrotext)
	local bind = getglobal("BWS_BindButton"..num);
	if ( bind ) then
		return bind;
	end
	bind = CreateFrame("Button", "BWS_BindButton"..num, UIParent, "SecureActionButtonTemplate");
	bind:SetAttribute("type", "macro");
	bind:SetAttribute("macrotext", macrotext);
	bind:RegisterForClicks("LeftButtonDown");
	bind:Hide();
	return bind;
end

local function getButton1()
	return getButton(1,"/cast [stance:1]방어 태세;전투 태세");
end

local function getButton2()
	return getButton(2,"/cast [stance:3]방어 태세;광폭 태세");
end

local function getButton3()
	return getButton(3,"/cast [stance:3]전투 태세;광폭 태세");
end

function BWS_UpdateBinding()
	local bindButton;
	local bindKeys = { GetBindingKey("TOGGLESTANCE1") };
	for i, keystr in ipairs(bindKeys) do
		if (keystr ~= "") then
			bindButton = getButton1();
			SetOverrideBindingClick(bindButton, true, keystr, bindButton:GetName());
		end
	end

	bindKeys = { GetBindingKey("TOGGLESTANCE2") };
	for i, keystr in ipairs(bindKeys) do
		if (keystr ~= "") then
			bindButton = getButton2();
			SetOverrideBindingClick(bindButton, true, keystr, bindButton:GetName());
		end
	end

	bindKeys = { GetBindingKey("TOGGLESTANCE3") };
	for i, keystr in ipairs(bindKeys) do
		if (keystr ~= "") then
			bindButton = getButton3();
			SetOverrideBindingClick(bindButton, true, keystr, bindButton:GetName());
		end
	end

end

-- event routine
local evFrame = CreateFrame("Frame",nil,nil);
evFrame:SetScript("OnEvent", function (self,event)
	if ((select(2,UnitClass("player"))) ~= "WARRIOR") then
		return;
	end
	if (event == "ADDON_LOADED" and arg1 == "BlinkWarriorStance") then
		getButton1();
		getButton2();
		getButton3();
	elseif ( event == "UPDATE_BINDINGS" ) then
		BWS_UpdateBinding();
	end
end);
evFrame:RegisterEvent("UPDATE_BINDINGS");
evFrame:RegisterEvent("ADDON_LOADED");
