LeoTrainer.craftNames = {"Blacksmith", "Clothing", "Woodworking", "Jewelry"}

LeoTrainer_Settings = ZO_Object:Subclass()
local LAM = LibStub("LibAddonMenu-2.0")

function LeoTrainer_Settings:New(...)
    local object = ZO_Object.New(self)
    object:Initialize(...)
    return object
end

function LeoTrainer_Settings:Initialize()
end

function LeoTrainer_Settings:CreatePanel()
	local OptionsName = "LeoTrainerOptions"
	local panelData = {
		type = "panel",
		name = LeoTrainer.name,
		displayName = "|cff8800"..LeoTrainer.displayName.."|r",
		author = "@LeandroSilva",
		version = LeoTrainer.version,
		registerForRefresh = false,
		registerForDefaults = false,
		--slashCommand = "/leotrainer",
		website = "http://www.esoui.com/downloads/"
	}
	LAM:RegisterAddonPanel(OptionsName, panelData)

	local optionsData = {
		{
			type = "header",
			name = "|c3f7fffCharacters|r"
		},{
			type = "description",
			text = "desc"
		},{
			type = "custom",
			reference = OptionsName.."Characters"
		}
	}
	LAM:RegisterOptionControls(OptionsName, optionsData)
end

function LeoTrainer_Settings:AddCharacters()
	if LeoTrainerOptionsCharactersSection then return end
	local control = CreateControlFromVirtual("$(parent)", LeoTrainerOptionsCharacters, "LeoTrainer_SettingsCharacters", "Section")
	self.container = control:GetNamedChild("Container")
	local last
	for i, char in ipairs(LeoAltholic.GetCharacters()) do
		last = self:AddCharacter(i, char, last)
	end
end

function LeoTrainer_Settings_OnMouseEnter(control, tooltip)
	InitializeTooltip(InformationTooltip, control, BOTTOMLEFT, 0, -2, TOPLEFT)
	SetTooltipText(InformationTooltip, tooltip)
end

local function Toggle(checkbox, checked)
	local control = checkbox:GetParent()
    LeoTrainer.setTrackingSkill(control.data.charName, checkbox.data.craftId, checked)
end

function LeoTrainer_Settings:AddCharacter(id, char, last)
	local control = CreateControlFromVirtual("$(parent)", self.container, "LeoTrainer_SettingsCharacter", id)
	if last then
		control:SetAnchor(TOPLEFT, last, BOTTOMLEFT, 0, 0)
		control:SetAnchor(BOTTOMRIGHT, last, BOTTOMRIGHT, 0, 30)
	else
		control:SetAnchor(TOPLEFT, nil, TOPLEFT, 0, 0)
		control:SetAnchor(BOTTOMRIGHT, nil, TOPRIGHT, 0, 30)
	end
	control.data = control.data or {}
	control.data.charName = char.bio.name
	control.label = control:GetNamedChild("Name")
	control.label:SetText(char.bio.name)
    control.label:SetColor(ZO_SELECTED_TEXT:UnpackRGBA())
	for k,craftId in pairs(LeoAltholic.craftResearch) do
		local checkbox = control:GetNamedChild("ST_" .. craftId)
		checkbox.data = checkbox.data or {}
        checkbox.data.craftId = craftId
		checkbox.data.tooltipText = nil
		ZO_CheckButton_SetCheckState(checkbox, LeoTrainer.isTrackingSkill(char.bio.name, craftId))
		ZO_CheckButton_SetToggleFunction(checkbox, Toggle)
	end
	return control
end

function LeoTrainer_Settings:ClearCharacter(id)
	if self.container then
		local control = self.container:GetNamedChild(id)
		if control then
			control:SetHidden(true)
		end
	end
end

function LeoTrainer_Settings:OnSettingsControlsCreated(panel)
	--Each time an options panel is created, once for each addon viewed
	if panel:GetName() == "LeoTrainerOptions" then
		self:AddCharacters()
	end
end

function LeoTrainer_Settings:IsCreated()
	if self.container then
		return true
	else
		return false
	end
end