-----------------------------------------------------------------------
-- AddOn namespace.
-----------------------------------------------------------------------
local ADDON_NAME, private = ...

local LibStub = _G.LibStub
local ADDON_NAME, private = ...

-- RareScanner database libraries
local RSContainerDB = private.ImportLib("RareScannerContainerDB")
local RSConfigDB = private.ImportLib("RareScannerConfigDB")
local RSNpcDB = private.ImportLib("RareScannerNpcDB")
local RSEventDB = private.ImportLib("RareScannerEventDB")
local RSGeneralDB = private.ImportLib("RareScannerGeneralDB")

-- RareScanner general libraries
local RSConstants = private.ImportLib("RareScannerConstants")

-- RareScanner services libraries
local RSMap = private.ImportLib("RareScannerMap")
local RSTooltip = private.ImportLib("RareScannerTooltip")
local RSMinimap = private.ImportLib("RareScannerMinimap")
local RSWaypoints = private.ImportLib("RareScannerWaypoints")
local RSTomtom = private.ImportLib("RareScannerTomtom")
local RSProvider = private.ImportLib("RareScannerProvider")

-- Locales
local AL = LibStub("AceLocale-3.0"):GetLocale("RareScanner");


RSVignettePinMixin = CreateFromMixins(VignettePinMixin);

RSVignettePinMixin.SetPassThroughButtons = function() end

function RSVignettePinMixin:OnAcquired(vignetteGUID, vignetteInfo, frameIndex)
	-- ORIGINAL CODE
	if (not self:IsSuperTrackingExternallyHandled()) then
		--self:UpdateMousePropagation();
		self:UpdateSuperTrackedState(C_SuperTrack[self:GetSuperTrackAccessorAPIName()]());
	end
	
	self.dataProvider = vignetteInfo.dataProvider;
	self.vignetteGUID = vignetteGUID;
	self.name = vignetteInfo.name;
	self.hasTooltip = vignetteInfo.hasTooltip or vignetteInfo.type == Enum.VignetteType.PvPBounty;
	self.isUnique = vignetteInfo.isUnique;
	self.vignetteID = vignetteInfo.vignetteID;
	self.tooltipWidgetSet = vignetteInfo.tooltipWidgetSet;
	self.iconWidgetSet = vignetteInfo.iconWidgetSet;
	self.vignetteInfo = vignetteInfo;
	self:ApplyTextures();
	self:UpdateFogOfWar(vignetteInfo);
	self:ApplyCurrentAlpha();
	self:UpdatePosition();
	self:UpdateSupertrackedHighlight();
	self:AddIconWidgets();
	self:SetFrameLevelType(frameIndex);
	
	-- ADDITIONS
	
	-- Overrides mouse motion if overriding ingame tooltips
	local POI = RSMap.GetWorldMapPOI(self:GetObjectGUID(), self.vignetteInfo, self:GetMap():GetMapID())
	if (RSConfigDB.IsShowingTooltipsOnIngameIcons() and POI) then
		self:EnableMouseMotion(true);
	else
		self:EnableMouseMotion(self.hasTooltip);
	end
	
	-- Extract container's name if we don't have it
	local _, _, _, _, _, vignetteObjectID = strsplit("-", self:GetObjectGUID())
	if (self:GetVignetteType() == Enum.VignetteType.Treasure) then
		local containerID = tonumber(vignetteObjectID)
		if (not RSContainerDB.GetContainerName(containerID) and self:GetVignetteName()) then
			RSContainerDB.SetContainerName(containerID, self:GetVignetteName())
		end
	elseif (self:GetVignetteType() == Enum.VignetteType.Normal) then
		if (RSConstants.IsContainerAtlas(self.vignetteInfo.atlasName)) then
			local containerID = tonumber(vignetteObjectID)
			if (not RSContainerDB.GetContainerName(containerID) and self:GetVignetteName()) then
				RSContainerDB.SetContainerName(containerID, self:GetVignetteName())
			end
		end
	end
end

function RSVignettePinMixin:OnMouseEnter()
	if (RSConfigDB.IsShowingTooltipsOnIngameIcons()) then
		local POI = RSMap.GetWorldMapPOI(self:GetObjectGUID(), self.vignetteInfo, self:GetMap():GetMapID())
		if (POI) then
			self.POI = POI
			
			-- Just in case the user didnt have the questID when he found it
			if (POI.isOpened) then
				RSContainerDB.DeleteContainerOpened(POI.entityID)
			elseif (POI.isDead) then
				RSNpcDB.DeleteNpcKilled(POI.entityID)
			elseif (POI.isCompleted) then
				RSEventDB.DeleteEventCompleted(POI.entityID)
			end
			
			RSTooltip.ShowSimpleTooltip(self)
		else
			self.tooltip = nil
			self.POI = nil
		end
	else
		VignettePinMixin.OnMouseEnter(self)
	end
end

function RSVignettePinMixin:OnMouseLeave()
	if (RSConfigDB.IsShowingTooltipsOnIngameIcons()) then
		if (RSTooltip.HideTooltip(self.tooltip)) then
			self.tooltip = nil
		end
	else
		VignettePinMixin.OnMouseLeave(self)
	end
end

function RSVignettePinMixin:OnMouseDown(button)
	if (RSConfigDB.IsShowingTooltipsOnIngameIcons()) then
		local POI = RSMap.GetWorldMapPOI(self:GetObjectGUID(), self.vignetteInfo, self:GetMap():GetMapID())
		if (not POI) then	
			return
		end
				
		if (button == "LeftButton") then				
			--Toggle state (if showing tooltips)
			if (IsShiftKeyDown() and IsAltKeyDown()) then
				if (POI.isNpc) then
					if (RSConfigDB.GetDefaultNpcFilter() == RSConstants.ENTITY_FILTER_ALERTS) then
						RSConfigDB.SetNpcFiltered(POI.entityID, RSConstants.ENTITY_FILTER_ALL)
					else
						RSConfigDB.SetNpcFiltered(POI.entityID)
					end
					self:Hide();
				elseif (POI.isContainer) then
					if (RSConfigDB.GetDefaultContainerFilter() == RSConstants.ENTITY_FILTER_ALERTS) then
						RSConfigDB.SetContainerFiltered(POI.entityID, RSConstants.ENTITY_FILTER_ALL)
					else
						RSConfigDB.SetContainerFiltered(POI.entityID)
					end
					self:Hide();
				elseif (POI.isEvent) then
					if (RSConfigDB.GetDefaultEventFilter() == RSConstants.ENTITY_FILTER_ALERTS) then
						RSConfigDB.SetEventFiltered(POI.entityID, RSConstants.ENTITY_FILTER_ALL)
					else
						RSConfigDB.SetEventFiltered(POI.entityID)
					end
					self:Hide();
				end
				RSProvider.RefreshAllDataProviders()
				RSMinimap.RefreshEntityState(POI.entityID)
			end
		elseif (button == "RightButton") then						
			-- Add waypoint
			if (IsShiftKeyDown()) then
				if (RSConfigDB.IsAddingWorldMapTomtomWaypoints()) then
					RSTomtom.AddWorldMapTomtomWaypoint(POI.mapID, POI.x, POI.y, POI.name)
				end
				if (RSConfigDB.IsAddingWorldMapIngameWaypoints()) then
					RSWaypoints.AddWorldMapWaypoint(POI.mapID, POI.x, POI.y)
				end
			elseif (not IsShiftKeyDown() and not IsAltKeyDown() and not IsControlKeyDown()) then
				-- If already showing a guide toggle it first
				if (self:GetMap():GetNumActivePinsByTemplate("RSGuideTemplate") > 0) then	
					self:GetMap():RemoveAllPinsByTemplate("RSGuideTemplate");
										
					local guideEntityID = RSGeneralDB.GetGuideActive()
					if (guideEntityID) then
						-- If same guide showing then disable it
						if (guideEntityID ~= POI.entityID) then
							RSGeneralDB.SetGuideActive(POI.entityID)
							RareScannerDataProviderMixin.ShowGuideLayer(self, POI.entityID, self:GetMap():GetMapID())
						else
							RSGeneralDB.RemoveGuideActive()
						end
					end
				-- Otherwise show it
				else
					RSGeneralDB.SetGuideActive(POI.entityID)
					RareScannerDataProviderMixin.ShowGuideLayer(self, POI.entityID, self:GetMap():GetMapID())
				end
			end

			-- Refresh minimap
			RSMinimap.RefreshAllData(true)
		end
	end
end