local addonName, addon = ...

-- Art Skin Frame Creation
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('GUI_ArtLeft', 'BACKGROUND')--Left
skin[1]:SetPoint('TOPLEFT')
skin[1]:SetPoint('BOTTOMLEFT', -1, 0)
skin[1]:SetWidth(468)
_G["GUI_ArtFarLeft"] = skin[1]

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

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

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

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

skin[-2] = skin:CreateTexture("GUI_ArtMidRight", '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["GUI_ArtMidRight"] = 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 or at all. 
	if GrimUITargetFrame ~= nil then
		skin[4]:SetParent("GrimUITargetFrame")
		skin[4]:SetPoint('BOTTOM', GrimUITargetFrame, 'BOTTOM', 0, 4)
	end
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 GUI_ArtAggroLeft = CreateFrame("Frame", "GUI_ArtAggroLeft", nil) 
GUI_ArtAggroLeft:SetAlpha(0.6) 
GUI_ArtAggroLeft:SetFrameStrata("LOW") 
GUI_ArtAggroLeft:SetFrameLevel(2) 
GUI_ArtAggroLeft:SetSize(250, 340)
GUI_ArtAggroLeft:SetPoint("BOTTOMLEFT", GUI_ArtFarLeft, "BOTTOMLEFT", 0, 0) 
GUI_ArtAggroLeft:SetBackdrop{ 
		bgFile = "Interface\\AddOns\\GrimUI\\Skins\\Aggro", 
		edgeFile = nil, tile = false, tileSize = 0, edgeSize = 0, 
		insets = { left = 0, right = 0, top = 0, bottom = 0 } 
	} 
GUI_ArtAggroLeft:Hide() 

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

local function toggleAggroArt(self, event) 
	if addon.settings.ShowAggroArt == false then
		GUI_ArtAggroLeft:Hide() 
		GUI_ArtAggroRight:Hide()
	else
		if event == "PLAYER_REGEN_ENABLED" then 
			GUI_ArtAggroLeft:Hide() 
			GUI_ArtAggroRight:Hide() 
		else 
			GUI_ArtAggroLeft:Show() 
			GUI_ArtAggroRight:Show() 
			end 
		end
	end
GUI_ArtAggroLeft:RegisterEvent("PLAYER_REGEN_ENABLED") 
GUI_ArtAggroLeft:RegisterEvent("PLAYER_REGEN_DISABLED")
GUI_ArtAggroLeft: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("Initialize-Skins", 'PLAYER_LOGIN', function(self, event, name)
	addon.UnregisterEvent(self, event)
	local curRes = GetCVar("gxResolution")
	if addon.settings.resolution then 
		if addon.settings.resolution ~= curRes then
			addon:ArtSetPointReset()
			addon.settings.resolution = curRes
		end
	else
		addon.settings.resolution = curRes 
		addon:SetUIScale()
	end
	addon:ConfigureSkin()
	addon:ArtSetPoint()
	addon:SetupViewPort()
	addon:ArtSetAlpha()
end)