
NURFED_LOCKALL = 1;

NURFED_SAVED_MENU = {};
local saved = {};
local player = nil;

Nurfed_LockFuncs = {};

local menu = Nurfed_Dropdown:New()

old_PickupAction = PickupAction;
function PickupAction(id)
	if (NURFED_LOCKALL == 0) then
		old_PickupAction(id);
	end
end

function Nurfed_LockButton_OnLoad()
	this:RegisterForClicks("LeftButtonUp", "RightButtonUp");
	this:RegisterEvent("PLAYER_ENTERING_WORLD");
	this:SetChecked(NURFED_LOCKALL);
end

function Nurfed_LockButton_OnEvent(event)
	if (event == "PLAYER_ENTERING_WORLD") then
		this:UnregisterEvent(event);

		player = UnitName("player").." - "..GetCVar("realmName");
		if (not NURFED_SAVED_MENU[player]) then
			NURFED_SAVED_MENU[player] = {
				["button_offsetX"] = 0;
				["button_offsetY"] = -80;
			};
		end
		saved = NURFED_SAVED_MENU[player];
		Nurfed_LockButton_UpdatePosition();
	end
end

function Nurfed_LockButton_OnMouseDown(button)
	if (IsShiftKeyDown() and button == "LeftButton") then
		this.isMoving = 1;
	end
end

function Nurfed_LockButton_OnMouseUp(button)
	this.isMoving = nil;
end

function Nurfed_LockButton_OnClick(button)
	if (IsShiftKeyDown()) then
		this:SetChecked(NURFED_LOCKALL);
		return;
	end
	if (button == "LeftButton") then
		if (NURFED_LOCKALL == 1) then
			NURFED_LOCKALL = 0;
			PlaySound("igMainMenuQuit");
		else
			NURFED_LOCKALL = 1;
			PlaySound("igMainMenuOption");
		end
		Nurfed_LockButton:SetChecked(NURFED_LOCKALL);
		Nurfed_LockButton_OnEnter();

		for k, v in Nurfed_LockFuncs do
			v();
		end

	elseif (button == "RightButton") then
		Nurfed_LockButton:SetChecked(NURFED_LOCKALL);
		menu:GenerateLockMenu(getglobal(this:GetName().."DropDown"));
		menu:ShowLockMenu(this);
	end
end

function Nurfed_LockButton_OnEnter()
	if (Nurfed_LockButtonDropDown:IsVisible()) then
		return;
	end
	GameTooltip:SetOwner(this, "ANCHOR_BOTTOMLEFT");
	GameTooltip:AddLine("Nurfed UI", 0,1,1);
	if (this:GetChecked()) then
		GameTooltip:AddLine("Current Status: |cffff0000Locked|r", 1,1,1);
		GameTooltip:AddLine("Left Click: (|cff00ff00Unlock|r)", 1,1,1);
		GameTooltip:AddLine("Right Click: Options Menu", 1,1,1);
		GameTooltip:AddLine("Shift + Click: Move Button", 1,1,1);
	else
		GameTooltip:AddLine("Current Status: |cff00ff00Unlocked|r", 1,1,1);
		GameTooltip:AddLine("Left Click: (|cffff0000Lock|r)", 1,1,1);
		GameTooltip:AddLine("Right Click: Options Menu", 1,1,1);
		GameTooltip:AddLine("Shift + Click: Move Button", 1,1,1);
	end
	GameTooltip:Show();
end

function Nurfed_LockButton_Menu()
	UIDropDownMenu_Initialize(this, this.initialize, "MENU");
end

function Nurfed_LockButton_OnUpdate()
	if (this.isMoving) then
		if (not IsShiftKeyDown()) then
			this.isMoving = nil;
			return;
		end
		
		-- Credit to Alex Brazie for this calculation, most excellent
		cursorX, cursorY = GetCursorPosition();
		centerX, centerY = Minimap:GetCenter();
		scale = Minimap:GetEffectiveScale();
		cursorX = cursorX / scale;
		cursorY = cursorY / scale;
		local radius = (Minimap:GetWidth()/2) + (this:GetWidth()/3);
		local x = math.abs(cursorX - centerX);
		local y = math.abs(cursorY - centerY);
		local xSign = 1;
		local ySign = 1;
		if (not (cursorX >= centerX)) then
			xSign = -1;
		end
		if (not (cursorY >= centerY)) then
			ySign = -1;
		end

		local angle = math.atan(x/y);
		x = math.sin(angle)*radius;
		y = math.cos(angle)*radius;
		saved.button_offsetX = xSign*x;
		saved.button_offsetY = ySign*y;
		
		Nurfed_LockButton_UpdatePosition();
	end
end

function Nurfed_LockButton_UpdatePosition()
	Nurfed_LockButton:ClearAllPoints();
	Nurfed_LockButton:SetPoint("CENTER", "Minimap", "CENTER", saved.button_offsetX, saved.button_offsetY);
end