local addonName, addon = ...

--[[-----------------------------------------------------------------------------
Skin frame and textures for each portion
-------------------------------------------------------------------------------]]
local skin = CreateFrame('Frame', nil, UIParent)
skin:SetFrameStrata('BACKGROUND')
skin:SetFrameLevel(1)

skin:SetPoint('BOTTOMLEFT')
skin:SetPoint('BOTTOMRIGHT')

skin:SetHeight(238)
addon.skin = skin

skin[1] = skin:CreateTexture(nil, 'BACKGROUND')--Left
skin[1]:SetPoint('TOPLEFT')
skin[1]:SetPoint('BOTTOMLEFT', -1, 0)
skin[1]:SetWidth(468)
_G["GrimUIcoreArtB1"] = skin[1]

skin[2] = skin:CreateTexture(nil, 'BACKGROUND')-- Mid-left
skin[2]:SetPoint('TOPLEFT', skin[1], 'TOPRIGHT')
skin[2]:SetPoint('BOTTOMLEFT', skin[1], 'BOTTOMRIGHT')
skin[2]:SetPoint('RIGHT', UIParent, 'CENTER')
_G["GrimUIcoreArtB2"] = skin[2]

skin[3] = skin:CreateTexture(nil, 'OVERLAY')-- Minimap
skin[3]:SetWidth(168)
skin[3]:SetHeight(134)
skin[3]:SetParent(Minimap)
skin[3]:SetPoint('CENTER', Minimap)
_G["GrimUIcoreArtMM"] = skin[3]

skin[4] = skin:CreateTexture(nil, 'BACKGROUND')-- Target
skin[4]:SetWidth(248)
skin[4]:SetHeight(251)
_G["GrimUItargetArt"] = skin[4]

skin[-1] = skin:CreateTexture(nil, 'BACKGROUND')-- Right
skin[-1]:SetPoint('TOPRIGHT')
skin[-1]:SetPoint('BOTTOMRIGHT')
skin[-1]:SetWidth(472)
skin[-1]:SetTexCoord(1, 0, 0, 1)
_G["GrimUIcoreArtB4"] = skin[-1]

skin[-2] = skin:CreateTexture(nil, 'BACKGROUND')-- Mid-right
skin[-2]:SetPoint('TOPRIGHT', skin[-1], 'TOPLEFT')
skin[-2]:SetPoint('BOTTOMRIGHT', skin[-1], 'BOTTOMLEFT')
skin[-2]:SetPoint('LEFT', UIParent, 'CENTER')
skin[-2]:SetTexCoord(1, 0, 0, 1)
_G["GrimUIcoreArtB3"] = skin[-2]

function addon:ArtSetPoint()
	skin:SetPoint('BOTTOMLEFT', -addon.settings.artSetPoint, 0)
	skin:SetPoint('BOTTOMRIGHT', addon.settings.artSetPoint, 0)
	
	-- The following is done so TargetFrame.lua does not need to load first.
	skin[4]:SetParent("GrimUITargetFrame")
	skin[4]:SetPoint('BOTTOM', GrimUITargetFrame, 'BOTTOM', 0, 4)
end

function addon:ArtSetAlpha()
	for i = 1, #skin do
		skin[i]:SetAlpha(addon.settings.artSetAlpha)
	end
		skin[-1]:SetAlpha(addon.settings.artSetAlpha)
		skin[-2]:SetAlpha(addon.settings.artSetAlpha)
end

function addon:ArtSetPointReset()
	skin:SetPoint('BOTTOMLEFT', 0, 0)
	skin:SetPoint('BOTTOMRIGHT', 0, 0)
	addon.settings.artSetPoint = 0
end

--[[-----------------------------------------------------------------------------
UpdateSkin - Call this when the uiSkin setting changes
-------------------------------------------------------------------------------]]
function addon:ConfigureSkin()
	local path = addon.settings.uiSkin .. "-"
	for i = 1, #skin do
		skin[i]:SetTexture(path .. i)
		if skin[-i] then
			skin[-i]:SetTexture(path .. i)
		end
	end
end

--[[-----------------------------------------------------------------------------
Combat Indicator
-------------------------------------------------------------------------------]]
local aggroL = CreateFrame("Frame", "GrimUIArtAggroL", nil) 
aggroL:SetAlpha(0.6) 
aggroL:SetFrameStrata("LOW") 
aggroL:SetFrameLevel(2) 
aggroL:SetWidth(340) 
aggroL:SetHeight(250) 
aggroL:SetPoint("BOTTOMLEFT", GrimUIcoreArtB1, "BOTTOMLEFT", 0, 0) 
aggroL:SetBackdrop{ 
		bgFile = "Interface\\AddOns\\GrimUI\\Skins\\Aggro", 
		edgeFile = nil, tile = false, tileSize = 0, edgeSize = 0, 
		insets = { left = 0, right = 0, top = 0, bottom = 0 } 
	} 
aggroL:Hide() 

local aggroR = CreateFrame("Frame", "GrimUIArtAggroR", nil) 
aggroR.texture = aggroR:CreateTexture()
aggroR.texture:SetAllPoints(aggroR)
aggroR.texture:SetTexture("Interface\\AddOns\\GrimUI\\Skins\\Aggro")
aggroR:SetAlpha(0.6) 
aggroR:SetFrameStrata("LOW") 
aggroR:SetFrameLevel(2) 
aggroR:SetWidth(340) 
aggroR:SetHeight(250) 
aggroR:SetPoint("BOTTOMRIGHT", GrimUIcoreArtB4, "BOTTOMRIGHT", 0, 0)
aggroR.texture:SetTexCoord(1, 0, 0, 1)
aggroR:Hide() 

local function toggleAggroArt(self, event) 
	if addon.settings.ShowAggroArt == false then
		aggroL:Hide() 
		aggroR:Hide()
	else
		if event == "PLAYER_REGEN_ENABLED" then 
			aggroL:Hide() 
			aggroR:Hide() 
		else 
			aggroL:Show() 
			aggroR:Show() 
			end 
		end
	end

aggroL:RegisterEvent("PLAYER_REGEN_ENABLED") 
aggroL:RegisterEvent("PLAYER_REGEN_DISABLED")
aggroL:SetScript("OnEvent", toggleAggroArt) 

--[[-----------------------------------------------------------------------------
WorldFrame / Viewport
-------------------------------------------------------------------------------]]
function addon:SetupViewPort()
		if addon.settings.useViewPort then
			WorldFrame:ClearAllPoints()
			WorldFrame:SetPoint('TOP', 0 ,-addon.settings.VPTopPos)
			WorldFrame:SetPoint('RIGHT', -addon.settings.VPRightPos, 0)
			WorldFrame:SetPoint('LEFT', addon.settings.VPLeftPos, 0)
			WorldFrame:SetPoint('BOTTOM', 0, addon.settings.VPBottomPos)
		else
			WorldFrame:ClearAllPoints()
			WorldFrame:SetPoint('TOP')
			WorldFrame:SetPoint('RIGHT')
			WorldFrame:SetPoint('LEFT')
			WorldFrame:SetPoint('BOTTOM')
		end
end

--[[-----------------------------------------------------------------------------
Initialize
-------------------------------------------------------------------------------]]
addon.RegisterEvent("InitializeSkins", 'PLAYER_ENTERING_WORLD', function(self, event)
	addon.UnregisterEvent(self, event)
	addon:RegisterDefaultSetting("uiSkin", [[Interface\AddOns\]] .. addonName .. [[\Skins\]] .. UnitFactionGroup('player'))
	addon:ConfigureSkin()
	addon:ArtSetPoint()
	addon:SetupViewPort()
	addon:ArtSetAlpha()
end)