local L = MaggzMarkerLocals
MAGGZMARKER_TITLE = "MaggzMarker";
MAGGZMARKER_VER = "1.0.7";
MAGGZMARKER_VER_TITLE = MAGGZMARKER_TITLE .. " " .. MAGGZMARKER_VER;

-- Databases and backdrops
MaggzMarkerDB = MaggzMarkerDB or {
    crf= false,
	locked= false,
	clamped = false,
	shown = true,
	flipped = false,
	vertical = false,
	partyShow = false,
	targetShow = false,
	assistShow = false,
	bgHide = false,
	tooltips = true,
	detach = false,
	scale = 1,
	alpha = 1,
	iconSpace = 0,
	x= 0,
	y = 0,
	relPt = "CENTER",
}
MaggzWMDB = MaggzWMDB or {
	locked = false,
	clamped = false,
	shown = true,
	flipped = false,
	vertical = false,
	icons = false,
	partyShow = false,
	assistShow = false,
	bgHide = false,
	tooltips = true,
	worldTex = 1,
	scale = 1,
	alpha = 1,
	x= 0,
	y = 50,
	relPt = "CENTER",
}
MaggzAIDB = MaggzAIDB or {
	locked = false,
	clamped = false,
	shown = true,
	bgHide = false,
	sound = false,
	x= -20,
	y = -50,
	relPt = "CENTER",
}
local defaultBackdrop = {
	bgFile = "Interface\\TabardFrame\\TabardFrameBackground",
	edgeFile = "Interface\\LFGFRAME\\LFGBorder",
	tile = true,
	tileSize = 16,
	edgeSize = 16,
	insets = {left = 4, right = 4, top = 4, bottom = 4,}
}
local borderlessBackdrop = {
	bgFile = "Interface\\TabardFrame\\TabardFrameBackground",
	tile = true,
	tileSize = 16
}
local optionsBackdrop = {
	bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
	edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Gold-Border",
	tile = true,
	tileSize = 16,
	edgeSize = 16,
	insets = {left = 4, right = 4, top = 4, bottom = 4}
}
local editBoxBackdrop = {
	bgFile = "Interface\\COMMON\\Common-Input-Border",
	tile = false,
}
local mM = "MaggzMarker"

-- Binding Frame Strings
BINDING_HEADER_MAGGZMARKER_RAID = string.format("MaggzMarker - %s",L["Raid marker"]);
BINDING_HEADER_MAGGZMARKER_WORLD = string.format("MaggzMarker - %s",L["World markers"]);
BINDING_NAME_MAGGZMARKER_SKULL = L["Skull"];
BINDING_NAME_MAGGZMARKER_CROSS = L["Cross"];
BINDING_NAME_MAGGZMARKER_SQUARE = L["Square"];
BINDING_NAME_MAGGZMARKER_MOON = L["Moon"];
BINDING_NAME_MAGGZMARKER_TRI = L["Triangle"];
BINDING_NAME_MAGGZMARKER_DIA = L["Diamond"];
BINDING_NAME_MAGGZMARKER_CIR = L["Circle"];
BINDING_NAME_MAGGZMARKER_STAR = L["Star"];
BINDING_NAME_MAGGZMARKER_CLEAR = L["Clear mark"];
BINDING_NAME_MAGGZMARKER_READY = L["Ready check"];
BINDING_NAME_MAGGZMARKER_ROLE = L["Role check"]; 
BINDING_NAME_MAGGZMARKER_CONVERT = L["Convert to Raid"]; 

_G["BINDING_NAME_CLICK MaggzMarkerSquareflare:LeftButton"] = L["Square"];
_G["BINDING_NAME_CLICK MaggzMarkerTriangleflare:LeftButton"] = L["Triangle"];
_G["BINDING_NAME_CLICK MaggzMarkerDiamondflare:LeftButton"] = L["Diamond"];
_G["BINDING_NAME_CLICK MaggzMarkerCrossflare:LeftButton"] = L["Cross"];
_G["BINDING_NAME_CLICK MaggzMarkerStarflare:LeftButton"] = L["Star"];
_G["BINDING_NAME_CLICK MaggzMarkerClearflares:LeftButton"] = L["Clear all world markers"];

-- MaggzMarker Main Frame setup like wmarker as i loved how that addon worked.
MaggzMarker = {}
function MaggzMarker:print(self, msg)
	DEFAULT_CHAT_FRAME:AddMessage(string.format("%s:%s",mM,msg))
end
MaggzMarker.other = {}
local main = CreateFrame("Frame", "MaggzMarkerMain", UIParent)
main:SetBackdrop(borderlessBackdrop)
main:SetBackdropColor(0,0,0,0)
main:EnableMouse(true)
main:SetMovable(true)
main:SetSize(285,35)
main:SetPoint("CENTER", UIParent, "CENTER")
main:SetClampedToScreen(false)
MaggzMarker.main = main
local function createMover(width,height,parent,pt,relPt)
	local f = CreateFrame("Frame",nil,parent.main)
	f:SetBackdrop(defaultBackdrop)
	f:SetBackdropColor(0.1,0.1,0.1,0.9)
	f:EnableMouse(true)
	f:SetMovable(true)
	f:SetSize(width,height)
	f:SetPoint(pt,parent.main,relPt)
	f:SetScript("OnMouseDown",function(self,button) if (button=="LeftButton") then parent.main:StartMoving() end end)
	f:SetScript("OnMouseUp",function() parent.main:StopMovingOrSizing(); parent:getLoc() end)
	return f
end
local mM_moverLeft = createMover(20,35,MaggzMarker,"RIGHT","LEFT")
MaggzMarker.other.moverLeft = mM_moverLeft
local mM_moverRight = createMover(20,35,MaggzMarker,"LEFT","RIGHT")
MaggzMarker.other.moverRight = mM_moverRight

-- MaggzMarker options frame
MaggzMarker.options = CreateFrame("Frame", "MaggzMarkerOptions", UIParent)
MaggzMarker.options.name = "MaggzMarker"
InterfaceOptions_AddCategory(MaggzMarker.options)

-- MaggzMarker Icon Frame (and icons)
MaggzMarker.iconFrame = CreateFrame("Frame", "MaggzMarker_iconFrame", MaggzMarker.main)
MaggzMarker.iconFrame:SetBackdrop(defaultBackdrop)
MaggzMarker.iconFrame:SetBackdropColor(0.1,0.1,0.1,0.9)
MaggzMarker.iconFrame:EnableMouse(true)
MaggzMarker.iconFrame:SetMovable(true)
MaggzMarker.iconFrame:SetSize(170,35)
MaggzMarker.iconFrame:SetPoint("LEFT", MaggzMarker.main, "LEFT")
table.insert(MaggzMarker.other, MaggzMarker.iconFrame)
MaggzMarker.icon = {}
MaggzMarker.icons = {}
local lastFrame, xOff
local function iconNew(name, num)
	if lastFrame then xOff = 0 else xOff = 5 end
	local f = CreateFrame("Button", string.format("MaggzMarker%sicon",name), MaggzMarker.iconFrame)
	table.insert(MaggzMarker.icon, f)
	f:SetSize(20,20)
	f:SetPoint("LEFT",lastFrame or MaggzMarker.iconFrame,xOff,0)
	f:SetNormalTexture(string.format("interface\\targetingframe\\ui-raidtargetingicon_%d",num))
	f:EnableMouse(true)
	f:RegisterForClicks("LeftButtonDown", "RightButtonDown")
	f:SetScript("OnClick", function(self, button) if (button=="LeftButton") then SetRaidTarget("target", num) else InterfaceOptionsFrame_OpenToCategory(MaggzMarker.options) end end)
	f:SetScript("OnEnter", function(self) if (MaggzMarkerDB.tooltips==true) then GameTooltip:SetOwner(self, "ANCHOR_CURSOR"); GameTooltip:ClearLines(); GameTooltip:AddLine(L[name]); GameTooltip:Show() end end)
	f:SetScript("OnLeave", function(self) GameTooltip:Hide() end)
	lastFrame = f
	MaggzMarker.icon[name] = f
end
iconNew("Skull",8)
iconNew("Cross",7)
iconNew("Square",6)
iconNew("Moon",5)
iconNew("Triangle",4)
iconNew("Diamond",3)
iconNew("Circle",2)
iconNew("Star",1)

-- MaggzMarker Control Frame
local controlFrame = CreateFrame("Frame", "MaggzMarker_controlFrame", MaggzMarker.main)
controlFrame:SetBackdrop(defaultBackdrop)
controlFrame:SetBackdropColor(0.1,0.1,0.1,0.9)
controlFrame:EnableMouse(true)
controlFrame:SetMovable(true)
controlFrame:SetSize(85,35)
controlFrame:SetPoint("LEFT", MaggzMarker.iconFrame, "RIGHT")
MaggzMarker.other.controlFrame = controlFrame 
local clearIcon = CreateFrame("Button", "MaggzMarkerClearIcon", controlFrame)
clearIcon:SetSize(20,20)
clearIcon:SetPoint("LEFT", controlFrame, "LEFT",10,0)
clearIcon:SetNormalTexture("interface\\glues\\loadingscreens\\dynamicelements")
clearIcon:GetNormalTexture():SetTexCoord(0,0.5,0,0.5)
clearIcon:EnableMouse(true)
clearIcon:SetScript("OnClick", function(self) SetRaidTarget("target", 0) end)
clearIcon:SetScript("OnEnter", function(self) if (MaggzMarkerDB.tooltips==true) then GameTooltip:SetOwner(self, "ANCHOR_CURSOR"); GameTooltip:ClearLines(); GameTooltip:AddLine(L["Clear mark"]); GameTooltip:Show() end end)
clearIcon:SetScript("OnLeave", function(self) GameTooltip:Hide() end)
MaggzMarker.other.clearIcon = clearIcon
local readyCheck = CreateFrame("Button", "MaggzMarkerReadyCheck", controlFrame)
readyCheck:SetSize(20,20)
readyCheck:SetPoint("LEFT", clearIcon, "RIGHT")
readyCheck:SetNormalTexture("interface\\raidframe\\readycheck-waiting")
readyCheck:GetNormalTexture():SetTexCoord(0,1,0,1)
readyCheck:EnableMouse(true)
readyCheck:SetScript("OnClick", function(self) if (UnitIsGroupLeader("player") or UnitIsGroupAssistant("player")) then DoReadyCheck() else print("You are not able to perform a readycheck without assist or lead."); end end)
readyCheck:SetScript("OnEnter", function(self) if (MaggzMarkerDB.tooltips==true) then GameTooltip:SetOwner(self, "ANCHOR_CURSOR"); GameTooltip:ClearLines(); GameTooltip:AddLine(L["Ready check"]); GameTooltip:Show() end end)
readyCheck:SetScript("OnLeave", function(self) GameTooltip:Hide() end)
MaggzMarker.other.readyCheck = readyCheck
local roleCheck = CreateFrame("Button", "MaggzMarkerRoleCheck", controlFrame)
roleCheck:SetSize(20,20)
roleCheck:SetPoint("LEFT", readyCheck, "RIGHT",0,0)
roleCheck:SetNormalTexture("interface\\raidframe\\readycheck-ready")
roleCheck:GetNormalTexture():SetTexCoord(0,1,0,1)
roleCheck:EnableMouse(true)
roleCheck:SetScript("OnClick", function(self) if (UnitIsGroupLeader("player") or UnitIsGroupAssistant("player")) then InitiateRolePoll() else print("You are not able to perform Rolecheck without assist or lead."); end end)
roleCheck:SetScript("OnEnter", function(self) if (MaggzMarkerDB.tooltips==true) then GameTooltip:SetOwner(self, "ANCHOR_CURSOR"); GameTooltip:ClearLines(); GameTooltip:AddLine(L["Role check"]); GameTooltip:Show() end end)
roleCheck:SetScript("OnLeave", function(self) GameTooltip:Hide() end)
MaggzMarker.other.roleCheck = roleCheck
local convertRaid = CreateFrame("Button", "MaggzMarkerConvertRaid", controlFrame)
convertRaid:SetSize(20,20)
convertRaid:SetPoint("LEFT", roleCheck, "RIGHT",0,0)
convertRaid:SetNormalTexture("interface\\DialogFrame\\UI-Dialog-Icon-AlertNew")
convertRaid:GetNormalTexture():SetTexCoord(0,1,0,1)
convertRaid:EnableMouse(true)
convertRaid:SetScript("OnClick", function(self) 
    if (UnitInParty("player")) and not (UnitInRaid("player")) then ConvertToRaid() print("Converted to Raid");
	elseif (UnitInRaid("player")) then ConvertToParty() print("Converted from Raid to Party");
    end 
end)
convertRaid:SetScript("OnEnter", function(self) 
    if (MaggzMarkerDB.tooltips==true) and (GetNumGroupMembers() > 0) and (UnitInParty("player")) and not (UnitInRaid("player")) then GameTooltip:SetOwner(self, "ANCHOR_CURSOR"); GameTooltip:ClearLines();GameTooltip:AddLine(L["Convert to Raid"]); GameTooltip:Show() end 
	if (MaggzMarkerDB.tooltips==true) and (GetNumGroupMembers() > 0) and (UnitInRaid("player")) then GameTooltip:SetOwner(self, "ANCHOR_CURSOR"); GameTooltip:ClearLines();GameTooltip:AddLine(L["Convert to Party"]); GameTooltip:Show() end
	if (MaggzMarkerDB.tooltips==true) and not (GetNumGroupMembers() > 0) then GameTooltip:SetOwner(self, "ANCHOR_CURSOR"); GameTooltip:ClearLines();GameTooltip:AddLine(L["Convert to Raid"]); GameTooltip:Show() end
end)
convertRaid:SetScript("OnLeave", function(self) GameTooltip:Hide() end)
MaggzMarker.other.convertRaid = convertRaid

-- MaggzWM Main Frame 
MaggzWM = {}
wMmain = CreateFrame("Frame", "MaggzWMMain", UIParent)
wMmain:SetBackdrop(defaultBackdrop)
wMmain:SetBackdropColor(0.1,0.1,0.1,0.7)
wMmain:EnableMouse(true)
wMmain:SetMovable(true)
wMmain:SetUserPlaced(true)
wMmain:SetSize(135,30)
wMmain:SetPoint("CENTER", UIParent, "CENTER",0,40)
wMmain:SetClampedToScreen(false)
MaggzWM.main = wMmain
local mM_moverLeft = createMover(20,30,MaggzWM,"RIGHT","LEFT")
MaggzWM.main.moverLeft = mM_moverLeft
local mM_moverRight = createMover(20,30,MaggzWM,"LEFT","RIGHT")
MaggzWM.main.moverRight = mM_moverRight

--Flares (World Markers)
MaggzWM.flare = {}
local function flareNew(name, tex, num, xOff)
	local f = CreateFrame("Button", string.format("MaggzMarker%sflare",name), MaggzWM.main, "SecureActionButtonTemplate")
	table.insert(MaggzWM.flare,f)
	f:SetSize(20,20)
	f:SetNormalTexture("interface\\targetingframe\\ui-raidtargeting6icons") -- "interface\\minimap\\partyraidblips"
	f:GetNormalTexture():SetTexCoord(tex[1],tex[2],tex[3],tex[4])
	f:SetPoint("LEFT",lastFlare or MaggzWM.main,"RIGHT",xOff or 0,0)
	f:SetAttribute("type", "macro")
	f:SetAttribute("macrotext1", string.format("/wm %d",num))
	f:SetScript("OnEnter", function(self) if (MaggzWMDB.tooltips==true) then GameTooltip:SetOwner(self, "ANCHOR_CURSOR"); GameTooltip:ClearLines(); GameTooltip:AddLine(string.format("%s %s",L[name],L["world marker"])); GameTooltip:Show() end end)
	f:SetScript("OnLeave", function(self) GameTooltip:Hide() end)
	lastFlare = f
	MaggzWM.flare[name] = f
end
flareNew("Square",{0.25,0.5,0.25,0.5},1,5)
flareNew("Triangle",{0.75,1,0,0.25},2)
flareNew("Diamond",{0.5,0.75,0,0.25},3)
flareNew("Cross",{0.5,0.75,0.25,0.5},4)
flareNew("Star",{0,0.25,0,0.25},5)
local flareClear = CreateFrame("Button", "MaggzMarkerClearflares", MaggzWM.main, "SecureActionButtonTemplate") -- Clear
flareClear:SetSize(15,15)
flareClear:SetNormalTexture("interface\\glues\\loadingscreens\\dynamicelements")
flareClear:GetNormalTexture():SetTexCoord(0,0.5,0,0.5)
flareClear:SetPoint("LEFT", MaggzWM.flare["Star"], "RIGHT",3,0)
flareClear:SetAttribute("type", "macro")
flareClear:SetAttribute("macrotext1", "/cwm all")
flareClear:SetScript("OnEnter", function(self) if (MaggzWMDB.tooltips==true) then GameTooltip:SetOwner(self, "ANCHOR_CURSOR"); GameTooltip:ClearLines(); GameTooltip:AddLine(L["Clear all world markers"]); GameTooltip:Show() end end)
flareClear:SetScript("OnLeave", function(self) GameTooltip:Hide() end)
MaggzWM.flareClear = flareClear

--Maggz Aggro Indicator Main
MaggzAI = {}
aImain = CreateFrame("Frame", "MaggzAIMain", UIParent)
aImain:SetBackdrop(defaultBackdrop)
aImain:SetBackdropColor(0.1,0.1,0.1,0.3)
aImain:EnableMouse(false)
aImain:SetMovable(true)
aImain:SetUserPlaced(true)
aImain:SetSize(150,60)
aImain:SetPoint("CENTER", UIParent, "CENTER",0,90)
aImain:SetClampedToScreen(false)
aImaintxt = aImain:CreateFontString("Aggro Text")
aImaintxt:SetFont([=[Fonts\FRIZQT__.TTF]=], 44, "OUTLINE")
aImaintxt:SetTextColor(1, 0, 0)
aImaintxt:SetText("AGGRO")
aImaintxt:SetPoint("CENTER", aImain)
MaggzAI.main = aImain
local mM_moverLeft = createMover(20,30,MaggzAI,"RIGHT","LEFT")
MaggzAI.main.moverLeft = mM_moverLeft
local mM_moverRight = createMover(20,30,MaggzAI,"LEFT","RIGHT")
MaggzAI.main.moverRight = mM_moverRight