--[[
	colorSelector.lua
		A bagnon color selector
--]]

local AddonName, Addon = ...
local L = Addon.L
local ColorSelector = Addon.Classy:New('Frame'); Addon.ColorSelector = ColorSelector

local backdrop = {
  bgFile = [[Interface\ChatFrame\ChatFrameBackground]],
  edgeFile = [[Interface\Tooltips\UI-Tooltip-Border]],
  edgeSize = 16,
  tile = true, tileSize = 16,
  insets = {left = 4, right = 4, top = 4, bottom = 4}
}

local ColorSliders = { 'Red', 'Green', 'Blue' }


function ColorSelector:New(colorState, parent)
	local f = self:Bind(CreateFrame('Frame', parent:GetName() .. '_' .. colorState, parent))	

	f:SetBackdrop(backdrop)
	f:SetBackdropBorderColor(0.4, 0.4, 0.4)
	f:SetBackdropColor(0, 0, 0, 0.3)

	local t = f:CreateFontString(nil, 'BACKGROUND', 'GameFontHighlightLarge')
	t:SetPoint('BOTTOMLEFT', f, 'TOPLEFT', 4, 2)
	t:SetText(L[colorState])
	f.text = t

	local preview = f:CreateTexture(nil, 'ARTWORK')
	preview:SetPoint('RIGHT', -16, 0)
	preview:SetSize(96, 96)
	f.preview = preview

	-- color sliders
	local sliders = {}
	for colorIndex, colorName in ipairs(ColorSliders) do
		local slider = Addon.Slider:New(colorName, f, 0, 100, 1)

		slider.SetSavedValue = function(self, value)
			tullaRange.sets[colorState][colorIndex] = math.floor(value + 0.5) / 100
			tullaRange:ForceColorUpdate() 

			preview:SetVertexColor(tullaRange:GetColor(colorState))
		end

		slider.GetSavedValue = function(self)			
			return tullaRange.sets[colorState][colorIndex] * 100
		end

		if colorIndex > 1 then
			slider:SetPoint('TOPLEFT', sliders[colorIndex - 1], 'BOTTOMLEFT', 0, -24)	
		else
			slider:SetPoint('BOTTOMLEFT', t, 'BOTTOMLEFT', 8, -40)
		end

		table.insert(sliders, slider)
	end



	f.sliders = sliders

	return f
end

function ColorSelector:UpdateValues()
	do
		local texture = nil

		while not texture do
			texture = GetActionTexture(math.random(1, 120))
		end

		self.preview:SetTexture(texture)
	end

	for i, slider in pairs(self.sliders) do
		slider:UpdateValue()
	end
end