--Modify From NDui
--msylgj0
--thx to siweia :P

--config start--
local MicroMenuPos = {"TOP", UIParent, "TOP", -120, -6}
local Scale = 1
--config end--

local Media = "Interface\\Addons\\MicroMenu\\Media\\"
local bdTex = "Interface\\ChatFrame\\ChatFrameBackground"
local glowTex = Media.."glowTex"
local InfoColor = "|cff70C0F5"
local myClass = select(2, UnitClass("player"))
local color = (CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS)[myClass]
local faction = UnitFactionGroup("player")

local MB = CreateFrame("frame", nil, UIParent)
MB:SetSize(210, 20)
MB:SetScale(Scale)
MB:SetPoint(unpack(MicroMenuPos))
MB:RegisterEvent("PLAYER_ENTERING_WORLD")
MB:SetScript("OnEvent", function()
	MB:UnregisterEvent("PLAYER_ENTERING_WORLD")
	--in case of first call in combat
	ToggleAllBags()
	ToggleAllBags()
end)

local function CreateBD(f, a, s)
	f:SetBackdrop({
		bgFile = bdTex, edgeFile = glowTex, edgeSize = s or 4,
		insets = {left = s or 4, right = s or 4, top = s or 4, bottom = s or 4},
	})
	f:SetBackdropColor(0, 0, 0, a or 0.5)
	f:SetBackdropBorderColor(0, 0, 0)
end

local function CreateMM(f, t)
	f:SetSize(50, 50)
	f:SetFrameStrata("DIALOG")
	f:SetBackdrop({bgFile = Media..t})
	f:SetBackdropColor(color.r, color.g, color.b, 1)
end

local function CreateMB(f, parent, w, h, x, y)
	f:SetSize(w, h)
	f:SetPoint("CENTER", parent, "CENTER", x, y)
	CreateBD(f, 1)
	f:SetFrameLevel(parent:GetFrameLevel() - 1)
	f:SetAlpha(0)
end

local function key(text, action)
	if GetBindingKey(action) then
		return text.." ("..GetBindingText(GetBindingKey(action))..")", color.r, color.g, color.b
	else
		return text, color.r, color.g, color.b
	end
end

local MB1 = CreateFrame("frame", nil, MB)
MB1:SetPoint("CENTER", MB, "CENTER", 15, 0)
CreateMM(MB1, "micro_player")
local MB1BG = CreateFrame("button", nil, MB1)
CreateMB(MB1BG, MB1, 35, 20, -6, 0)
MB1BG:SetScript("OnEnter", function(self)
	self:SetAlpha(0.7)
	GameTooltip:SetOwner(self, 'ANCHOR_TOP')
	GameTooltip:AddLine(key(CHARACTER_BUTTON, "TOGGLECHARACTER0"))
	GameTooltip:Show()
end)
MB1BG:SetScript("OnLeave", function(self)
	GameTooltip:Hide()
	self:SetAlpha(0)
end)
MB1BG:SetScript("OnClick", function(self)
	ToggleFrame(CharacterFrame)
end)

local MB2 = CreateFrame("frame", nil, MB)
MB2:SetPoint("CENTER", MB, "CENTER", 41, 0)
CreateMM(MB2, "micro_spellbook")
local MB2BG = CreateFrame("button", nil, MB2)
CreateMB(MB2BG, MB2, 24, 20, 0, 0)
MB2BG:SetScript("OnEnter", function(self)
	self:SetAlpha(0.7)
	GameTooltip:SetOwner(self, 'ANCHOR_TOP')
	GameTooltip:AddLine(key(SPELLBOOK_ABILITIES_BUTTON, "TOGGLESPELLBOOK"))
	GameTooltip:Show()
end)
MB2BG:SetScript("OnLeave", function(self)
	GameTooltip:Hide()
	self:SetAlpha(0)
end)
ToggleFrame(SpellBookFrame)	-- prevent taint
MB2BG:SetScript("OnClick", function(self)
	ToggleFrame(SpellBookFrame)
end)


local MB4 = CreateFrame("frame", nil, MB)
MB4:SetPoint("CENTER", MB, "CENTER", 93, 0)
CreateMM(MB4, "micro_talents")
local MB4BG = CreateFrame("button", nil, MB4)
CreateMB(MB4BG, MB4, 24, 20, 0, 0)
MB4BG:SetScript("OnEnter", function(self)
	self:SetAlpha(0.7)
	GameTooltip:SetOwner(self, 'ANCHOR_TOP')
	GameTooltip:AddLine(key(TALENT_BUTTON, "TOGGLETALEN"))
	GameTooltip:Show()
end)
MB4BG:SetScript("OnLeave", function(self)
	GameTooltip:Hide()
	self:SetAlpha(0)
end)
MB4BG:SetScript("OnClick", function(self)
	ToggleTalentFrame()
end)

local MB5 = CreateFrame("frame", nil, MB)
MB5:SetPoint("CENTER", MB, "CENTER", 67, 0)
CreateMM(MB5, "micro_quests")
local MB5BG = CreateFrame("button", nil, MB5)
CreateMB(MB5BG, MB5, 24, 20, 0, 0)
MB5BG:SetScript("OnEnter", function(self)
	self:SetAlpha(0.7)
	GameTooltip:SetOwner(self, 'ANCHOR_TOP')
	GameTooltip:AddLine(key(QUESTLOG_BUTTON, "TOGGLEQUESTLOG"))
	GameTooltip:Show()
end)
MB5BG:SetScript("OnLeave", function(self)
	GameTooltip:Hide()
	self:SetAlpha(0)
end)
MB5BG:SetScript("OnClick", function(self)
	ToggleFrame(QuestLogFrame)
end)

local MB6 = CreateFrame("frame", nil, MB)
MB6:SetPoint("CENTER", MB, "CENTER", 145, 0)
CreateMM(MB6, "micro_guild")
local MB6BG = CreateFrame("button", nil, MB6)
CreateMB(MB6BG, MB6, 24, 20, 0, 0)
MB6BG:SetScript("OnEnter", function(self)
	self:SetAlpha(0.7)
	GameTooltip:SetOwner(self, 'ANCHOR_TOP')
	GameTooltip:AddLine(key(SOCIALS_BUTTON, "TOGGLESOCIALS"))
	GameTooltip:Show()
end)
MB6BG:SetScript("OnLeave", function(self)
	GameTooltip:Hide()
	self:SetAlpha(0)
end)
MB6BG:SetScript("OnClick", function(self)
	ToggleFriendsFrame()
end)

local MB7 = CreateFrame("frame", nil, MB)
MB7:SetPoint("CENTER", MB, "CENTER", 119, 0)
CreateMM(MB7, "micro_encounter")
local MB7BG = CreateFrame("button", nil, MB7)
CreateMB(MB7BG, MB7, 24, 20, 0, 0)
MB7BG:SetScript("OnEnter", function(self)
	self:SetAlpha(0.7)
	GameTooltip:SetOwner(self, 'ANCHOR_TOP')
	GameTooltip:AddLine(key(WORLDMAP_BUTTON, "TOGGLEWorldMap"))
	GameTooltip:Show()
end)
MB7BG:SetScript("OnLeave", function(self)
	GameTooltip:Hide()
	self:SetAlpha(0)
end)
MB7BG:SetScript("OnClick", function(self)
	ToggleFrame(WorldMapFrame)
end)


local MB12 = CreateFrame("frame", nil, MB)
MB12:SetPoint("CENTER", MB, "CENTER", 171, 0)
CreateMM(MB12, "micro_gm")
local MB12BG = CreateFrame("button", nil, MB12)
CreateMB(MB12BG, MB12, 24, 20, 0, 0)
MB12BG:SetScript("OnEnter", function(self)
	self:SetAlpha(0.7)
	GameTooltip:SetOwner(self, 'ANCHOR_TOP')
	GameTooltip:AddLine(HELP_BUTTON, color.r, color.g, color.b)
	GameTooltip:Show()
end)
MB12BG:SetScript("OnLeave", function(self)
	GameTooltip:Hide()
	self:SetAlpha(0)
end)
MB12BG:SetScript("OnClick", function(self)
	ToggleFrame(HelpFrame)
end)

local MB13 = CreateFrame("frame", nil, MB)
MB13:SetPoint("CENTER", MB, "CENTER", 197, 0)
CreateMM(MB13, "micro_settings")
local MB13BG = CreateFrame("button", nil, MB13)
CreateMB(MB13BG, MB13, 24, 20, 0, 0)
MB13BG:SetScript("OnEnter", function(self)
	self:SetAlpha(0.7)
	GameTooltip:SetOwner(self, 'ANCHOR_TOP')
	GameTooltip:AddLine(MAIN_MENU, color.r, color.g, color.b)
	GameTooltip:Show()
end)
MB13BG:SetScript("OnLeave", function(self)
	GameTooltip:Hide()
	self:SetAlpha(0)
end)
MB13BG:SetScript("OnClick", function(self)
	ToggleFrame(GameMenuFrame)
	PlaySound("igMiniMapOpen")
end)

local MB14 = CreateFrame("frame", nil, MB)
MB14:SetPoint("CENTER", MB, "CENTER", 234, 0)
CreateMM(MB14, "micro_bags")
local MB14BG = CreateFrame("button", nil, MB14)
CreateMB(MB14BG, MB14, 35, 20, -6, 0)
MB14BG:SetScript("OnEnter", function(self)
	self:SetAlpha(0.7)
	GameTooltip:SetOwner(self, 'ANCHOR_TOP')
	GameTooltip:AddLine(key(BAGSLOT, "OPENALLBAGS"))
	GameTooltip:Show()
end)
MB14BG:SetScript("OnLeave", function(self)
	GameTooltip:Hide()
	self:SetAlpha(0)
end)
MB14BG:SetScript("OnClick", function(self)
	ToggleAllBags()
end)