if not SimpleCombatLog then return end

local L = {
	["SCL: ChatFrame%d Settings"] = "SCL: ChatFrame%d Settings",
	["Color skills by element"] = "Color skills by element",
	["Greater resize"] = "Greater resize",
	["Suppress combat log"] = "Suppress combat log",
	["Clear all settings"] = "Clear all settings",
}

local SCL = SimpleCombatLog

SCL.menuFunc = {}

local dewdrop

function SimpleCombatLog:SetValue(id, field, value)

	if not self.settings[id] then
		self.settings[id] = {}
	else
		if self.settings[id].theme then
			self.settings[id] = SimpleCombatLog.CopyTable({}, self.settings[id])
			self.settings[id].theme = nil
			self.settingDB[id] = self.settings[id]
		end
	end
		
	self.settings[id][field] = value
	
end


function SCL:ClearSettings(id, noRefresh)
	if not self.settings[id] then return end
	
	local colors = self.settings[id].colors
	local formats = self.settings[id].formats
	
	self.settings[id] = nil
	
	if colors or formats then
		self.settings[id] = {
			colors = colors,
			formats = formats,
		}
	end

	self.settingDB[id] = self.settings[id]
	
	if not noRefresh then
		self:RefreshSettings()
	end
	
end


function SCL:ShowSettingMenu(id)

	local tabFrame = getglobal("ChatFrame" .. id .. "Tab")	
	if not tabFrame then return end

	if not self.initGUI then
		self.typeList = {
			"hit", 	"hitCrit", 	"hitDOT", 	"heal", 	"healCrit", 	"healDOT", 	"miss", 	"gain", 	"drain", 	"leech", "environment", "",
			"buff", 	"debuff", 	"fade", 	"dispel", 	"dispelFailed", 	"extraattack", 	"cast", 	"castBegin", 	"castTargeted", "interrupt", "",
			"create", 	"death", 	"deathSource", "deathSkill", "honor", 	"honorKill", "dishonor", "experience", 	"reputation",  "reputationRank", "reputationMinus", "enchant", 	"feedpet", 	"fail",
		}
	
		self.colorList = {
		"player", 	"pet", 	"raid", 	"party", 	"target", 	"targettarget", 	"other", "",
		"physical", 	"holy", 	"fire", 	"nature", 	"frost", 	"shadow", 	"arcane", "",
		"heal", 	"miss", 	"buff", 	"debuff", 	"skill", 		
		}
		
		self.optionsTable = {
			type = 'group',
			args = {
				suppress = {
					order = 550,
					type = 'toggle',
					name = L["Suppress combat log"],
					desc = L["Suppress combat log"],
					get = function() return SCL:GetValue(id, 'suppress') end,
					set = function(v) SCL:SetValue(id, 'suppress', v) end,
				},
				colorspell = {
					order = 600,
					type = 'toggle',
					name = L["Color skills by element"],
					desc = L["Color skills by element"],
					get = function() return SCL:GetValue(id, 'colorspell') end,
					set = function(v) SCL:SetValue(id, 'colorspell', v) end		
				},
				resize = {
					order = 700,
					type = 'toggle',
					name = L["Greater resize"],
					desc = L["Greater resize"],
					get = function() return SCL:GetValue(id, 'resize') end,
					set = function(v) SCL:SetValue(id, 'resize', v) SCL:RefreshSettings() end		
				},
				clear = {
					order = 800,
					type = 'execute',
					name = L["Clear all settings"],
					desc = L["Clear all settings"],
					func = function() SCL:ClearSettings(id) end		
				},
			}
		}
		
		
		if not dewdrop then
			dewdrop = AceLibrary("Dewdrop-2.0")
		end

		self.initGUI = true
		
	end

	if not dewdrop:IsRegistered(tabFrame) then
		dewdrop:Register(tabFrame, 
			'children', function(level, value) self:MenuSettings(id, level, value) end, 
			'dontHook', true
		)
	end


	-- self:UpdateColorOptions(id)
	dewdrop:Open(tabFrame)
end
	

function SimpleCombatLog:MenuSettings(id, level, value)

	if level == 1 then
		dewdrop:AddLine(
			'text', string.format( L["SCL: ChatFrame%d Settings"], id),
			'isTitle', true,
			'notCheckable', true
		)
	end
	
	for i, func in pairs(self.menuFunc) do
		func(self, id, level, value)
	end

	if level == 1 then
		dewdrop:AddLine(
			'text', L["Suppress combat log"],
			'func', function() 
				self:SetValue(id, 'suppress', not self:GetValue(id, 'suppress') ) 
				self:RefreshSettings()
			end,
			'checked', self:GetValue(id, 'suppress')
		)
		dewdrop:AddLine(
			'text', L["Color skills by element"],
			'func', function() 
				self:SetValue(id, 'colorspell', not self:GetValue(id, 'colorspell') ) 
				self:RefreshSettings()
			end,
			'checked', self:GetValue(id, 'colorspell')
		)
		dewdrop:AddLine(
			'text', "Color messages by event",
			'tooltipTitle', "Color messages by event",
			'tooltipDesc', "The messages being shown will have a base event-based colors, which you can set in the Blizzard drop down chat options.",
			'func', function()
				self:SetValue(id, 'colorEvent', not self:GetValue(id, 'colorEvent') )
				self:RefreshSettings()
			end,
			'checked', self:GetValue(id, 'colorEvent')
		)
		dewdrop:AddLine(
			'text', L["Greater resize"],
			'func', function() 
				self:SetValue(id, 'resize', not self:GetValue(id, 'resize') ) 
				self:RefreshSettings()
			end,
			'checked', self:GetValue(id, 'resize')
		)
		dewdrop:AddLine()
		
		dewdrop:AddLine(
			'text', L["Clear all settings"],
			'notCheckable', true,
			'func', function() self:ClearSettings(id) end
		)
				

	end

	if self.ThemeMenuSettings then
		self:ThemeMenuSettings(id, level, value)
	end

end


