
local ipairs = ipairs;
local UnitLevel = UnitLevel;
local C_Map = C_Map;
local GetQuestDifficultyColor = GetQuestDifficultyColor;
local MouseIsOver = MouseIsOver;
local WorldMapFrame = WorldMapFrame;
local QuestDifficultyColors = QuestDifficultyColors;
local UIDD = LibStub:GetLibrary("LibUIDropDownMenu-4.0");

---->	Hide taint
	-- Hide world map dropdown menus to prevent GuildControlSetRank() taint
	WorldMapZoneDropDown:Hide()
	WorldMapContinentDropDown:Hide()

	-- Hide zone map dropdown menu as it's shown in the main panel
	WorldMapZoneMinimapDropDown:Hide()

	-- Hide right-click to zoom out button and message
	WorldMapZoomOutButton:Hide()
	WorldMapMagnifyingGlassButton:Hide()

---->	Reimp

	local function CreateDropDown(parent, name, labeltext)

		local dd = CreateFrame('FRAME', name, parent, "UIDropDownMenuTemplate");
		dd:SetScript("OnHide", nil);

		local label = dd:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall");
		label:SetPoint("TOPLEFT", dd, 20, 10);
		label:SetJustifyH("LEFT");
		label:SetText(labeltext);

		return dd;
	end

	local _WorldMapContinentDropDown;
	local function ContinentChecked(self)
		local continentInfo = WorldMapFrame:GetCurrentMapContinent();
		return continentInfo ~= nil and continentInfo.mapID == self.value;
	end
	local function ContinentFunc(self)
		WorldMapFrame:SetMapID(self.value);
		UIDD:UIDropDownMenu_SetSelectedValue(_WorldMapContinentDropDown, self.value);
	end
	local ContinentEntry = nil;
	local function ContinentInitialize(self, level)
		if ContinentEntry == nil then
			local children = C_Map.GetMapChildrenInfo(947);
			if children ~= nil then
				ContinentEntry = {  };
				for _, mapInfo in ipairs(children) do
					if mapInfo.mapType == Enum.UIMapType.Continent then
										ContinentEntry[#ContinentEntry + 1] = {
											value = mapInfo.mapID,
											text = mapInfo.name,
											func = ContinentFunc,
											checked = ContinentChecked,
											classicChecks = true,
										};
					elseif mapInfo.mapType == Enum.UIMapType.World then
						local children = C_Map.GetMapChildrenInfo(mapInfo.mapID);
						if children ~= nil then
							for _, mapInfo in ipairs(children) do
								if mapInfo.mapType == Enum.UIMapType.Continent then
										ContinentEntry[#ContinentEntry + 1] = {
											value = mapInfo.mapID,
											text = mapInfo.name,
											func = ContinentFunc,
											checked = ContinentChecked,
											classicChecks = true,
										};
								end
							end
						end
					end
				end
			end
		end
		for _, entry in ipairs(ContinentEntry) do
			entry.checked = ContinentChecked;
			UIDD:UIDropDownMenu_AddButton(entry, level);
		end
		local continentInfo = WorldMapFrame:GetCurrentMapContinent();
		if continentInfo ~= nil then
			UIDD:UIDropDownMenu_SetSelectedValue(_WorldMapContinentDropDown, continentInfo.mapID);
		end
	end

	local _WorldMapZoneDropDown;
	local function ZoneChecked(self)
		return WorldMapFrame:GetMapID() == self.value;
	end
	local function ZoneFunc(self)
		WorldMapFrame:SetMapID(self.value);
		UIDD:UIDropDownMenu_SetSelectedValue(_WorldMapZoneDropDown, self.value);
	end
	local ZoneEntry = {  };
	local function ZoneInitialize(self, level)
		local continentInfo = WorldMapFrame:GetCurrentMapContinent();
		if continentInfo ~= nil then
			local cid = continentInfo.mapID;
			local Z = ZoneEntry[cid];
			if Z == nil then
				local children = C_Map.GetMapChildrenInfo(cid);
				if children ~= nil then
					Z = {  };
					for _, mapInfo in ipairs(children) do
						Z[#Z + 1] = {
							value = mapInfo.mapID,
							text = mapInfo.name,
							func = ZoneFunc,
							checked = ZoneChecked,
							classicChecks = true,
						};
					end
					table.sort(Z, function(entry1, entry2) return entry1.text < entry2.text; end);
					ZoneEntry[cid] = Z;
				end
			end
			for _, entry in ipairs(Z) do
				entry.checked = ZoneChecked;
				UIDD:UIDropDownMenu_AddButton(entry, level);
			end
		end
		UIDD:UIDropDownMenu_SetSelectedValue(_WorldMapZoneDropDown, WorldMapFrame:GetMapID());
	end

	local _WorldMapZoomOutButton;

	local _WorldMapZoneMinimapDropDown;
	local function ZoneMinimapChecked(self)
		return GetCVar("showBattlefieldMinimap") == self.value;
	end
	local function ZoneMinimapFunc(self)
		SetCVar("showBattlefieldMinimap", self.value);
		UIDD:UIDropDownMenu_SetSelectedValue(_WorldMapZoneMinimapDropDown, self.value);
		if (DoesInstanceTypeMatchBattlefieldMapSettings()) then
			if BattlefieldMapFrame == nil then
				BattlefieldMap_LoadUI();
			end
			BattlefieldMapFrame:Show();
		else
			if BattlefieldMapFrame ~= nil then
				BattlefieldMapFrame:Hide();
			end
		end
	end
	local ZoneMinimapEntry = {
		{
			value = "0",
			text = BATTLEFIELD_MINIMAP_SHOW_NEVER,
			func = ZoneMinimapFunc,
			checked = ZoneMinimapChecked,
			classicChecks = true,
		},
		{
			value = "1",
			text = BATTLEFIELD_MINIMAP_SHOW_BATTLEGROUNDS,
			func = ZoneMinimapFunc,
			checked = ZoneMinimapChecked,
			classicChecks = true,
		},
		{
			value = "2",
			text = BATTLEFIELD_MINIMAP_SHOW_ALWAYS,
			func = ZoneMinimapFunc,
			checked = ZoneMinimapChecked,
			classicChecks = true,
		},
	};
	local function ZoneMinimapInitialize(self, level)
		for _, entry in ipairs(ZoneMinimapEntry) do
			entry.checked = ZoneMinimapChecked;
			UIDD:UIDropDownMenu_AddButton(entry, level);
		end
		UIDD:UIDropDownMenu_SetSelectedValue(_WorldMapZoneMinimapDropDown, GetCVar("showBattlefieldMinimap"));
	end


	local ActuallyParentMap = {
		[1453] = 1429,	--	暴风城
		[1455] = 1426,	--	铁炉堡
		[1457] = 1438,	--	达纳苏斯
		[1947] = 1943,	--	埃索达
		[1454] = 1411,	--	奥格瑞玛
		[1456] = 1412,	--	雷霆崖
		[1458] = 1420,	--	幽暗城
		[1954] = 1941,	--	银月城
		[1955] = 1952,	--	沙塔斯城
	};
	local currMapID = WorldMapFrame:GetMapID();
	local prevMapID = WorldMapFrame:GetMapID();
	local mapCallback = CreateFromMixins(MapCanvasDataProviderMixin);
	function mapCallback:OnMapChanged()
		--  Optionally override in your mixin, called when map ID changes
		local mapID = WorldMapFrame:GetMapID();
		if mapID ~= nil then
			local mapInfo = C_Map.GetMapInfo(mapID);
			if mapInfo.mapType > Enum.UIMapType.Continent then
				_WorldMapZoneDropDown.Text:SetText(mapInfo.name);
			else
				_WorldMapZoneDropDown.Text:SetText(nil);
			end
			if mapID ~= currMapID then
				prevMapID = currMapID;
				currMapID = mapID;
			end
		else
			_WorldMapZoneDropDown.Text:SetText(nil);
		end
		local continentInfo = WorldMapFrame:GetCurrentMapContinent();
		if continentInfo ~= nil then
			_WorldMapContinentDropDown.Text:SetText(continentInfo.name);
		else
			_WorldMapContinentDropDown.Text:SetText(nil);
		end
	end
	hooksecurefunc(WorldMapFrame, "NavigateToParentMap", function()
		--	after "MapCanvasDataProviderMixin:OnMapChanged()"
		if prevMapID ~= currMapID then
			local parentMap = ActuallyParentMap[prevMapID];
			if parentMap ~= nil then
				WorldMapFrame:SetMapID(parentMap);
			end
		end
	end);

	local function reimp()
		_WorldMapContinentDropDown = CreateDropDown(WorldMapFrame, "_WorldMapContinentDropDown", CONTINENT);
		_WorldMapContinentDropDown:SetPoint("LEFT", WorldMapContinentDropDown, 10, 0);
		UIDD:UIDropDownMenu_SetWidth(_WorldMapContinentDropDown, 130, 32);
		UIDD:UIDropDownMenu_Initialize(_WorldMapContinentDropDown, ContinentInitialize);
		_WorldMapContinentDropDown.Button:SetScript("OnClick", function(self)
			UIDD:ToggleDropDownMenu(1, nil, _WorldMapContinentDropDown);
		end);
		--
		_WorldMapZoneDropDown = CreateDropDown(WorldMapFrame, "_WorldMapZoneDropDown", ZONE);
		_WorldMapZoneDropDown:SetPoint("RIGHT", WorldMapZoneDropDown, -10, 0);
		UIDD:UIDropDownMenu_SetWidth(_WorldMapZoneDropDown, 130, 32);
		UIDD:UIDropDownMenu_Initialize(_WorldMapZoneDropDown, ZoneInitialize);
		_WorldMapZoneDropDown.Button:SetScript("OnClick", function(self)
			UIDD:ToggleDropDownMenu(1, nil, _WorldMapZoneDropDown);
		end);
		--
		_WorldMapZoomOutButton = CreateFrame('BUTTON', nil, WorldMapFrame, "UIPanelButtonTemplate");
		_WorldMapZoomOutButton:SetSize(110, 22);
		_WorldMapZoomOutButton:SetText(WorldMapZoomOutButton:GetText());
		_WorldMapZoomOutButton:SetPoint("CENTER", WorldMapZoomOutButton);
		_WorldMapZoomOutButton:SetScript("OnClick", function()
			WorldMapFrame:NavigateToParentMap();
		end);
		--
		_WorldMapZoneMinimapDropDown = CreateDropDown(WorldMapFrame, "_WorldMapZoneMinimapDropDown", BATTLEFIELD_MINIMAP);
		_WorldMapZoneMinimapDropDown:SetPoint("CENTER", WorldMapZoneMinimapDropDown);
		UIDD:UIDropDownMenu_SetWidth(_WorldMapZoneMinimapDropDown, 130, 32);
		UIDD:UIDropDownMenu_Initialize(_WorldMapZoneMinimapDropDown, ZoneMinimapInitialize);
		_WorldMapZoneMinimapDropDown.Button:SetScript("OnClick", function(self)
			UIDD:ToggleDropDownMenu(1, nil, _WorldMapZoneMinimapDropDown);
		end);
		--
		--
		WorldMapFrame:AddDataProvider(mapCallback);
	end
-->

local E = CreateFrame('FRAME');
E:RegisterEvent("PLAYER_ENTERING_WORLD");
E:SetScript("OnEvent", function()
	E:UnregisterEvent("PLAYER_ENTERING_WORLD");
	reimp();
end);
