local AceAddon = AceLibrary("AceAddon-2.0")

local L = AceLibrary("AceLocale-2.2"):new("Cartographer_Hotspot")
L:RegisterTranslations("enUS", function() return {
	["Position"] = true,
	["Button position"] = true,
  
  	["Module description"] = "Minimap button for Cartographer",
	
	["Angle"] = true,
	["Button position angle"] = true,
	
	["Radius"] = true,
	["Button position radius"] = true,
	
	["On mouse hover"] = true,
	["When the mouse is hover the button"] = true,
	
	["Transparency"] = true,
	["Transparency of the World Map"] = true,

	["Overlay transparency"] = true,
	["Transparency of World Map overlays"] = true,

	["Scale"] = true,
	["Scale of the World Map"] = true,

	["Show buttons"] = true,
	["Show/hide top buttons"] = true,

	["Show coordinates"] = true,
	["Show/hide coordinates"] = true,
	
	["Reset"] = true,
	["Reset options"] = true,

	["Enable"] = true,
	
	-- *** NEW ***
	["Enable/disable options"] = true,
	
	["Enable alpha"] = true,
	["Enable/disable alpha option"] = true,
	
	["Enable overlay alpha"] = true,
	["Enable/disable overlay alpha option"] = true,
	
	["Enable scale"] = true,
	["Enable/disable scale option"] = true,
	
	["Enable position"] = true,
	["Enable/disable position option"] = true,
	
	["Suspend/resume 'on mouse hover'"] = true,
	
	["Position"] = true,
	["Set map position"] = true,
	
	["Horizontal position"] = true,
	["Set the horizontal position of the map"] = true,
	
	["Vertical position"] = true,
	["Set the vertical position of the map"] = true,
	
	["Current"] = true,
	["Set the map position to the current map position"] = true,
	-- ************
} end)

L:RegisterTranslations("deDE", function() return {
	["Position"] = "Position",
	["Button position"] = "Position des Minimap-Knopfes",
	
  ["Module description"] = "Minimap-Knopf f\195\188r Cartographer",
  
	["Angle"] = "Position",
	["Button position angle"] = "Position",
	
	["Radius"] = "Radius",
	["Button position radius"] = "Radius des Minimap-Knopfes",
	
	["On mouse hover"] = "Reagiert auf Ber\195\188hrung", -- suboptimal, any suggestions?
	["When the mouse is hover the button"] = "Aktiviert die Karte, wenn man mit der Maus auf den Knopf zeigt.",
	
	["Transparency"] = "Transparenz",
	["Transparency of the World Map"] = "Transparenz der Weltkarte",

	["Overlay transparency"] = "Overlay-Transparenz",
	["Transparency of World Map overlays"] = "Transparenz des Weltkarten-Overlays",

	["Scale"] = "Skalierung",
	["Scale of the World Map"] = "Skalierung der Weltkarte",
	
	["Show buttons"] = "Kn\195\182pfe anzeigen",
	["Show/hide top buttons"] = "Kn\195\182pfe oben an der Karte anzeigen/verstecken",

	["Show coordinates"] = "Koordinaten anzeigen",
	["Show/hide coordinates"] = "Koordinaten anzeigen/verstecken",
	
	["Reset"] = "Zur\195\188cksetzen",
	["Reset options"] = "Optionen auf Standardeinstellungen zur\195\188cksetzen",

	["Enable"] = "Aktiviert",
	["Suspend/resume 'on mouse hover'"] = "'Ber\195\188hrungsempfindlichkeit' deaktivieren/reaktivieren", -- suboptimal, any suggestions?
  
  	-- *** NEW ***
	["Enable/disable options"] = "Optionen ein- und ausschalten.",
	
	["Enable alpha"] = "Transparenz",
	["Enable/disable alpha option"] = "Transparenz-Option ein- und ausschalten.",
	
	["Enable overlay alpha"] = "Overlay-Transparenz",
	["Enable/disable overlay alpha option"] = "Overlay-Transparenz-Option ein- und ausschalten.",
	
	["Enable scale"] = "Skalierung",
	["Enable/disable scale option"] = "Skalierungsoption ein- und ausschalten.",
	
	["Enable position"] = "Position",
	["Enable/disable position option"] = "Positionsoption ein- und ausschalten.",
	
	["Position"] = "Position",
	["Set map position"] = "Kartenposition einstellen",
	
	["Horizontal position"] = "Horizontal",
	["Set the horizontal position of the map"] = "Horizontale Position der Karte einstellen.",
	
	["Vertical position"] = "Vertikal",
	["Set the vertical position of the map"] = "Vertikale Position der Karte einstellen.",
	
	["Current"] = "Aktuelle Position",
	["Set the map position to the current map position"] = "Stellt die Kartenposition auf die aktuelle Position der Weltkarte ein.",
	-- ************
  
} end)  

L:RegisterTranslations("koKR", function() return {
	["Position"] = "위치",
	["Button position"] = "버튼의 위치를 변경합니다.",
	
	["Angle"] = "각도",
	["Button position angle"] = "버튼 위치의 각도를 변경합니다.",
	
	["Radius"] = "지름",
	["Button position radius"] = "버튼 위치의 반지름을 변경합니다.",
	
	["On mouse hover"] = "마우스 오버",
	["When the mouse is hover the button"] = "마우스 오버시 세계 지도를 표시합니다.",
	
	["Transparency"] = "투명도",
	["Transparency of the World Map"] = "세계 지도의 투명도를 변경합니다.",

	["Overlay transparency"] = "오버레이 투명도",
	["Transparency of World Map overlays"] = "세계 지도의 오버레이 투명도를 변경합니다.",

	["Scale"] = "크기",
	["Scale of the World Map"] = "세계 지도의 크기를 변경합니다.",
	
	["Show buttons"] = "버튼 표시",
	["Show/hide top buttons"] = "버튼을 표시하거나 숨김니다.",

	["Show coordinates"] = "좌표 표시",
	["Show/hide coordinates"] = "좌표를 표시하거나 숨김니다.",
	
	["Reset"] = "초기화",
	["Reset options"] = "옵션을 초기화 합니다.",

	["Enable"] = "활성화",
	["Suspend/resume 'on mouse hover'"] = "마우스 오버 모듈을 켜거나 끔니다.",
} end)  

L:RegisterTranslations("zhTW", function() return {
	["Position"] = "位置",
	["Button position"] = "按鈕位置",
	
	["Angle"] = "角度",
	["Button position angle"] = "按鈕位置角度",
	
	["Radius"] = "半徑",
	["Button position radius"] = "按鈕位置半徑",
	
	["On mouse hover"] = "游標經過",
	["When the mouse is hover the button"] = "當游標經過按鈕",
	
	["Transparency"] = "透明度",
	["Transparency of the World Map"] = "世界地圖的透明度",

	["Overlay transparency"] = "外框透明度",
	["Transparency of World Map overlays"] = "世界地圖外框的透明度",

	["Scale"] = "縮放",
	["Scale of the World Map"] = "世界地圖的大小",
	
	["Show buttons"] = "顯示按鈕",
	["Show/hide top buttons"] = "顯示按鈕",

	["Show coordinates"] = "顯示座標",
	["Show/hide coordinates"] = "顯示/隱藏座標",
	
	["Reset"] = "重置",
	["Reset options"] = "重置選項",

	["Enable"] = "開啟",
	["Suspend/resume 'on mouse hover'"] = "暫停/回復 '游標經過'",
} end)

L:RegisterTranslations("esES", function() return {
	["Position"] = "Posici\195\179n",
	["Button position"] = "Posici\195\179n del bot\195\179n",
  
  	["Module description"] = "Bot\195\179n de minimapa para Cartographer",
	
	["Angle"] = "\195\129ngulo",
	["Button position angle"] = "Posici\195\179n en \195\161ngulo del bot\195\179n",
	
	["Radius"] = "Radio",
	["Button position radius"] = "Distancia desde el centro de la posici\195\179n del bot\195\179n",
	
	["On mouse hover"] = "Al pasar el cursor por encima",
	["When the mouse is hover the button"] = "Cuando el cursor est\195\161 sobre el bot\195\179n",
	
	["Transparency"] = "Transparencia",
	["Transparency of the World Map"] = "Transparencia del Mapa del Mundo",

	["Overlay transparency"] = "Transparencia de superpuestos",
	["Transparency of World Map overlays"] = "Transparencia de los superpuestos del Mapa del Mundo",

	["Scale"] = "Escala",
	["Scale of the World Map"] = "Escala del Mapa del Mundo",
	
	["Show buttons"] = "Mostrar botones",
	["Show/hide top buttons"] = "Determina si se muestran los botones superiores",

	["Show coordinates"] = "Mostrar coordenadas",
	["Show/hide coordinates"] = "Determina si se muestran las coordenadas",
	
	["Reset"] = "Reestablecer",
	["Reset options"] = "Opciones de reestablecimiento",

	["Enable"] = "Activar",
	["Suspend/resume 'on mouse hover'"] = "Determina si se activa 'al pasar el cursor por encima'",
} end)


local Cartographer_Hotspot = Cartographer:NewModule("Hotspot", "AceHook-2.1", "AceConsole-2.0")


function Cartographer_Hotspot:OnInitialize()
	self.db = Cartographer:AcquireDBNamespace("Hotspot");
	self.looknfeel = Cartographer:AcquireDBNamespace("LookNFeel");
	Cartographer:RegisterDefaults("Hotspot", "profile", {
		angle = 43,
		radius = 78,
		showButtons = true,
		showCoords = true,
		enableAlpha = true,
		enableOverlayAlpha = true,
		enableScale = true,
		enableButtons = true,
		enableAll = true,
		xPosition = self.looknfeel.profile.x or 0,
		yPosition = self.looknfeel.profile.y or 0,
		alpha = self.looknfeel.profile.alpha or 100,
		overlayAlpha = self.looknfeel.profile.overlayAlpha or 100,
		scale = self.looknfeel.profile.scale or 1,
	})
	
	self.hotspotOpen = false;
	self.oldButtonPosition = { MiniMapWorldMapButton:GetPoint() };
	self.oldPosition = {};
	self.oldAlpha = 0;
	self.oldOverlayAlpha = 0;
	self.oldScale = 0;
	
	Cartographer.options.args["Hotspot"] = {
		name = "Hotspot",
		desc = L["Module description"],
		type = 'group',
		handler = self,
		args = { 
			position = {
				name = L["Position"],
				desc = L["Button position"],
				type = 'group',
				args = {
					angle = {
						name = L["Angle"],
						desc = L["Button position angle"],
						type = 'range',
						min = 0,
						max = 360,
						step = 1,
						get = "GetAngle",
						set = "SetAngle",
					},
					radius = {
						name = L["Radius"],
						desc = L["Button position radius"],
						type = 'range',
						min = 20,
						max = 150,
						step = 1,
						get = "GetRadius",
						set = "SetRadius",
					},
				
				},
			},
			mouseHover = {
				name = L["On mouse hover"],
				desc = L["When the mouse is hover the button"],
				type = 'group',
				args = {
					alpha = {
						name = L["Transparency"],
						desc = L["Transparency of the World Map"],
						type = 'range',
						min = 0,
						max = 1,
						step = 0.05,
						isPercent = true,
						hidden = function() return not Cartographer:IsModuleActive("LookNFeel") end,
						disabled = "~IsEnableAll",
						get = "GetAlpha",
						set = "SetAlpha",
						order = 1,
					},
					overlayAlpha = {
						name = L["Overlay transparency"],
						desc = L["Transparency of World Map overlays"],
						type = 'range',
						min = 0.25,
						max = 1,
						step = 0.05,
						isPercent = true,
						hidden = function() return not Cartographer:IsModuleActive("LookNFeel") end,
						disabled = "~IsEnableAll",
						get = "GetOverlayAlpha",
						set = "SetOverlayAlpha",
						order = 2,
					},
					scale = {
						name = L["Scale"],
						desc = L["Scale of the World Map"],
						type = 'range',
						min = 0.2,
						max = 1,
						step = 0.05,
						isPercent = true,
						hidden = function() return not Cartographer:IsModuleActive("LookNFeel") end,
						disabled = "~IsEnableAll",
						get = "GetScale",
						set = "SetScale",
						order = 3,
					},
					buttons = {
	 				    name = L["Show buttons"],
	 				    desc = L["Show/hide top buttons"],
				    	type = 'toggle',
				    	disabled = "~IsEnableAll",
	 				    get = "IsShowingButtons",
	 				    set = "ToggleShowButtons",
	 				    order = 5,
	 				},
	 				coords = {
	 				    name = L["Show coordinates"],
	 				    desc = L["Show/hide coordinates"],
				    	type = 'toggle',
				    	hidden = function() return not Cartographer:IsModuleActive("Coordinates") end,
				    	disabled = "~IsEnableAll",
	 				    get = "IsShowingCoords",
	 				    set = "ToggleShowCoords",
	 				    order = 6,
	 				},
	 				reset = {
	 				    name = L["Reset"],
	 				    desc = L["Reset options"],
	 				    type = 'execute',
          				func = "ResetOnMouseHover",
          				order = -2,
	 				},
	 				enable = {
	 				    name = L["Enable"],
	 				    desc = L["Enable/disable options"],
	 				    type = 'group',
						args = {
							enableAlpha = {
	 				    		name = L["Enable alpha"],
	 				    		desc = L["Enable/disable alpha option"],
				    			type = 'toggle',
				    			disabled = "~IsEnableAll",
	 				    		get = "IsEnableAlpha",
	 				    		set = function() self.db.profile.enableAlpha = not self.db.profile.enableAlpha; end,
	 				    		order = 1,

	 						},
	 						enableOverlayAlpha = {
	 				    		name = L["Enable overlay alpha"],
	 				    		desc = L["Enable/disable overlay alpha option"],
				    			type = 'toggle',
				    			disabled = "~IsEnableAll",
	 				    		get = "IsEnableOverlayAlpha",
	 				    		set = function() self.db.profile.enableOverlayAlpha = not self.db.profile.enableOverlayAlpha; end,
	 				    		order = 2,

	 						},
	 						enableScale = {
	 				    		name = L["Enable scale"],
	 				    		desc = L["Enable/disable scale option"],
				    			type = 'toggle',
				    			disabled = "~IsEnableAll",
	 				    		get = "IsEnableScale",
	 				    		set = function() self.db.profile.enableScale = not self.db.profile.enableScale; end,
	 				    		order = 3,

	 						},
	 						enablePosition = {
	 				    		name = L["Enable position"],
	 				    		desc = L["Enable/disable position option"],
				    			type = 'toggle',
				    			disabled = "~IsEnableAll",
	 				    		get = "IsEnablePosition",
	 				    		set = function() self.db.profile.enablePosition = not self.db.profile.enablePosition; end,
	 				    		order = 4,

	 						},
                            enableAll = {
	 				    		name = L["Enable"],
	 				    		desc = L["Suspend/resume 'on mouse hover'"],
				    			type = 'toggle',
	 				    		get = "IsEnableAll",
	 				    		set = function() self.db.profile.enableAll = not self.db.profile.enableAll; end,
	 				    		order = -1,

	 						},
						},
	 				    order = -1,
	 				},
	 				position = {
					 	name = L["Position"],
						desc = L["Set map position"],
						type = 'group',
						order = 4,
						hidden = function() return not Cartographer:IsModuleActive("LookNFeel") end,
						disabled = "~IsEnableAll",
						args = {
						    xPos = {
								name = L["Horizontal position"],
								desc = L["Set the horizontal position of the map"],
								type = 'text',
								usage = "<number>",
								hidden = function() return not Cartographer:IsModuleActive("LookNFeel") end,
								disabled = "~IsEnableAll",
								validate = function(str) return tonumber(str); end, 
								get = "GetXPosition",
								set = "SetXPosition",
								order = 1,
							},
							yPos = {
								name = L["Vertical position"],
								desc = L["Set the vertical position of the map"],
								type = 'text',
								usage = "<number>",
								hidden = function() return not Cartographer:IsModuleActive("LookNFeel") end,
								disabled = "~IsEnableAll",
								validate = function(str) return tonumber(str); end, 
								get = "GetYPosition",
								set = "SetYPosition",
								order = 2,
							},
							reset = {
	 				    		name = L["Current"],
	 				    		desc = L["Set the map position to the current map position"],
	 				    		type = 'execute',
	 				    		hidden = function() return not Cartographer:IsModuleActive("LookNFeel") end,
								disabled = "~IsEnableAll",
          						func = "SetCurrentPosition",
          						order = 3,
	 						},
						},
	 				},
				},
			},
			toggle = {
				name = AceLibrary("AceLocale-2.2"):new("Cartographer")["Enabled"],
				desc = AceLibrary("AceLocale-2.2"):new("Cartographer")["Suspend/resume this module."],
				type  = 'toggle',
				order = -1,
				get   = function() return Cartographer:IsModuleActive(self) end,
				set   = function() Cartographer:ToggleModuleActive(self) end,
			},
		}	
	}	
end

function Cartographer_Hotspot:OnEnable()
	self:HookScript(MiniMapWorldMapButton, "OnEnter");
	self:HookScript(MiniMapWorldMapButton, "OnLeave");
	self:HookScript(MiniMapWorldMapButton, "OnClick");
	
	self:SecureHook(MiniMapWorldMapButton, "Hide");
	
	self:UpdateButtonPosition();
	
	MiniMapWorldMapButton:Show();
end

function Cartographer_Hotspot:OnDisable()
    MiniMapWorldMapButton:ClearAllPoints();
	MiniMapWorldMapButton:SetPoint(unpack(self.oldButtonPosition));
end

function Cartographer_Hotspot:OnClick()
	this:SetFrameStrata("LOW");
	
	if not WorldMapFrame:IsShown() then
		WorldMapFrame:Show();
	elseif WorldMapFrame:IsShown() and not self.hotspotOpen then
		WorldMapFrame:Hide();
	end

 	self.hotspotOpen = false;
end

function Cartographer_Hotspot:OnEnter()
	if not self:IsEnableAll() then return end
	
	if Cartographer:IsModuleActive("LookNFeel") then
	    local LookNFeel = Cartographer:GetModule("LookNFeel");

		if self:IsEnableAlpha() then
			self.oldAlpha = LookNFeel:GetAlpha();
			
			LookNFeel:SetAlpha(self.db.profile.alpha);
		end
		
		if self:IsEnableOverlayAlpha() then
			self.oldOverlayAlpha = LookNFeel:GetOverlayAlpha();
			
			LookNFeel:SetOverlayAlpha(self.db.profile.overlayAlpha);
		end
		
		if self:IsEnableScale() then
			self.oldScale = LookNFeel:GetScale();
			
			LookNFeel:SetScale(self.db.profile.scale);
		end
		
		if self:IsEnablePosition() then
			self.oldPosition = { WorldMapFrame:GetPoint("CENTER") };
			
			WorldMapFrame:ClearAllPoints();
			WorldMapFrame:SetPoint(self:GetMapPosition());
		end    
	end
	
	if not self:IsShowingCoords() and Cartographer:IsModuleActive("Coordinates") then
		CartographerCoordinates:Hide();
		CartographerCoordinatesBackground1:Hide();
		CartographerCoordinatesBackground2:Hide();	
	end
	
	if not self:IsShowingButtons() then
		CartographerGoToButton:Hide();
		CartographerOptionsButton:Hide();
		
		if Cartographer:IsModuleActive("LookNFeel") then
			CartographerLookNFeelLockButton:Hide();
		end	
	end
	    
	if not WorldMapFrame:IsShown() then
		this:SetFrameStrata("DIALOG");
		WorldMapFrame:Show();
		self.hotspotOpen = true;
	end
end

function Cartographer_Hotspot:OnLeave()
	if not self:IsEnableAll() then return end
	
	if Cartographer:IsModuleActive("LookNFeel") then
	    local LookNFeel = Cartographer:GetModule("LookNFeel");

        if self:IsEnableAlpha() then
			LookNFeel:SetAlpha(self.oldAlpha);
		end
		
		if self:IsEnableOverlayAlpha() then
			LookNFeel:SetOverlayAlpha(self.oldOverlayAlpha);
		end
		
		if self:IsEnableScale() then
			LookNFeel:SetScale(self.oldScale);
		end
		
		if self:IsEnablePosition() then
			WorldMapFrame:ClearAllPoints();
			WorldMapFrame:SetPoint(unpack(self.oldPosition));
		end 
 
	    CartographerLookNFeelLockButton:Show();
	end
	
	if Cartographer:IsModuleActive("Coordinates") then
		CartographerCoordinates:Show();
		
		if Cartographer:GetModule("Coordinates"):IsShowingBackground() then	
			CartographerCoordinatesBackground1:Show();
			CartographerCoordinatesBackground2:Show();
		end
	end
	
	CartographerGoToButton:Show();
	CartographerOptionsButton:Show();  
	    
	if self.hotspotOpen then
		WorldMapFrame:Hide();
		this:SetFrameStrata("LOW");
		self.hotspotOpen = false;
	end
end

function Cartographer_Hotspot:Hide()
	MiniMapWorldMapButton:Show();
end

function Cartographer_Hotspot:UpdateButtonPosition()
	MiniMapWorldMapButton:SetPoint("TOPLEFT","Minimap","TOPLEFT",54 - (self.db.profile.radius * cos(self.db.profile.angle)), (self.db.profile.radius * sin(self.db.profile.angle)) - 55);
end

function Cartographer_Hotspot:GetMapPosition()
	return "CENTER", "UIParent", "CENTER", self.db.profile.xPosition, self.db.profile.yPosition;
end

function Cartographer_Hotspot:SetXPosition(x)
    self.db.profile.xPosition = tonumber(x);
end

function Cartographer_Hotspot:GetXPosition()
    return floor(self.db.profile.xPosition + 0.5);
end

function Cartographer_Hotspot:SetYPosition(y)
    self.db.profile.yPosition = tonumber(y);
end

function Cartographer_Hotspot:GetYPosition()
    return floor(self.db.profile.yPosition + 0.5);
end

function Cartographer_Hotspot:SetCurrentPosition()
	self.db.profile.xPosition = self.looknfeel.profile.x;
	self.db.profile.yPosition = self.looknfeel.profile.y;
end

function Cartographer_Hotspot:GetRadius()
	return self.db.profile.radius;
end

function Cartographer_Hotspot:SetRadius(r)
	self.db.profile.radius = r;
	self:UpdateButtonPosition();
end

function Cartographer_Hotspot:GetAngle()
	return self.db.profile.angle;
end

function Cartographer_Hotspot:SetAngle(r)
	self.db.profile.angle = r;
	self:UpdateButtonPosition();
end

function Cartographer_Hotspot:GetAlpha()
	return self.db.profile.alpha;
end

function Cartographer_Hotspot:SetAlpha(r)
	self.db.profile.alpha = r;
end

function Cartographer_Hotspot:GetOverlayAlpha()
	return self.db.profile.overlayAlpha;
end

function Cartographer_Hotspot:SetOverlayAlpha(r)
	self.db.profile.overlayAlpha = r;
end

function Cartographer_Hotspot:GetScale()
	return self.db.profile.scale;
end

function Cartographer_Hotspot:SetScale(r)
	self.db.profile.scale = r;
end

function Cartographer_Hotspot:IsOnMouseHover()
	return self.db.profile.onMouseHover;
end

function Cartographer_Hotspot:IsShowingButtons()
	return self.db.profile.showButtons;
end

function Cartographer_Hotspot:ToggleShowButtons()
	self.db.profile.showButtons = not self.db.profile.showButtons;
end

function Cartographer_Hotspot:IsShowingCoords()
	return self.db.profile.showCoords;
end

function Cartographer_Hotspot:ToggleShowCoords()
	self.db.profile.showCoords = not self.db.profile.showCoords;
end

function Cartographer_Hotspot:ResetOnMouseHover()
	self.db.profile.alpha = self.looknfeel.profile.alpha or 100;
	self.db.profile.overlayAlpha = self.looknfeel.profile.overlayAlpha or 100;
	self.db.profile.scale = self.looknfeel.profile.scale or 1;
	self.db.profile.showButtons = true;
	self.db.profile.showCoords = true;
end

function Cartographer_Hotspot:IsEnableAlpha()
	return self.db.profile.enableAlpha;
end

function Cartographer_Hotspot:IsEnableOverlayAlpha()
	return self.db.profile.enableOverlayAlpha;
end

function Cartographer_Hotspot:IsEnableScale()
	return self.db.profile.enableScale;
end

function Cartographer_Hotspot:IsEnablePosition()
	return self.db.profile.enablePosition;
end

function Cartographer_Hotspot:IsEnableAll()
	return self.db.profile.enableAll;
end


                                         