local E = unpack(ElvUI)
local S = E:GetModule('Skins')
local _G = _G
local hooksecurefunc = _G.hooksecurefunc
local IsAddOnLoaded = _G.C_AddOns and _G.C_AddOns.IsAddOnLoaded

--main function to hook/set gradient/textures/shadows
local function hookCell(frame,region,solo,isPet)
	--gradient/custom texture
	if region then
		if solo then
			if not region.EltruismHook then
				if frame and frame.SetVertexColor then
					hooksecurefunc(frame,"SetVertexColor", function()
						--gradient
						if E.db.ElvUI_EltreumUI.unitframes.gradientmode.enable then
							if E.db.ElvUI_EltreumUI.unitframes.gradientmode.customcolor then
								frame:SetGradient("HORIZONTAL", ElvUI_EltreumUI:GradientColorsCustom(E.myclass, false, false))
							else
								frame:SetGradient("HORIZONTAL", ElvUI_EltreumUI:GradientColors(E.myclass, false, false))
							end
						end
						--custom texture
						if E.db.ElvUI_EltreumUI.unitframes.ufcustomtexture.enable then
							if not E.db.ElvUI_EltreumUI.unitframes.ufcustomtexture.noclasstexture then
								frame:SetTexture(ElvUI_EltreumUI:UnitframeClassTextureCustom(E.myclass))
							else
								frame:SetTexture(ElvUI_EltreumUI:UnitframeClassTexture(E.myclass))
							end
						end
					end)
				end
				--shadows
				if E.db.ElvUI_EltreumUI.skins.shadow.enable then
					if E.db.ElvUI_EltreumUI.skins.shadow.raid then
						if not region.shadow then
							region:CreateShadow()
							ElvUI_EltreumUI:ShadowColor(region.shadow)
						end
					end
				end
				region.EltruismHook = true
			end
		else
			if region.unitid and not region.EltruismHook then
				if frame and frame.SetVertexColor then
					hooksecurefunc(frame,"SetVertexColor", function()
						local _, englishClass = _G.UnitClass(region.unitid)
						--gradient
						if E.db.ElvUI_EltreumUI.unitframes.gradientmode.enable then
							if E.db.ElvUI_EltreumUI.unitframes.gradientmode.customcolor then
								frame:SetGradient("HORIZONTAL", ElvUI_EltreumUI:GradientColorsCustom(englishClass, false, false))
							else
								frame:SetGradient("HORIZONTAL", ElvUI_EltreumUI:GradientColors(englishClass, false, false))
							end
						end
						--custom texture
						if E.db.ElvUI_EltreumUI.unitframes.ufcustomtexture.enable then
							if not E.db.ElvUI_EltreumUI.unitframes.ufcustomtexture.noclasstexture then
								frame:SetTexture(ElvUI_EltreumUI:UnitframeClassTextureCustom(englishClass))
							else
								frame:SetTexture(ElvUI_EltreumUI:UnitframeClassTexture(englishClass))
							end
						end
					end)
				end
				--shadows
				if E.db.ElvUI_EltreumUI.skins.shadow.enable then
					if E.db.ElvUI_EltreumUI.skins.shadow.raid then
						if not region.shadow then
							region:CreateShadow()
							ElvUI_EltreumUI:ShadowColor(region.shadow)
						end
					end
				end
				region.EltruismHook = true
			end
		end
	else
		if not isPet then
			if frame and frame.SetStatusBarColor and not frame.EltruismHook then
				hooksecurefunc(frame,"SetStatusBarColor", function()
					if frame:GetParent().__unitGuid then
						local _, englishClass = _G.GetPlayerInfoByGUID(frame:GetParent().__unitGuid)
						--gradient
						if E.db.ElvUI_EltreumUI.unitframes.gradientmode.enable then
							if E.db.ElvUI_EltreumUI.unitframes.gradientmode.customcolor then
								frame:GetStatusBarTexture():SetGradient("HORIZONTAL", ElvUI_EltreumUI:GradientColorsCustom(englishClass, false, false))
							else
								frame:GetStatusBarTexture():SetGradient("HORIZONTAL", ElvUI_EltreumUI:GradientColors(englishClass, false, false))
							end
						end
						--custom texture
						if E.db.ElvUI_EltreumUI.unitframes.ufcustomtexture.enable then
							if not E.db.ElvUI_EltreumUI.unitframes.ufcustomtexture.noclasstexture then
								frame:GetStatusBarTexture():SetTexture(ElvUI_EltreumUI:UnitframeClassTextureCustom(englishClass))
							else
								frame:GetStatusBarTexture():SetTexture(ElvUI_EltreumUI:UnitframeClassTexture(englishClass))
							end
						end
					end
				end)

				--shadows
				if E.db.ElvUI_EltreumUI.skins.shadow.enable then
					if E.db.ElvUI_EltreumUI.skins.shadow.raid then
						if not frame:GetParent().shadow then
							frame:GetParent():CreateShadow()
							ElvUI_EltreumUI:ShadowColor(frame:GetParent().shadow)
						end
					end
				end
				frame.EltruismHook = true
			end
		else
			if frame and frame.SetStatusBarColor and not frame.EltruismHook then
				hooksecurefunc(frame,"SetStatusBarColor", function()
					--gradient
					if E.db.ElvUI_EltreumUI.unitframes.gradientmode.enable then
						if E.db.ElvUI_EltreumUI.unitframes.gradientmode.customcolor then
							frame:GetStatusBarTexture():SetGradient("HORIZONTAL", ElvUI_EltreumUI:GradientColorsCustom("NPCFRIENDLY", false, false))
						else
							frame:GetStatusBarTexture():SetGradient("HORIZONTAL", ElvUI_EltreumUI:GradientColors("NPCFRIENDLY", false, false))
						end
					end
					--custom texture
					if E.db.ElvUI_EltreumUI.unitframes.ufcustomtexture.enable then
						if not E.db.ElvUI_EltreumUI.unitframes.ufcustomtexture.noclasstexture then
							frame:GetStatusBarTexture():SetTexture(ElvUI_EltreumUI:UnitframeClassTextureCustom("NPCFRIENDLY"))
						else
							frame:GetStatusBarTexture():SetTexture(ElvUI_EltreumUI:UnitframeClassTexture("NPCFRIENDLY"))
						end
					end
				end)

				--shadows
				if E.db.ElvUI_EltreumUI.skins.shadow.enable then
					if E.db.ElvUI_EltreumUI.skins.shadow.raid then
						if not frame:GetParent().shadow then
							frame:GetParent():CreateShadow()
							ElvUI_EltreumUI:ShadowColor(frame:GetParent().shadow)
						end
					end
				end
				frame.EltruismHook = true
			end
		end
	end
end

--cell gradient/textures
function ElvUI_EltreumUI:EltruismCell()
	if not IsAddOnLoaded("Cell") then return end
	if E.db.ElvUI_EltreumUI.skins.cell then
		--solo
		if _G["CellSoloFramePlayer"] then
			if E.db.ElvUI_EltreumUI.skins.cellhealthloss then
				for _,k in pairs({_G["CellSoloFramePlayer"]:GetRegions()}) do
					if k:GetName() and k:GetName() == ("CellSoloFramePlayerHealthBarLoss") then
						hookCell(k,_G["CellSoloFramePlayer"],true)
					end
				end
			else
				hookCell(_G["CellSoloFramePlayerHealthBar"])
			end
		end
		if _G["CellSoloFramePet"] then
			hookCell(_G["CellSoloFramePetHealthBar"],false,true,true)
		end

		--raid
		if _G["CellRaidFrame"] and _G.IsInGroup() then
			for i = 1, 8 do
				if _G["CellRaidFrameHeader"..i] then
					for v = 1, 5 do
						if _G["CellRaidFrameHeader"..i.."UnitButton"..v] and _G["CellRaidFrameHeader"..i.."UnitButton"..v.."HealthBar"] then
							if E.db.ElvUI_EltreumUI.skins.cellhealthloss then
								for _,k in pairs({_G["CellRaidFrameHeader"..i.."UnitButton"..v]:GetRegions()}) do
									if k:GetName() and k:GetName():match("HealthBarLoss") then
										hookCell(k,_G["CellRaidFrameHeader"..i.."UnitButton"..v])
									end
								end
							else
								hookCell(_G["CellRaidFrameHeader"..i.."UnitButton"..v.."HealthBar"])
							end
						end
						if _G["CellRaidFrameHeader"..i.."UnitButton"..v.."Pet"] and _G["CellRaidFrameHeader"..i.."UnitButton"..v.."PetHealthBar"] then
							if E.db.ElvUI_EltreumUI.skins.cellhealthloss then
								for _,k in pairs({_G["CellRaidFrameHeader"..i.."UnitButton"..v.."Pet"]:GetRegions()}) do
									if k:GetName() and k:GetName():match("HealthBarLoss") then
										hookCell(k,_G["CellRaidFrameHeader"..i.."UnitButton"..v.."Pet"],false,true)
									end
								end
							else
								hookCell(_G["CellRaidFrameHeader"..i.."UnitButton"..v.."PetHealthBar"],false,true)
							end
						end
					end
				end
			end
		end

		--party
		if _G["CellPartyFrame"] and _G.IsInGroup() then
			if _G["CellPartyFrameHeader"] then
				for i = 1, 5 do
					if _G["CellPartyFrameHeaderUnitButton"..i] and _G["CellPartyFrameHeaderUnitButton"..i.."HealthBar"] then
						for _,v in pairs({_G["CellPartyFrameHeaderUnitButton"..i.."HealthBar"]:GetRegions()}) do
							if E.db.ElvUI_EltreumUI.skins.cellhealthloss then
								if v:GetName() and v:GetName():match("HealthBarLoss") then
									hookCell(v,_G["CellPartyFrameHeaderUnitButton"..i.."HealthBar"])
								end
							else
								hookCell(_G["CellPartyFrameHeaderUnitButton"..i.."HealthBar"])
							end
						end
					end
					if _G["CellPartyFrameHeaderUnitButton"..i.."Pet"] and _G["CellPartyFrameHeaderUnitButton"..i.."PetHealthBar"] then
						for _,v in pairs({_G["CellPartyFrameHeaderUnitButton"..i.."PetHealthBar"]:GetRegions()}) do
							if E.db.ElvUI_EltreumUI.skins.cellhealthloss then
								if v:GetName() and v:GetName():match("HealthBarLoss") then
									hookCell(v,_G["CellPartyFrameHeaderUnitButton"..i.."PetHealthBar"],false,true)
								end
							else
								hookCell(_G["CellPartyFrameHeaderUnitButton"..i.."PetHealthBar"],false,true)
							end
						end
					end
				end
			end
		end
	end
end
S:AddCallbackForAddon('Cell', "EltruismCell", ElvUI_EltreumUI.EltruismCell)
