local spartan = LibStub("AceAddon-3.0"):GetAddon("SpartanUI");
local addon = spartan:GetModule("RaidFrames");
----------------------------------------------------------------------------------------------------
SpartanoUF:SetActiveStyle("Spartan_RaidFrames");

if DBMod.RaidFrames.mode == "group" then
	raid = SpartanoUF:SpawnHeader("SUI_RaidFrameHeader", nil, 'raid',
		'showPlayer', true,
		'showRaid', true,
		'showParty', false,
		'showSolo', false,
		'xoffset', 3,
		'yOffset', -5,
		'point', 'TOP',
		'groupFilter', '1,2,3,4,5,6,7,8',
		'groupBy', 'GROUP',
		'groupingOrder', '1,2,3,4,5,6,7,8',
		'sortMethod', 'name',
		'maxColumns', DBMod.RaidFrames.maxColumns,
		'unitsPerColumn', DBMod.RaidFrames.unitsPerColumn,
		'columnSpacing', DBMod.RaidFrames.columnSpacing,
		'columnAnchorPoint', 'LEFT'
	)
else
	raid = SpartanoUF:SpawnHeader("SUI_RaidFrameHeader", nil, 'raid',
		'showPlayer', true,
		'showRaid', true,
		'showParty', false,
		'showSolo', false,
		'xoffset', 3,
		'yOffset', 0,
		'point', 'LEFT',
		'groupFilter', '1,2,3,4,5,6,7,8',
		'groupBy', 'ROLE',
		'groupingOrder', '1,2,3,4,5,6,7,8',
		'sortMethod', 'name',
		'maxColumns', DBMod.RaidFrames.maxColumns,
		'unitsPerColumn', DBMod.RaidFrames.unitsPerColumn,
		'columnSpacing', DBMod.RaidFrames.columnSpacing,
		'columnAnchorPoint', 'TOP'
	)
end
raid:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 20, -40)
	
do -- raid header configuration
	raid:SetParent("SpartanUI");
	raid:SetClampedToScreen(false);
	if DBMod.PartyFrames.HideBlizzFrames then
		CompactRaidFrameContainer:UnregisterAllEvents()
		CompactRaidFrameContainer:Hide()

		local function hideRaid()
			CompactRaidFrameContainer:UnregisterAllEvents()
			if( InCombatLockdown() ) then return end
			local shown = CompactRaidFrameManager_GetSetting("IsShown")
			if( shown and shown ~= "0" ) then
				CompactRaidFrameManager_SetSetting("IsShown", "0")
			end
		end

		hooksecurefunc("CompactRaidFrameManager_UpdateShown", function()
			hideRaid()
		end)

		hideRaid();
		CompactRaidFrameContainer:HookScript("OnShow", hideRaid)
	end
end

do -- scripts to make it movable
	raid.mover = CreateFrame("Frame");
	raid.mover:SetSize(20, 20);
	raid.mover:SetPoint("TOPLEFT",raid,"TOPLEFT");
	raid.mover:SetPoint("BOTTOMRIGHT",raid,"BOTTOMRIGHT");
	raid.mover:EnableMouse(true);
	
	raid.bg = raid.mover:CreateTexture(nil,"BACKGROUND");
	raid.bg:SetAllPoints(raid.mover);
	raid.bg:SetTexture(1,1,1,0.5);
	
	raid.mover:SetScript("OnEvent",function()
		addon.locked = 1;
		raid.mover:Hide();
	end);
	raid.mover:RegisterEvent("VARIABLES_LOADED");
	raid.mover:RegisterEvent("PLAYER_REGEN_DISABLED");
	
	function addon:UpdateRaidPosition()
		addon.offset = DB.yoffset
		if DBMod.RaidFrames.moved then
			raid:SetMovable(true);
			raid:SetUserPlaced(false);
		else
			raid:SetMovable(false);
		end
		if not DBMod.RaidFrames.moved then
			raid:ClearAllPoints();
			if spartan:GetModule("PartyFrames",true) then
				raid:SetPoint("TOPLEFT",UIParent,"TOPLEFT",10,-140-(addon.offset));
			else
				raid:SetPoint("TOPLEFT",UIParent,"TOPLEFT",10,-20-(addon.offset));
			end
		else
			local Anchors = {}
			for k,v in pairs(DBMod.RaidFrames.Anchors) do
				Anchors[k] = v;
			end
			raid:ClearAllPoints();
			raid:SetPoint(Anchors.point, nil, Anchors.relativePoint, Anchors.xOfs, Anchors.yOfs);
		end
	end
end

do	-- Hide Frame
	function addon:UpdateRaid(event,...)
		raid:SetAttribute('showRaid',DBMod.RaidFrames.showRaid);
		if DBMod.RaidFrames.showRaid and IsInRaid() then
			raid:Show();
		else
			raid:Hide();
		end
		addon:UpdateRaidPosition()
		raid:SetScale(DBMod.RaidFrames.scale);
	end
	
	
	local raidWatch = CreateFrame("Frame");
	raidWatch:RegisterEvent('GROUP_ROSTER_UPDATE');
	raidWatch:RegisterEvent('PLAYER_ENTERING_WORLD');
	
	raidWatch:SetScript('OnEvent',function(self,event,...)
		if(InCombatLockdown()) then
			self:RegisterEvent('PLAYER_REGEN_ENABLED');
		else
			self:UnregisterEvent('PLAYER_REGEN_ENABLED');
			addon:UpdateRaid(event);
		end
	end);
end