local registry = LibStub('AceConfigRegistry-3.0')
local optionsTitle = " |cff00ccffWhat's Next?|r"
WhatsNext.optionsClass = select(2,UnitClass("player"))

local function GetFontList()
	return AceGUIWidgetLSMlists.font
end

function DeepCopy(object)
    local lookup_table = {}
    local function _copy(object)
        if type(object) ~= "table" then
            return object
        elseif lookup_table[object] then
            return lookup_table[object]
        end
        local new_table = {}
        lookup_table[object] = new_table
        for index, value in pairs(object) do
            new_table[_copy(index)] = _copy(value)
        end
        return new_table
    end
    return _copy(object)
end

local function GetDefaults()	
	return {
		global = {
			minimap = {
				hide = false,
			},
			placeHolders = {
			},
		},
		profile = {	
			enabled = true,
			locked = false,
			timeBetweenUpdates = .4,
			hideOutOfCombat = false,
			pos = {
				p1 = "TOPLEFT",
				p2 = "CENTER",
				x = 0,
				y = 0,
			},
			configPos = {
				p1 = "CENTER",
				p2 = "CENTER",
				x = 0,
				y = 0,
			},
			font = "Bazooka",
			outline = 'OUTLINE',
			scale = 1,
			showAbilityName = true,
			showIcon = true,
			textColor = {r=1,b=0,g=0.8196079,a=1},
			backgroundColor = {r=.6,b=0,g=0,a=.5},
			borderFullCDColor = {r=1,b=1,g=1,a=1},
			borderNoCDColor = {r=0,b=0,g=0,a=1},
		}
	}
end

local function SetValue(info,value) 
	WhatsNext.db.profile[info.arg] = value
	if info.arg == "showIcon" and not value then
		WhatsNext.db.profile["showAbilityName"] = true 
	elseif info.arg == "showAbilityName" and not value then
		WhatsNext.db.profile["showIcon"] = true 
	end
	WhatsNext:ApplyDisplaySettings()
end

local function GetValue(info) 
	return WhatsNext.db.profile[info.arg] 
end

local function GetColorValue(info)
	local color = WhatsNext.db.profile[info.arg]
	return color.r, color.g, color.b, color.a
end

local function SetColorValue(info, r, g, b, a)
	WhatsNext.db.profile[info.arg] = {["r"] = r, ["g"] = g, ["b"] = b, ["a"] = a}
	WhatsNext:ApplyDisplaySettings()
end
	
local optionsTop = {name = "",type = 'group',desc = "",args = {}}

local optionsGeneral = {
	name = "What's Next? by Strucker",
	type = 'group',
	desc = "",
	set = 	SetValue,
	get = 	GetValue,
	handler = WhatsNext,
	args = {
		["ENABLE"] = {
			name = "Enabled",
			type = 'toggle',
			width  = 'full',
			order = 50,
			arg = "enabled",
		},
		["LOCK"] = {
			name = "Locked",
			type = 'toggle',
			width  = 'full',
			order = 150,
			arg = "locked",
		},
		["HIDEOUTOFCOMBAT"] = {
			name = "Hide Out Of Combat",
			type = 'toggle',
			width  = 'full',
			order = 200,
			arg = "hideOutOfCombat",
		},
		["TIME_BETWEEN_UPDATES"] = {
			name = "Seconds Between Updates",
			type = 'range',
			min = .01, max = 1, step = .01,
			order = 250,
			arg = "timeBetweenUpdates"
		},
	}
}

local function OpenCastLogicDisplay(info)
	InterfaceOptionsFrame:Hide()
	WhatsNext_CastLogicDisplay:SetCastLogicSet(WhatsNext.optionsClass,WhatsNext.optionsSpec)

end

local function GetClassList()
	local classList = {}
	classList["DEATHKNIGHT"] = WhatsNext:Localize("DEATHKNIGHT")
	classList["DRUID"] = WhatsNext:Localize("DRUID")
	classList["HUNTER"] = WhatsNext:Localize("HUNTER")
	classList["MAGE"] = WhatsNext:Localize("MAGE")
	classList["MONK"] = WhatsNext:Localize("MONK")
	classList["PALADIN"] = WhatsNext:Localize("PALADIN")
	classList["PRIEST"] = WhatsNext:Localize("PRIEST")
	classList["ROGUE"] = WhatsNext:Localize("ROGUE")
	classList["SHAMAN"] = WhatsNext:Localize("SHAMAN")
	classList["WARLOCK"] = WhatsNext:Localize("WARLOCK")
	classList["WARRIOR"] = WhatsNext:Localize("WARRIOR")
	return classList
end

local function GetSpecList()
	local specList = {}
	specList[1] = WhatsNext:Localize(WhatsNext.optionsClass..1)
	specList[2] = WhatsNext:Localize(WhatsNext.optionsClass..2)
	specList[3] = WhatsNext:Localize(WhatsNext.optionsClass..3)
	if WhatsNext.optionsClass == "DRUID" then
		specList[4] = WhatsNext:Localize(WhatsNext.optionsClass..4)
	end
	return specList
end

local function SetClass(info,value)
	WhatsNext.optionsClass = value
	WhatsNext.optionsSpec = WhatsNext.optionsClass == "DRUID" and WhatsNext.optionsSpec or math.min(WhatsNext.optionsSpec,3)
end

local function GetAppearanceOptions()
	return {
		name = "What's Next? by Strucker",
		type = 'group',
		desc = "",
		set = 	SetValue,
		get = 	GetValue,
		handler = WhatsNext,
		args = {
			["BREAK3"] = {
				name = "\n ",
				type = 'description',
				order = 10,
			},
			["SCALE"] = {
				name = "Scale",
				type = 'range',
				min = .05, max = 5, step = .05,
				order = 50,
				arg = "scale"
			},
			["BREAK4"] = {
				name = "",
				type = 'description',
				order = 100,
			},
			["SHOW_ICON"] = {
				name = "Show Ability Icon",
				type = 'toggle',
				order = 150,
				arg = "showIcon",
			},
			["BREAK5"] = {
				name = "",
				type = 'description',
				order = 200,
			},
			["SHOW_ABILITY_NAME"] = {
				name = "Show Ability Name",
				type = 'toggle',
				order = 250,
				arg = "showAbilityName",
			},
			["BREAK6"] = {
				name = "",
				type = 'description',
				order = 300,
			},
			["FONT"] = {
				name = "Font",
				type = 'select',
				dialogControl = 'LSM30_Font',
				values = GetFontList,
				order = 310,
				arg = 'font',
			},
			["BREAK7"] = {
				name = "",
				type = 'description',
				order = 320,
			},
			["Outline"] = {
				name = "Outline",
				type = 'select',
				desc = "Set font outline.",
				values = {[''] = NONE, OUTLINE = VOICE_CHAT_NORMAL, THICKOUTLINE = "Thick"},
				order = 325,
				arg = 'outline',
			},
			["BREAK8"] = {
				name = "",
				type = 'description',
				order = 340,
			},
			["FColor"] = {
				name = "Text Color",
				type = 'color',
				hasAlpha = true,
				set = SetColorValue,
				get = GetColorValue,
				order = 350,
				arg = 'textColor',
			},
			["BREAK9"] = {
				name = "",
				type = 'description',
				order = 400,
			},
			["BGCOLOR"] = {
				name = "Background Color",
				type = 'color',
				hasAlpha = true,
				set = SetColorValue,
				get = GetColorValue,
				order = 450,
				arg = "backgroundColor",
			},
			["BREAK10"] = {
				name = "",
				type = 'description',
				order = 500,
			},
			["BORDERCDCOLOR"] = {
				name = "Border Full CD Color",
				type = 'color',
				hasAlpha = true,
				set = SetColorValue,
				get = GetColorValue,
				order = 550,
				arg = "borderFullCDColor",
			},
			["BREAK11"] = {
				name = "",
				type = 'description',
				order = 600,
			},
			["BORDERNOCDCOLOR"] = {
				name = "Border Off CD Color",
				type = 'color',
				hasAlpha = true,
				set = SetColorValue,
				get = GetColorValue,
				order = 650,
				arg = "borderNoCDColor",
			},
		}
	}
end

function WhatsNext:RegisterOptions()
	self:RegisterMedia()
	local dialog = LibStub('AceConfigDialog-3.0')
	
	self.db = LibStub('AceDB-3.0'):New('WhatsNextDB', GetDefaults()	, 'Default')
	self.db.RegisterCallback(self, "OnProfileChanged", "ApplyMainSettings")
	self.db.RegisterCallback(self, "OnProfileCopied", "ApplyMainSettings")
	self.db.RegisterCallback(self, "OnProfileReset", "ApplyMainSettings")
	
	
	local profileOptions = LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db)
	registry:RegisterOptionsTable("WhatsNext", {name = "",type = 'group',desc = "",args = {}})
	local dummy = dialog:AddToBlizOptions("WhatsNext", optionsTitle)
	dummy:SetScript('OnShow', function(self)
		InterfaceOptionsFrame_OpenToCategory(WhatsNext.mainOptions)
	end)
	
	registry:RegisterOptionsTable("WhatsNext-General", optionsGeneral)
	WhatsNext.mainOptions = dialog:AddToBlizOptions("WhatsNext-General", "General", optionsTitle)
	
	registry:RegisterOptionsTable("WhatsNext-Appearance", GetAppearanceOptions())
	WhatsNext.appearanceOptions = dialog:AddToBlizOptions("WhatsNext-Appearance", "Appearance", optionsTitle)
	
	registry:RegisterOptionsTable("WhatsNext-CastLogic", {name = "",type = 'group',desc = "",args = {}})
	dummy = dialog:AddToBlizOptions("WhatsNext-CastLogic", "Logic Sets", optionsTitle)
	dummy:SetScript('OnShow', function(self)
		InterfaceOptionsFrame:Hide()
		WhatsNext_LogicSetManagementDisplay:Show()
	end)
	
	registry:RegisterOptionsTable("WhatsNext-Profile", profileOptions)
	dialog:AddToBlizOptions("WhatsNext-Profile", "Profile", optionsTitle)
	
	local dataobj = LibStub:GetLibrary('LibDataBroker-1.1'):NewDataObject("What's Next?", {
		type = 'launcher',
		text = "What's Next?",
		label = "What's Next?",
		icon = 'Interface\\Icons\\INV_Misc_QuestionMark',
	})
	
	if dataobj then
		dataobj.OnClick = function(self,button,down) 
			if button == "LeftButton" then
				WhatsNext:ShowOptions()
			else
				WhatsNext.db.profile.locked = not WhatsNext.db.profile.locked
				WhatsNext:ApplyDisplaySettings()
			end
		end
		dataobj.OnTooltipShow = function(tooltip)
			tooltip:AddLine("|cff00ccffWhat's Next?|r by Strucker")
			tooltip:AddLine("     |cffffffffLeft Click|r -> Toggle Options Display")
			tooltip:AddLine("     |cffffffffRight Click|r -> Toggle Lock")
		end
		
		LibStub("LibDBIcon-1.0", true):Register("What's Next?", dataobj, self.db.global.minimap)
	end
end

function WhatsNext:ShowOptions()
	if InterfaceOptionsFrame:IsVisible() then
		InterfaceOptionsFrame:Hide()
	else
		InterfaceOptionsFrame_OpenToCategory(WhatsNext.mainOptions) 
	end
end


function WhatsNext:RegisterMedia()
	local media = LibStub("LibSharedMedia-3.0");
	media:Register("font", "ABF",					[[Interface\Addons\WhatsNext\fonts\ABF.ttf]])
	media:Register("font", "Accidental Presidency",	[[Interface\Addons\WhatsNext\fonts\Accidental Presidency.ttf]])
	media:Register("font", "Adventure",				[[Interface\Addons\WhatsNext\fonts\Adventure.ttf]])
	media:Register("font", "AnonymousPro (Bold)",	[[Interface\Addons\WhatsNext\fonts\AnonymousPro-Bold.ttf]])
	media:Register("font", "Avqest",				[[Interface\Addons\WhatsNext\fonts\Avqest.ttf]])
	media:Register("font", "Bazooka",				[[Interface\Addons\WhatsNext\fonts\Bazooka.ttf]])
	media:Register("font", "BigNoodleTitling",		[[Interface\Addons\WhatsNext\fonts\BigNoodleTitling.ttf]])
	media:Register("font", "BigNoodleTitling-Oblique",[[Interface\Addons\WhatsNext\fonts\BigNoodleTitling-Oblique.ttf]])
	media:Register("font", "BlackChancery",			[[Interface\Addons\WhatsNext\fonts\BlackChancery.ttf]])
	media:Register("font", "CRYSRG__",				[[Interface\Addons\WhatsNext\fonts\CRYSRG__.ttf]])
	media:Register("font", "DejaVuSansMono",		[[Interface\Addons\WhatsNext\fonts\DejaVuSansMono.ttf]])
	media:Register("font", "DorisPP",				[[Interface\Addons\WhatsNext\fonts\DORISPP.ttf]])
	media:Register("font", "Emblem",				[[Interface\Addons\WhatsNext\fonts\Emblem.ttf]])
	media:Register("font", "Enigma__2",				[[Interface\Addons\WhatsNext\fonts\Enigma__2.ttf]])
	media:Register("font", "FSEX300",				[[Interface\Addons\WhatsNext\fonts\FSEX300.ttf]])
	media:Register("font", "JAi_____",				[[Interface\Addons\WhatsNext\fonts\JAi_____.ttf]])
	media:Register("font", "LiberationSans Regular",[[Interface\Addons\WhatsNext\fonts\LiberationSans-Regular.ttf]])
	media:Register("font", "MONOFONT",				[[Interface\Addons\WhatsNext\fonts\MONOFONT.ttf]])
	media:Register("font", "Movie Poster (Bold)",	[[Interface\Addons\WhatsNext\fonts\Movie_Poster-Bold.ttf]])
	media:Register("font", "Porky",					[[Interface\Addons\WhatsNext\fonts\Porky.ttf]])
	media:Register("font", "rm_midse",				[[Interface\Addons\WhatsNext\fonts\rm_midse.ttf]])
	media:Register("font", "Tangerin",				[[Interface\Addons\WhatsNext\fonts\Tangerin.ttf]])
	media:Register("font", "Tw_Cen_MT_Bold",		[[Interface\Addons\WhatsNext\fonts\Tw_Cen_MT_Bold.ttf]])
	media:Register("font", "Ultima Campagnoli",		[[Interface\Addons\WhatsNext\fonts\Ultima_Campagnoli.ttf]])
	media:Register("font", "VeraSe",				[[Interface\Addons\WhatsNext\fonts\VeraSe.ttf]])
	media:Register("font", "WHITRABT",				[[Interface\Addons\WhatsNext\fonts\WHITRABT.ttf]])
	media:Register("font", "Yellow Jacket",			[[Interface\Addons\WhatsNext\fonts\Yellowjacket.ttf]])
end