local addonName, ItemProfConstants = ...

local frame = CreateFrame( "Frame" )
frame.name = addonName

local profsCheck
local questCheck
local iconSizeSlider
local iconSizeLabel
local iconDemoTexture

local PROF_CHECK = { nil, nil, nil, nil, nil, nil, nil, nil, nil, nil }
local QUEST_CHECK = { nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil }

local userVariables



local function SaveAndQuit() 
	
	local profFlags = 0
	for i=1, #PROF_CHECK do
		if PROF_CHECK[ i ]:GetChecked() then
			local mask = bit.lshift( 1, i-1 )
			profFlags = profFlags + mask
		end
	end

	local questFlags = 1		-- Always track quest flag 'Q'
	for i=1, #QUEST_CHECK do
		if QUEST_CHECK[ i ]:GetChecked() then
			local mask = bit.lshift( 1, i )	-- Offset the Q flag
			questFlags = questFlags + mask
		end
	end

	userVariables.showProfs = profsCheck:GetChecked()
	userVariables.showQuests = questCheck:GetChecked()
	userVariables.profFlags = profFlags
	userVariables.questFlags = questFlags
	userVariables.iconSize = iconSizeSlider:GetValue()
	ItemProfConstants:ConfigChanged( userVariables )
end


local function ToggleProfCheckbox() 
	-- Enable/disable profession tracking
	local isChecked = profsCheck:GetChecked()
	for i=1, #PROF_CHECK do
		PROF_CHECK[ i ]:SetEnabled( isChecked )
	end
end

local function ToggleQuestCheckbox() 
	-- Enable/disable quest tracking
	local isChecked = questCheck:GetChecked()
	for i=1, #QUEST_CHECK do
		QUEST_CHECK[ i ]:SetEnabled( isChecked )
	end
end


local function RefreshWidgets()
	-- Sync the widgets with the config variables
	profsCheck:SetChecked( userVariables.showProfs )
	questCheck:SetChecked( userVariables.showQuests )
	local profFlags = userVariables.profFlags
	local questFlags = userVariables.questFlags
	iconSizeSlider:SetValue( userVariables.iconSize )
	
	-- Update the profession checkboxes
	for i=1, #PROF_CHECK do
		local mask = bit.lshift( 1, i-1 )
		local isSet = bit.band( profFlags, mask )
		PROF_CHECK[ i ]:SetChecked( isSet ~= 0 )
	end
	-- Update the quest checkboxes
	for i=1, #QUEST_CHECK do
		local mask = bit.lshift( 1, i )
		local isSet = bit.band( questFlags, mask )
		QUEST_CHECK[ i ]:SetChecked( isSet ~= 0 )
	end

	-- Set checkboxes enabled/disabled
	ToggleProfCheckbox()
	ToggleQuestCheckbox()
end


local function IconSizeChanged( self, value ) 
	-- Called when the icon slider widget changes value
	iconDemoTexture:SetSize( value, value )
	iconSizeLabel:SetText( value )
end


local function InitVariables( self, event, loadedName )
	
	if loadedName ~= addonName then
		return
	end

	frame:UnregisterEvent( "ADDON_LOADED" )

	local configRealm = GetRealmName()
	local configChar = UnitName( "player" )
	
	if not ItemTooltipIconsConfig then
		ItemTooltipIconsConfig = {}
	end
	
	if not ItemTooltipIconsConfig[ configRealm ] then
		ItemTooltipIconsConfig[ configRealm ] = {}
	end
	
	if not ItemTooltipIconsConfig[ configRealm ][ configChar ] then
		ItemTooltipIconsConfig[ configRealm ][ configChar ] = {}
	end
	
	userVariables = ItemTooltipIconsConfig[ configRealm ][ configChar ]

	if next( userVariables ) == nil then -- Check for table entry
		-- Default config
		userVariables.showProfs = true
		userVariables.showQuests = true
		userVariables.profFlags = 0x3FF
		userVariables.questFlags = 0x1FFFFF
		userVariables.iconSize = 16
		-- New entries need init
	end
	
	RefreshWidgets()
	ItemProfConstants:ConfigChanged( userVariables )
end


local function CreateCheckbox( name, label )

	local check = CreateFrame( "CheckButton", name, frame, "ChatConfigCheckButtonTemplate" )
	check.Text:SetText( label )
	return check
end


local function CreateProfessionWidgets() 

	profsCheck = CreateCheckbox( "ToggleAllProfs", " Enable Profession Icons" )
	profsCheck:SetPoint( "TOPLEFT", "ConfigHeaderText", "BOTTOMLEFT", 0, -20 )
	profsCheck.tooltip = "If enabled profession icons will be displayed on items that are crafting materials"
	profsCheck:SetScript( "OnClick", ToggleProfCheckbox )

	PROF_CHECK[ 1 ] = CreateCheckbox( "ToggleCook", " Cooking" )
	PROF_CHECK[ 2 ] = CreateCheckbox( "ToggleFAid", " First Aid" )
	PROF_CHECK[ 3 ] = CreateCheckbox( "ToggleAlc", " Alchemy" )
	PROF_CHECK[ 4 ] = CreateCheckbox( "ToggleBS", " Blacksmithing" )
	PROF_CHECK[ 5 ] = CreateCheckbox( "ToggleEnc", " Enchanting" )
	PROF_CHECK[ 6 ] = CreateCheckbox( "ToggleEng", " Engineering" )
	PROF_CHECK[ 7 ] = CreateCheckbox( "ToggleLW", " Leatherworking" )
	PROF_CHECK[ 8 ] = CreateCheckbox( "ToggleTail", " Tailoring" )
	PROF_CHECK[ 9 ] = CreateCheckbox( "ToggleJC", " Jewelcrafting" )
	PROF_CHECK[ 10 ] = CreateCheckbox( "ToggleIns", " Inscription" )
	-- Align alphabetically in rows top to bottom
	PROF_CHECK[ 1 ]:SetPoint( "TOPLEFT", "ToggleAlc", "BOTTOMLEFT", 0, 5 )
	PROF_CHECK[ 2 ]:SetPoint( "LEFT", "ToggleEng", "RIGHT", 200, 0 )
	PROF_CHECK[ 3 ]:SetPoint( "TOPLEFT", "ToggleAllProfs", "BOTTOMLEFT", 25, 5 )
	PROF_CHECK[ 4 ]:SetPoint( "LEFT", "ToggleAlc", "RIGHT", 200, 0 )
	PROF_CHECK[ 5 ]:SetPoint( "LEFT", "ToggleCook", "RIGHT", 200, 0 )
	PROF_CHECK[ 6 ]:SetPoint( "TOPLEFT", "ToggleCook", "BOTTOMLEFT", 0, 5 )
	PROF_CHECK[ 7 ]:SetPoint( "TOPLEFT", "ToggleIns", "BOTTOMLEFT", 0, 5 )
	PROF_CHECK[ 8 ]:SetPoint( "LEFT", "ToggleLW", "RIGHT", 200, 0 )
	PROF_CHECK[ 9 ]:SetPoint( "LEFT", "ToggleIns", "RIGHT", 200, 0 )
	PROF_CHECK[ 10 ]:SetPoint( "TOPLEFT", "ToggleEng", "BOTTOMLEFT", 0, 5 )
end

local function CreateQuestWidgets() 
	-- Inscription/Monk not required (quests mostly for vanilla content)	
	questCheck = CreateCheckbox( "ToggleAllQuests", " Enable Quest Icons" )
	questCheck:SetPoint( "TOPLEFT", "ToggleAllProfs", "BOTTOMLEFT", 0, -130 )
	questCheck.tooltip = "If enabled quest icons will be displayed on items that are needed by quests"
	questCheck:SetScript( "OnClick", ToggleQuestCheckbox )

	QUEST_CHECK[ 1 ] = CreateCheckbox( "ToggleAllianceQ", " Alliance" )
	QUEST_CHECK[ 1 ]:SetPoint( "TOPLEFT", "ToggleAllQuests", "BOTTOMLEFT", 25, 5 )
	QUEST_CHECK[ 2 ] = CreateCheckbox( "ToggleHordeQ", " Horde" )
	QUEST_CHECK[ 2 ]:SetPoint( "TOPLEFT", "ToggleAllianceQ", "BOTTOMLEFT", 0, 5 )


	local classQuestLabel = frame:CreateFontString( "ClassQuestLabel", "OVERLAY", "GameTooltipText" )
	classQuestLabel:SetFont( "Fonts\\FRIZQT__.TTF", 14, "THINOUTLINE" )
	classQuestLabel:SetPoint( "TOPLEFT", "ToggleHordeQ", "BOTTOMLEFT", 0, -10 )
	classQuestLabel:SetTextColor( 1, 0.85, 0.15 )
	classQuestLabel:SetText( "Class Quests" )

	QUEST_CHECK[ 3 ] = CreateCheckbox( "ToggleDruidQ", " Druid" )
	QUEST_CHECK[ 3 ]:SetPoint( "TOPLEFT", "ClassQuestLabel", "BOTTOMLEFT", 0, -5 )
	QUEST_CHECK[ 4 ] = CreateCheckbox( "ToggleHunterQ", " Hunter" )
	QUEST_CHECK[ 4 ]:SetPoint( "LEFT", "ToggleDruidQ", "RIGHT", 200, 0 )
	QUEST_CHECK[ 5 ] = CreateCheckbox( "ToggleMageQ", " Mage" )
	QUEST_CHECK[ 5 ]:SetPoint( "LEFT", "ToggleHunterQ", "RIGHT", 200, 0 )

	QUEST_CHECK[ 6 ] = CreateCheckbox( "TogglePaladinQ", " Paladin" )
	QUEST_CHECK[ 6 ]:SetPoint( "TOPLEFT", "ToggleDruidQ", "BOTTOMLEFT", 0, 5 )
	QUEST_CHECK[ 7 ] = CreateCheckbox( "TogglePriestQ", " Priest" )
	QUEST_CHECK[ 7 ]:SetPoint( "LEFT", "TogglePaladinQ", "RIGHT", 200, 0 )
	QUEST_CHECK[ 8 ] = CreateCheckbox( "ToggleRogueQ", " Rogue" )
	QUEST_CHECK[ 8 ]:SetPoint( "LEFT", "TogglePriestQ", "RIGHT", 200, 0 )

	QUEST_CHECK[ 9 ] = CreateCheckbox( "ToggleShamanQ", " Shaman" )
	QUEST_CHECK[ 9 ]:SetPoint( "TOPLEFT", "TogglePaladinQ", "BOTTOMLEFT", 0, 5 )
	QUEST_CHECK[ 10 ] = CreateCheckbox( "ToggleWarlockQ", " Warlock" )
	QUEST_CHECK[ 10 ]:SetPoint( "LEFT", "ToggleShamanQ", "RIGHT", 200, 0 )
	QUEST_CHECK[ 11 ] = CreateCheckbox( "ToggleWarriorQ", " Warrior" )
	QUEST_CHECK[ 11 ]:SetPoint( "LEFT", "ToggleWarlockQ", "RIGHT", 200, 0 )


	local profQuestLabel = frame:CreateFontString( "ProfQuestLabel", "OVERLAY", "GameTooltipText" )
	profQuestLabel:SetFont( "Fonts\\FRIZQT__.TTF", 14, "THINOUTLINE" )
	profQuestLabel:SetPoint( "TOPLEFT", "ToggleShamanQ", "BOTTOMLEFT", 0, -10 )
	profQuestLabel:SetTextColor( 1, 0.85, 0.15 )
	profQuestLabel:SetText( "Profession Quests" )
	
	QUEST_CHECK[ 12 ] = CreateCheckbox( "ToggleCookQ", " Cooking" )
	QUEST_CHECK[ 13 ] = CreateCheckbox( "ToggleFAidQ", " First Aid" )
	QUEST_CHECK[ 14 ] = CreateCheckbox( "ToggleAlcQ", " Alchemy" )
	QUEST_CHECK[ 15 ] = CreateCheckbox( "ToggleBSQ", " Blacksmithing" )
	QUEST_CHECK[ 16 ] = CreateCheckbox( "ToggleEncQ", " Enchanting" )
	QUEST_CHECK[ 17 ] = CreateCheckbox( "ToggleEngQ", " Engineering" )
	QUEST_CHECK[ 18 ] = CreateCheckbox( "ToggleLWQ", " Leatherworking" )
	QUEST_CHECK[ 19 ] = CreateCheckbox( "ToggleTailQ", " Tailoring" )
	QUEST_CHECK[ 20 ] = CreateCheckbox( "ToggleJCQ", " Jewelcrafting" )
	-- alc bs cook, enc eng FA, JC LW tail
	QUEST_CHECK[ 12 ]:SetPoint( "LEFT", "ToggleBSQ", "RIGHT", 200, 0 )
	QUEST_CHECK[ 13 ]:SetPoint( "LEFT", "ToggleEngQ", "RIGHT", 200, 0 )
	QUEST_CHECK[ 14 ]:SetPoint( "TOPLEFT", "ProfQuestLabel", "BOTTOMLEFT", 0, -5 )
	QUEST_CHECK[ 15 ]:SetPoint( "LEFT", "ToggleAlcQ", "RIGHT", 200, 0 )
	QUEST_CHECK[ 16 ]:SetPoint( "TOPLEFT", "ToggleAlcQ", "BOTTOMLEFT", 0, 5 )
	QUEST_CHECK[ 17 ]:SetPoint( "LEFT", "ToggleEncQ", "RIGHT", 200, 0 )
	QUEST_CHECK[ 18 ]:SetPoint( "LEFT", "ToggleJCQ", "RIGHT", 200, 0 )
	QUEST_CHECK[ 19 ]:SetPoint( "LEFT", "ToggleLWQ", "RIGHT", 200, 0 )
	QUEST_CHECK[ 20 ]:SetPoint( "TOPLEFT", "ToggleEncQ", "BOTTOMLEFT", 0, 5 )
end

local function CreateIconResizeWidgets()
	
	iconSizeSlider = CreateFrame( "Slider", "SliderIconSize", frame, "UISliderTemplateWithLabels" )
	iconSizeSlider:SetPoint( "TOPLEFT", 20, -540 )
	iconSizeSlider:SetMinMaxValues( 8, 32 )
	iconSizeSlider:SetValueStep( 1 )
	iconSizeSlider:SetStepsPerPage( 1 )
	iconSizeSlider:SetSize( 200, 17 )
	iconSizeSlider:SetObeyStepOnDrag( true )
	iconSizeSlider:SetScript( "OnValueChanged", IconSizeChanged )
	iconSizeSlider.Text:SetText( "Icon Size" )
	iconSizeSlider.Low:SetText( nil )
	iconSizeSlider.High:SetText( nil )

	iconSizeLabel = iconSizeSlider:CreateFontString( "ValueLabel", "OVERLAY", nil )
	iconSizeLabel:SetFont( "Fonts\\FRIZQT__.TTF", 12, "THINOUTLINE" )
	iconSizeLabel:SetPoint( "LEFT", "SliderIconSize", "RIGHT", 16, 0 )

	iconDemoTexture = iconSizeSlider:CreateTexture( "PreviewIcon", "OVERLAY" )
	iconDemoTexture:SetPoint( "LEFT", "ValueLabel", "RIGHT", 24, 0 )
	iconDemoTexture:SetTexture( 136243 )
end


local dialogHeader = frame:CreateFontString( "ConfigHeaderText", "OVERLAY", "GameTooltipText" )
dialogHeader:SetFont( "Fonts\\FRIZQT__.TTF", 10, "THINOUTLINE" )
dialogHeader:SetPoint( "TOPLEFT", 20, -20 )
dialogHeader:SetText( "These options allow you control which icons are displayed on the item tooltips.\nThe quest icon can be filtered to display on items needed for quests of specific class/profession." )


CreateProfessionWidgets()
CreateQuestWidgets()
CreateIconResizeWidgets()


frame:Hide()	-- The options menu is calling hide before 1st show
frame:SetScript( "OnEvent", InitVariables )
frame:SetScript( "OnHide", SaveAndQuit )
frame:RegisterEvent( "ADDON_LOADED" )

if Settings and ( Settings.RegisterCanvasLayoutCategory and Settings.RegisterAddOnCategory ) then
	-- Classic client is now using the modern Settings API
	local category = _G.Settings.RegisterCanvasLayoutCategory( frame, frame.name )
	category.ID = frame.name
	_G.Settings.RegisterAddOnCategory( category )
else
	frame.okay = SaveAndQuit
	--frame:SetScript( "OnShow", RefreshWidgets )
	InterfaceOptions_AddCategory( frame )
end
