------------------------------------------------
--                  CT_Core                   --
--                                            --
-- Core addon for doing basic and popular     --
-- things in an intuitive way.                --
-- Please do not modify or otherwise          --
-- redistribute this without the consent of   --
-- the CTMod Team. Thank you.                 --
------------------------------------------------

--------------------------------------------
-- Initialization

local module = { };
local _G = getfenv(0);

local MODULE_NAME = "CT_Core";
local MODULE_VERSION = strmatch(GetAddOnMetadata(MODULE_NAME, "version"), "^([%d.]+)");

module.name = MODULE_NAME;
module.version = MODULE_VERSION;

_G[MODULE_NAME] = module;
CT_Library:registerModule(module);

--------------------------------------------
-- Minimap Handler

local sqrt, abs = sqrt, abs;
local function minimapMover(self)
	self:ClearAllPoints();
	
	local uiScale = UIParent:GetScale();
	local cX, cY = GetCursorPosition();
	local mX, mY = Minimap:GetCenter();
	cX, cY = cX/uiScale, cY/uiScale;
	
	local width, height = (cX-mX), (cY-mY);
	local dist = sqrt(width^2 + height^2);
	if ( dist < 85 ) then
		-- Get angle
		local a = atan(height/width);
		if ( width < 0 ) then
			a = a + 180;
		end
		self:SetClampedToScreen(false);
		self:SetPoint("CENTER", Minimap, "CENTER", 80*cos(a), 80*sin(a));
	else
		self:SetClampedToScreen(true);
		self:SetPoint("CENTER", nil, "BOTTOMLEFT", cX, cY);
	end
end

local minimapFrame;

local function minimapResetPosition()
	minimapFrame:ClearAllPoints();
	minimapFrame:SetPoint("CENTER", UIParent, "CENTER", 0, 0);
	minimapFrame:SetUserPlaced(true);
end

local function minimapFrameSkeleton()
	return "button#n:ctmm#s:32:32#mid:bl:Minimap:15:15#st:LOW", {
		"texture#all#i:disabled#Interface\\AddOns\\CT_Core\\Images\\minimapIcon",
		"texture#all#i:enabled#hidden#Interface\\AddOns\\CT_Core\\Images\\minimapIconHighlight",
		
		["onclick"] = function(self)
			module:showControlPanel("toggle");
		end,
		
		["ondragstart"] = function(self)
			self:StartMoving();
			self:SetScript("OnUpdate", minimapMover);
		end,
		
		["ondragstop"] = function(self)
			self:StopMovingOrSizing();
			self:SetScript("OnUpdate", nil);
			
			local x, y = self:GetCenter();
			module:setOption("minimapX", x, true);
			module:setOption("minimapY", y, true);
		end,
		
		["onload"] = function(self)
			local highlight = self:CreateTexture(nil, "HIGHLIGHT");
			highlight:SetAllPoints(self);
			highlight:SetTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight");
			highlight:SetBlendMode("ADD");
			highlight:SetVertexColor(1, 1, 1, 0.5);
			
			self:RegisterForDrag("LeftButton");
			self:SetMovable(true);
			
			local x, y = module:getOption("minimapX"), module:getOption("minimapY");
			if ( x and y ) then
				self:ClearAllPoints();
				self:SetPoint("CENTER", nil, "BOTTOMLEFT", x, y);
				local mX, mY = Minimap:GetCenter();
				local width, height = (x-mX), (y-mY);
				local dist = sqrt(width^2 + height^2);
				if ( dist >= 85 ) then
					self:SetClampedToScreen(true);
				end
			end
		end
	}
end

local function showMinimap(enable)
	if ( not enable ) then
		if ( minimapFrame ) then
			minimapFrame:Hide();
		end
		return;
	end
	
	if ( not minimapFrame ) then
		minimapFrame = module:getFrame(minimapFrameSkeleton);
	else
		minimapFrame:Show();
	end
end

module:regEvent("CONTROL_PANEL_VISIBILITY", function(event, enabled)
	if ( minimapFrame ) then
		if ( enabled ) then
			minimapFrame.enabled:Show();
			minimapFrame.disabled:Hide();
		else
			minimapFrame.enabled:Hide();
			minimapFrame.disabled:Show();
		end
	end
end);

--------------------------------------------
-- Options
module.update = function(self, type, value)
	self:modupdate(type, value);
	self:chatupdate(type, value);
	if ( type == "init" or type == "minimapIcon" ) then
		showMinimap(self:getOption("minimapIcon") ~= false);
	end
end

-- Options frame
module.frame = function()
	local options = {};
	local yoffset = 0;

	-- Generic Options
	options["frame#tl#r"] = {
		"font#tl#v:GameFontNormalLarge#General Options",
		"checkbutton#o:questLevels#tl:10:-20#Display quest levels in the Quest Log",
		"checkbutton#tl:10:-40#o:tickMod#Display health/mana regeneration rates",
		"font#tl:60:-70#v:ChatFontNormal#Format:",
		"dropdown#tl:100:-70#s:125:50#o:tickModFormat#n:CTCoreDropdown1#Health - Mana#HP/Tick - MP/Tick#HP - MP",
		"font#tl:25:-90#r:-5:0#(Formerly CT_TickMod)#0.5:0.5:0.5",
		"checkbutton#tl:10:-100#o:blockBankTrades#Block trades while using the bank",
		"checkbutton#tl:10:-120#o:hideGryphons#Hide the Main Bar gryphons",
	};
	yoffset = yoffset + 160;
		
	-- Minimap Options
	options["frame#tl:0:-" .. yoffset .. "#r"] = {
		"font#tl#v:GameFontNormalLarge#Minimap Options",
		"checkbutton#tl:10:-20#o:hideWorldMap#Hide the World Map minimap button",
		"checkbutton#tl:10:-40#o:minimapIcon:true#Display the CTMod minimap icon",
		["button#t:0:-70#s:160:30#v:GameMenuButtonTemplate#Reset position"] = {
			["onclick"] = function(self)
				minimapResetPosition();
			end
		},
		"font#t:0:-105#s:0:40#l#r#Note: This will place the CTMod minimap icon at the center of your screen. From there it can dragged anywhere on the screen.#0.5:0.5:0.5",
	};
	yoffset = yoffset + 170;
		
	-- Chat Options
	options["frame#tl:0:-" .. yoffset .. "#r"] = {
		"font#tl#v:GameFontNormalLarge#Casting Bar Options",
		"checkbutton#tl:10:-20#o:castingTimers#Display casting bar timers",
		"checkbutton#tl:10:-40#o:castingbarEnabled#Use custom casting bar position",
		"checkbutton#tl:40:-60#o:castingbarMovable#Unlock the casting bar",
	};
	yoffset = yoffset + 100;

	-- Chat Options
	options["frame#tl:0:-" .. yoffset .. "#r"] = {
		"font#tl#v:GameFontNormalLarge#Chat Options",
		"checkbutton#tl:10:-20#o:chatTimestamp#Display chat timestamps",
		"font#tl:60:-50#v:ChatFontNormal#Format:",
		"dropdown#tl:100:-50#o:chatTimestampFormat#n:CTCoreDropdown2#s:100:50#12:00#12:00:00#24:00#24:00:00",
		"checkbutton#tl:10:-70#o:chatArrows#Hide chat arrows",
		"checkbutton#tl:10:-90#o:chatEditMove#Move chat input box to top",
		"checkbutton#tl:10:-110#o:chatScrolling#Enable chat scrolling",
		"font#tl:0:-135#r#(Ctrl+Scroll: One Page, Shift+Scroll: Start/End)#0.5:0.5:0.5",
	};
	yoffset = yoffset + 160;
		
	-- Quest / Achievement Tracker
	options["frame#tl:0:-" .. yoffset .. "#r"] = {
		"font#tl#v:GameFontNormalLarge#Quest/Achievement Trackers",
		"checkbutton#tl:10:-20#o:questTracker#Custom tracker positions",
		"font#tl:0:-40#r#s:0:40#Allows you to reposition the quest and achievement tracking information by dragging anchors attached to the frames.#0.5:0.5:0.5",
		"checkbutton#tl:30:-80#o:combinedAnchor:true#Combine the trackers",
		"font#tl:0:-100#r#s:0:40#You can combine both trackers so that only one anchor is needed to position them, or you can move each tracker separately.#0.5:0.5:0.5",
		"checkbutton#tl:30:-145#o:questTrackerAnchor:true#Unlock the trackers",
		"font#tl:0:-165#r#s:0:40#Drag an anchor to move the tracker. Right click the anchor to change the expand direction.#0.5:0.5:0.5",
		["button#t:0:-210#s:160:30#v:GameMenuButtonTemplate#Reset positions"] = {
			["onclick"] = function(self)
				CT_Core_QuestTracker_ResetAnchor();
				CT_Core_AchievementTracker_ResetAnchor();
			end
		},
	};
	yoffset = yoffset + 260;
		
	-- Tooltip Relocation
	options["frame#tl:0:-" .. yoffset .. "#r"] = {
		"font#tl#v:GameFontNormalLarge#Tooltip Relocation",
		"font#tl:10:-30#v:ChatFontNormal#Tooltip location:",
		"dropdown#tl:100:-30#o:tooltipRelocation#n:CTCoreDropdown3#s:125:50#Default#On Mouse#On Anchor",
		"checkbutton#tl:30:-50#o:tooltipRelocationAnchor#Show the 'On Anchor' tooltip anchor",
		"font#tl:0:-75#r#s:0:40#Drag the anchor to change where 'On Anchor' style tooltips are displayed. Right click the anchor to change the expand direction.#0.5:0.5:0.5",
		["button#t:0:-125#s:160:30#v:GameMenuButtonTemplate#Reset position"] = {
			["onclick"] = function(self)
				CT_Core_ResetTooltipAnchor();
			end
		},
	};
	yoffset = yoffset + 180;

	-- Reset Options
	options["frame#tl:0:-"..(yoffset).."#r"] = {
		"font#tl:5:0#v:GameFontNormalLarge#Reset Options",
		"checkbutton#tl:20:-20#o:resetAll#Reset options for all of your characters",
		["button#t:0:-50#s:120:30#v:UIPanelButtonTemplate#Reset options"] = {
			["onclick"] = function(self)
				if (module:getOption("resetAll")) then
					CT_CoreOptions = {};
				else
					if (not CT_CoreOptions or not type(CT_CoreOptions) == "table") then
						CT_CoreOptions = {};
					else
						CT_CoreOptions[module:getCharKey()] = nil;
					end
				end
				ConsoleExec("RELOADUI");
			end
		},
		"font#t:0:-80#s:0:40#l#r#Note: This will reset options to default and then reload your UI.#0.5:0.5:0.5",
	};

	return "frame#all", options;
end
