-- This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
-- To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
-- or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.

local MereHealingFrames, privateVars = ...

local colourChangeFuncs = {}
local colourChangeEvents = {}

MereHealingFrames.CurrentColours = {}

MereHealingFrames.DefaultColours = {
	RoleTank = {
		r = 8/255,
		g = 27/255,
		b = 69/255,
		a = 1,
		},
	RoleHealer = {
		r = 0/255,
		g = 100/255,
		b = 27/255,
		a = 1,
		},
	RoleDps = {
		r = 122/255,
		g = 2/255,
		b = 4/255,
		a = 1,
		},
	RoleSupport = {
		r = 104/255,
		g = 2/255,
		b = 140/255,
		a = 1,
		},
	RoleBorder = {
		r = 1,
		g = 1,
		b = 1,
		a = 1,
		},
	RoleUnknown = {
		r = 1,
		g = 1,
		b = 1,
		a = 1,
		},
	PanelBorderAggro = {
		r = 1,
		g = 0,
		b = 0,
		a = 1,
	},
	PanelBorderDefault = {
		r = 0,
		g = 0,
		b = 0,
		a = 1,
	},
	PanelBorderBlocked = {
		r = 1,
		g = 1,
		b = 0,
		a = 1,
	},
	BarHealth = {
		a = 1, r = 102/255, g = 172/255, b = 13/255
	},
	BarMana = {
		a = 1, r = 23/255,  g = 40/255, b = 247/255
	},
	BarRogue = {
		a = 1, r = 131/255, g = 22/255, b = 139/255
	},
	BarWarrior = {
		a = 1, r = 225/255, g = 206/255, b = 68/255
	},
    BarAbsorb = {
        a = 1, r = 28/255,  g = 203/255, b = 196/255
    },
	LayoutBackground = {
		r = 0,
		g = 0,
		b = 0,
		a = 0.5,
	},
	LayoutLabelBackground = {
		r = 0,
		g = 0,
		b = 0,
		a = 0.5,
	},
	LayoutBackgroundUnlocked = {
		r = 1,
		g = 0,
		b = 0,
		a = 0.5,
	},
	LayoutBackgroundConfiguring = {
		r = 0, g = 0, b = 1.0, a = 0.5
	},
	WarriorColour = {
		r = 255/255, g = 40/255, b = 40/255, a = 1
	},
	ClericColour = {
		r = 119/255, g = 239/255, b = 0, a = 1
	},
	MageColour = {
		r = 200/255, g = 94/255, b = 255/255, a=1
	},
	RogueColour = {
		r = 255/255, g = 219/255, b = 0, a = 1
	},
    UnknownColour =
    {
        r = 1, g = 1, b = 1, a = 1
    },
    HealthBarText = {
        r = 1, g = 1, b = 1, a = 1
    },
    HealthBarTextShadow = {
        r = 0, g = 0, b = 0, a = 1
    },
    ManaBarText = {
        r = 1, g = 1, b = 1, a = 1
    },
    ManaBarTextShadow = {
        r = 0, g = 0, b = 0, a = 1
    },
    EnergyBarText = {
        r = 1, g = 1, b = 1, a = 1
    },
    EnergyBarTextShadow = {
        r = 0, g = 0, b = 0, a = 1
    },
    PowerBarText = {
        r = 1, g = 1, b = 1, a = 1
    },
    PowerBarTextShadow = {
        r = 0, g = 0, b = 0, a = 1
    },

}

MereHealingFrames.Colours = {}
function MereHealingFrames.Colours.FetchRGBA(name)
	local colour
	if (MereHealingFrames.CurrentColours[name]) then
		colour = MereHealingFrames.CurrentColours[name]
	elseif (MereHealingFrames.DefaultColours[name]) then
		colour = MereHealingFrames.DefaultColours[name]
	else
		error("Unable to find colour: " .. name)
	end
	return colour.r, colour.g, colour.b, colour.a
end

function MereHealingFrames.Colours.FetchRGB(name)
	local colour
	if (MereHealingFrames.CurrentColours[name]) then
		colour = MereHealingFrames.CurrentColours[name]
	elseif (MereHealingFrames.DefaultColours[name]) then
		colour = MereHealingFrames.DefaultColours[name]
	else
		error("Unable to find colour: " .. name)
	end
	return colour.r, colour.g, colour.b
end

-- note this on purposefully uses the colour, rather than copy it
function MereHealingFrames.Colours.FetchColour(name)
	local colour
	if (MereHealingFrames.CurrentColours[name]) then
		colour = MereHealingFrames.CurrentColours[name]
	elseif (MereHealingFrames.DefaultColours[name]) then
		colour = MereHealingFrames.DefaultColours[name]
	else
		error("Unable to find colour: " .. name)
	end
	return colour
end

function MereHealingFrames.Colours.Clear(name)
	if MereHealingFrames.CurrentColours[name] then
		MereHealingFrames.CurrentColours[name] = nil

        local defaultColour = MereHealingFrames.DefaultColours[name]
        colourChangeFuncs[name](defaultColour.r, defaultColour.g, defaultColour.b, defaultColour.a or 1.0)
	end
end

function MereHealingFrames.Colours.Update(name, r, g, b, a)
	if not MereHealingFrames.DefaultColours[name] then
		return false
	end

	MereHealingFrames.CurrentColours[name] = { r = r, g = g, b = b, a = a or 1.0 }
    colourChangeFuncs[name](r, g, b, a or 1.0)

	return true
end

function MereHealingFrames.Colours.ResetAll()
    for colourName, colourDetail in pairs(MereHealingFrames.CurrentColours) do
        local defaultColour = MereHealingFrames.DefaultColours[colourName]
        colourChangeFuncs[colourName](defaultColour.r, defaultColour.g, defaultColour.b, defaultColour.a or 1.0)
    end

	MereHealingFrames.CurrentColours = {}
end

function MereHealingFrames.Colours.RegisterEventHooks()
    for colourName, colourDetails in pairs(MereHealingFrames.DefaultColours) do
        local EventPath =  'Colours.' .. colourName
        local caller, handle = Utility.Event.Create("MereHealingFrames", EventPath)
        colourChangeFuncs[colourName] = caller
        colourChangeEvents[colourName] = handle
    end
end