WindActionBorderSize = {
	{495, 55},
	{255, 95},
	{175, 135},
	{135, 175},
	{95, 255},
	{55, 495}
}

function WindAction_OnLoad()
	WindCommon_setPoint(MainMenuBar, "BOTTOM", UIParent, "BOTTOM", 0, -1000);
	WindActionBody_initConfig();
	hooksecurefunc("ActionButton_UpdateHotkeys", function() WindActionButton_UpdateHotkeys(this.buttonType); end);
	hooksecurefunc("ActionButton_OnUpdate", function() WindActionButton_OnUpdate(); end);
	UIParent_ManageFramePositions = WindAction_ManageFramePositions;
end

function WindActionBody_initConfig()
	if (not WindActionBodyConfig) then WindActionBodyConfig = WindActionDefault["Body"]; end
end

function WindActionButton_UpdateHotkeys(actionButtonType)
    if ( not actionButtonType ) then
        actionButtonType = "ACTIONBUTTON";
    end

	local hotkey = getglobal(this:GetName().."HotKey");
	local action = ActionButton_GetPagedID(this);
	local key = GetBindingKey(actionButtonType..this:GetID()) or GetBindingKey("CLICK "..this:GetName()..":LeftButton");
	
	local text = GetBindingText(key, "KEY_", 1);

	if ( text == "" ) then
		hotkey:SetText(RANGE_INDICATOR);
		hotkey:SetPoint("TOPLEFT", this, "TOPLEFT", 1, -2);
		hotkey:Hide();
	else
		text = string.gsub(text, "숫자패드% ", "n-");
		text = string.gsub(text, "Backspace", "Back");
		text = string.gsub(text, "스페이스 바", "Spac");
		text = string.gsub(text, "마우스 가운데 버튼", "M3");
		text = string.gsub(text, "4번 마우스 버튼", "M4");
		text = string.gsub(text, "5번 마우스 버튼", "M5");
		text = string.gsub(text, "마우스 휠 아래로", "▼");
		text = string.gsub(text, "마우스 휠 위로",  "▲");
		text = string.gsub(text, "Page Up", "p-U");
		text = string.gsub(text, "Page Down", "p-D");
		text = string.gsub(text, "Home", "Hom");
		text = string.gsub(text, "Insert", "Ins");
		text = string.gsub(text, "Delete", "Del");
		
		hotkey:SetText(text);
		if (WindActionBodyConfig[3]) then
			hotkey:SetFont("Fonts\\2002b.TTF", 15, "OUTLINE");
			hotkey:SetPoint("TOPLEFT", -10, -2);
			hotkey:SetWidth(44);
			hotkey:SetTextColor(1, 1, 1, 1);
			hotkey:SetShadowOffset(1, -1);
			hotkey:SetShadowColor(0, 0, 0, 1);
		else
			hotkey:SetFont("Fonts\\ARIALN.TTF", 11, "THICKOUTLINE, MONOCHROME");
			hotkey:SetPoint("TOPLEFT", -2, -2);
			hotkey:SetWidth(36);
			hotkey:SetTextColor(0.6, 0.6, 0.6, 1);
			hotkey:SetShadowOffset(0, 0);
			hotkey:SetShadowColor(0, 0, 0, 0);
		end

		hotkey:Show();
	end
end


function WindActionButton_OnUpdate()
	if (HasAction(ActionButton_GetPagedID(this))) then WindActionButton_updateCCount(); end
	if (ActionButton_IsFlashing()) then
		this.flashtime = this.flashtime - arg1;
		if (this.flashtime <= 0) then
			local overtime = -this.flashtime;
			if (overtime >= ATTACK_BUTTON_FLASH_TIME) then overtime = 0; end
			this.flashtime = ATTACK_BUTTON_FLASH_TIME - overtime;
			local flashTexture = getglobal(this:GetName().."Flash");
			if (flashTexture:IsVisible()) then flashTexture:Hide();
			else flashTexture:Show(); end
		end
	end

	if (this.rangeTimer) then
		this.rangeTimer = this.rangeTimer - arg1;
		if (this.rangeTimer <= 0) then
			local range = getglobal(this:GetName().."RCRange");
			if (range) then
				if (WindActionBodyConfig[2] and IsActionInRange(ActionButton_GetPagedID(this)) == 0) then range:Show();
				range:SetVertexColor(0.9, 0.3, 0.3, 0.7);
				else range:Hide(); end
			end
			this.rangeTimer = TOOLTIP_UPDATE_TIME;
		end
	end

	if (not this.updateTooltip) then return; end
	this.updateTooltip = this.updateTooltip - arg1;
	if (this.updateTooltip > 0) then return; end
	if (GameTooltip:IsOwned(this)) then ActionButton_SetTooltip();
	else this.updateTooltip = nil; end
end

function WindActionButton_updateCCount()
	local start, duration, enable = GetActionCooldown(ActionButton_GetPagedID(this));
	local text = getglobal(this:GetName().."RCCCount");
	if (text) then
		if (start>0 and duration>0 and enable>0) then
			if (WindActionBodyConfig[1] and duration > 2.0) then
				text:SetText(WindActionButton_getColldownText(start, duration, GetTime()));
				text:Show();
			else
				text:Hide();
			end
		else
			text:Hide();
		end
	end
end

function WindActionButton_getColldownText(start, duration, now)
	local remainingTime = ceil((start + duration) - now);
	if (remainingTime>=3600) then
		getglobal(this:GetName().."RCCCount"):SetFont("Fonts\\2002b.TTF",18,"OUTLINE");
		getglobal(this:GetName().."RCCCount"):SetTextColor(.85,.85,.85,1);
		return ( math.floor((remainingTime / 3600)+0.5).."시" );
	elseif (remainingTime>=60) then
		getglobal(this:GetName().."RCCCount"):SetFont("Fonts\\2002b.TTF",18,"OUTLINE");
		getglobal(this:GetName().."RCCCount"):SetTextColor(.85,.85,.85,1);
		return ( math.floor((remainingTime / 60)+0.5).."분" );
	elseif (remainingTime>9) then
		getglobal(this:GetName().."RCCCount"):SetFont("Fonts\\2002b.TTF",24,"OUTLINE");
		getglobal(this:GetName().."RCCCount"):SetTextColor(1,.82,0,1);
		return remainingTime;
	else
		getglobal(this:GetName().."RCCCount"):SetFont("Fonts\\2002b.TTF",34,"OUTLINE");
		getglobal(this:GetName().."RCCCount"):SetTextColor(1,.12,.12,1);
		return remainingTime;
	end
end

function WindActionButton_setAnchor(index, btn)
	if (index == 1) then
		WindCommon_setPoint(getglobal(btn.."2"), "LEFT", btn.."1", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."3"), "LEFT", btn.."2", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."4"), "LEFT", btn.."3", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."5"), "LEFT", btn.."4", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."6"), "LEFT", btn.."5", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."7"), "LEFT", btn.."6", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."8"), "LEFT", btn.."7", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."9"), "LEFT", btn.."8", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."10"), "LEFT", btn.."9", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."11"), "LEFT", btn.."10", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."12"), "LEFT", btn.."11", "RIGHT", 4, 0);
	elseif (index == 2) then
		WindCommon_setPoint(getglobal(btn.."2"), "LEFT", btn.."1", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."3"), "LEFT", btn.."2", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."4"), "LEFT", btn.."3", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."5"), "LEFT", btn.."4", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."6"), "LEFT", btn.."5", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."7"), "TOPLEFT", btn.."1", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."8"), "LEFT", btn.."7", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."9"), "LEFT", btn.."8", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."10"), "LEFT", btn.."9", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."11"), "LEFT", btn.."10", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."12"), "LEFT", btn.."11", "RIGHT", 4, 0);
	elseif (index == 3) then
		WindCommon_setPoint(getglobal(btn.."2"), "LEFT", btn.."1", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."3"), "LEFT", btn.."2", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."4"), "LEFT", btn.."3", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."5"), "TOPLEFT", btn.."1", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."6"), "LEFT", btn.."5", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."7"), "LEFT", btn.."6", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."8"), "LEFT", btn.."7", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."9"), "TOPLEFT", btn.."5", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."10"), "LEFT", btn.."9", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."11"), "LEFT", btn.."10", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."12"), "LEFT", btn.."11", "RIGHT", 4, 0);
	elseif (index == 4) then
		WindCommon_setPoint(getglobal(btn.."2"), "LEFT", btn.."1", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."3"), "LEFT", btn.."2", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."4"), "TOPLEFT", btn.."1", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."5"), "LEFT", btn.."4", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."6"), "LEFT", btn.."5", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."7"), "TOPLEFT", btn.."4", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."8"), "LEFT", btn.."7", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."9"), "LEFT", btn.."8", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."10"), "TOPLEFT", btn.."7", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."11"), "LEFT", btn.."10", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."12"), "LEFT", btn.."11", "RIGHT", 4, 0);
	elseif (index == 5) then
		WindCommon_setPoint(getglobal(btn.."2"), "LEFT", btn.."1", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."3"), "TOPLEFT", btn.."1", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."4"), "LEFT", btn.."3", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."5"), "TOPLEFT", btn.."3", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."6"), "LEFT", btn.."5", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."7"), "TOPLEFT", btn.."5", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."8"), "LEFT", btn.."7", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."9"), "TOPLEFT", btn.."7", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."10"), "LEFT", btn.."9", "RIGHT", 4, 0);
		WindCommon_setPoint(getglobal(btn.."11"), "TOPLEFT", btn.."9", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."12"), "LEFT", btn.."11", "RIGHT", 4, 0);
	elseif (index == 6) then
		WindCommon_setPoint(getglobal(btn.."2"), "TOPLEFT", btn.."1", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."3"), "TOPLEFT", btn.."2", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."4"), "TOPLEFT", btn.."3", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."5"), "TOPLEFT", btn.."4", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."6"), "TOPLEFT", btn.."5", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."7"), "TOPLEFT", btn.."6", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."8"), "TOPLEFT", btn.."7", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."9"), "TOPLEFT", btn.."8", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."10"), "TOPLEFT", btn.."9", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."11"), "TOPLEFT", btn.."10", "BOTTOMLEFT", 0, -4);
		WindCommon_setPoint(getglobal(btn.."12"), "TOPLEFT", btn.."11", "BOTTOMLEFT", 0, -4);
	end
end

function WindAction_ManageFramePositions()
	local yOffsetFrames = {};
	local xOffsetFrames = {};
	local HWAOffsetX = WindActionBodyConfig[5] or 60;
	local frame, xOffset, yOffset, anchorTo, point, rpoint;
	for index, value in pairs(UIPARENT_MANAGED_FRAME_POSITIONS) do
		frame = getglobal(index);
		if ( frame ) then
			xOffset = 0;
			if ( value["baseX"] ) then
				xOffset = value["baseX"];
			elseif ( value["xOffset"] ) then
				xOffset = value["xOffset"];
			end
			yOffset = 0;
			if ( value["baseY"] ) then
				yOffset = value["baseY"];
			end
			for flag, flagValue in pairs(yOffsetFrames) do
				if ( value[flagValue] ) then
					yOffset = yOffset + value[flagValue];
				end
			end
			for flag, flagValue in pairs(xOffsetFrames) do
				if ( value[flagValue] ) then
					xOffset = xOffset + value[flagValue];
				end
			end
			anchorTo = value["anchorTo"];
			point = value["point"];
			rpoint = value["rpoint"];
			if ( not anchorTo ) then
				anchorTo = "UIParent";
			end
			if ( not point ) then
				point = "BOTTOM";
			end
			if ( not rpoint ) then
				rpoint = "BOTTOM";
			end
			if ( value["isVar"] ) then
				if ( value["isVar"] == "xAxis" ) then
					setglobal(index, xOffset);
				else
					setglobal(index, yOffset);
				end
			else
				if ( frame == ChatFrame1 and SIMPLE_CHAT == "1" ) then
					frame:SetPoint(point, anchorTo, rpoint, xOffset, yOffset);
				elseif ( not(frame:IsObjectType("frame") and frame:IsUserPlaced()) ) then
					frame:SetPoint(point, anchorTo, rpoint, xOffset, yOffset);
				end
			end
		end
	end
	if ( BattlefieldMinimapTab and not BattlefieldMinimapTab:IsUserPlaced() ) then
		BattlefieldMinimapTab:SetPoint("BOTTOMRIGHT", "UIParent", "BOTTOMRIGHT", -10-HWAOffsetX, 120);
	end
	ShapeshiftBarLeft:Hide();
	ShapeshiftBarRight:Hide();

	for i=1, GetNumShapeshiftForms() do
		getglobal("ShapeshiftButton"..i.."NormalTexture"):SetWidth(60);
		getglobal("ShapeshiftButton"..i.."NormalTexture"):SetHeight(60);
	end
	local anchorY = 0;
	QuestTimerFrame:SetPoint("TOPRIGHT", "MinimapCluster", "BOTTOMRIGHT", -HWAOffsetX, anchorY);
	if ( QuestTimerFrame:IsShown() ) then
		anchorY = anchorY - QuestTimerFrame:GetHeight();
	end
	if ( DurabilityFrame ) then
		local durabilityOffset = 0;
		if ( DurabilityShield:IsShown() or DurabilityOffWeapon:IsShown() or DurabilityRanged:IsShown() ) then
			durabilityOffset = 20;
		end
		DurabilityFrame:SetPoint("TOPRIGHT", "MinimapCluster", "BOTTOMRIGHT", -HWAOffsetX-durabilityOffset, anchorY);
		if ( DurabilityFrame:IsShown() ) then
			anchorY = anchorY - DurabilityFrame:GetHeight();
		end
	end
	QuestWatchFrame:SetPoint("TOPRIGHT", "MinimapCluster", "BOTTOMRIGHT", -HWAOffsetX, anchorY);
	FCF_DockUpdate();
	--FCF_UpdateCombatLogPosition();
	updateContainerFrameAnchors();
end
