local addonName, addon = ...
addonName = addonName:match("^(.+)_Config$")
addon = _G[addonName]
if not addon then return end

local AceGUI = LibStub("AceGUI-3.0")

local path = [[Interface\AddOns\]] .. addonName .. [[\Skins\]]
local skins = {
	[path .. "Alliance"] = "Alliance",
	[path .. "Horde"] = "Horde",
	[path .. "BlackMetal"] = "Black Metal",
	[path .. "Elf"] = "Elf",
	[path .. "HordeMac"] = "Horde Minimal",
}

local styles = {
	"Vertical",
	"Horizontal",
}
local growDirections = {
	"Up-Right",
	"Up-Left",
	"Down-Right",
	"Down-Left",
	"Centered-Right",
	"Centered-Left",
	"Centered",
}

local GmapShape = {
	["Interface\\BUTTONS\\WHITE8X8"] = "Square",
	["Textures\\MinimapMask"] = "Circle",
}

local NSmapShape = {
	["Interface\\BUTTONS\\WHITE8X8"] = "Square",
	["Textures\\MinimapMask"] = "Circle",
}

--[[-----------------------------------------------------------------------------
Common for all options
-------------------------------------------------------------------------------]]
local function Get(info)
	return addon.settings[info[#info]]
end

local function Set(info, value)
	addon.settings[info[#info]] = value or false
end

--[[-----------------------------------------------------------------------------
Frame Mover functions and setup
-------------------------------------------------------------------------------]]
local framelist = {}
local function UpdateFrameList()
	wipe(framelist)
	for name, value in pairs(GMoveableFrames) do
		if value == false then
			framelist[name] = name
		elseif value == true then
			framelist[name] = name .. " (moves parent)"
		else
			framelist[name] = name .. " (moves " .. value .. ")"
		end
	end
end
UpdateFrameList()

--[[-----------------------------------------------------------------------------
Reset WTF Func
-------------------------------------------------------------------------------]]
local function WTFResetReload()
	addon:ResetWTF()
	ReloadUI()
end

--[[-----------------------------------------------------------------------------
Options
-------------------------------------------------------------------------------]]
local options = {
	type = 'group',
	childGroups = "tab",
	get = Get,
	set = Set,
	args = {
		option1 = {
			name = "General",
			desc = "General Options.",
			type = "group",
			order = 1,
			args = {
				lockFrames = {
					type = 'toggle',
					order = 1,
					name = "Lock Frames",
					desc = "Prevent frames from being moved by the mouse."
				},
				showWatchFrame = {
							type = 'toggle',
							order = 2,
							name = "Show Objectives",
							desc = "Show Objectives/Watch Frame.",
							set = function(info, value)
								Set(info, value)
									addon:SafeCall("InitWatchFrame")
							end
				},
				showExpBar = {
							type = 'toggle',
							order = 3,
							name = "Show XP Bar",
							desc = "Show GrimUI Experiance Bar.",
							set = function(info, value)
								Set(info, value)
									addon:SafeCall("ConfigureExpBar")
							end
				},
				showRepBar = {
							type = 'toggle',
							order = 4,
							name = "Show Rep Bar",
							desc = "Show GrimUI Reputatiom/Faction Bar.",
							set = function(info, value)
								Set(info, value)
									addon:SafeCall("ConfigureRepBar")
							end
				},
				useViewPort = {
							type = 'toggle',
							order = 5,
							name = "Use ViewPort",
							desc = "Moves world frame up - currently causes water problems turn water detail down in order to us without glitch.",
							set = function(info, value)
								Set(info, value)
									addon:SafeCall("SetupViewPort")
							end
				},
				Art = {
					type = 'group', inline = true,
					order = 6,
					name = "Art",
					args = {
						uiSkin = {
							type = 'select',
							order = 1,
							name = "Art Skin",
							desc = "Choose which skin to use.",
							values = skins,
							set = function(info, value)
								Set(info, value)
								addon:SafeCall("ConfigureSkin")
							end
						},
						artSetPoint = {
									type = 'range',
									order = 2,
									name = "Art Skin Width",
									desc = "Set the width of the main art skin.",
									min = -1920, 
									max = 1920, 
									step = 2,
									set = function(info, value)
										Set(info, value)
										addon:SafeCall("ArtSetPoint")
									end
								},
						artSetAlpha = {
									type = 'range',
									order = 3,
									name = "Art Skin Alpha",
									desc = "Set the Alpha of the main art skin.",
									min = 0, 
									max = 1, 
									step = 0.01,
									set = function(info, value)
										Set(info, value)
										addon:SafeCall("ArtSetAlpha")
									end
								},
						MapArt = {
									type = 'toggle',
									order = 4,
									name = "Show MiniMap Art",
									desc = "Show/Hide MiniMap Artwork.",
									set = function(info, value)
										Set(info, value)
										addon:SafeCall("ShowMapArt")
									end
								},
						},
					},
				ResetWTF = {
					type = 'execute',
					order = 99,
					name = "Reset",
					desc = "Wipe all WTF file settings!",
					func = function(info)
					InterfaceOptionsFrame:Hide()
					addon:OpenReloadFrame(WTFResetReload)
				end
				},
			},
		},
		option2 = {
			name = "Unit Frames",
			desc = "Unit Frame Options.",
			type = "group",
			order = 2,
			args = {
				PlayerFrame = {
					type = 'group', inline = true,
					order = 1,
					name = "Player Frame",
					args = {
						playerFrameScale = {
							type = 'range',
							order = 3,
							name = "Scale",
							desc = "Set the Scale of PlayerFrame.",
							min = 0.4, 
							max = 2.4, 
							step = 0.1,
							set = function(info, value)
								Set(info, value)
								addon:SafeCall("ConfigurePlayerFrame")
							end
						},
						
						showPlayerFrame = {
							type = 'toggle',
							order = 1,
							name = "Enable",
							desc = "Show " .. addonName .. "'s player frame.",
							set = function(info, value)
								Set(info, value)
								addon:SafeCall("ConfigurePlayerFrame")
							end
						},
						hideBlizPlayerFrame = {
							type = 'toggle',
							order = 2,
							name = "Hide Blizzard",
							desc = "Disable Blizzard's default player frame.",
							set = function(info, value)
								Set(info, value)
									addon:SafeCall("ConfigureBlizPlayerFrame")
							end
						},
					},
				},
				PartyFrames = {
					type = 'group', inline = true,
					order = 2,
					name = "Party Frames",
					args = {
						showPartyFrames = {
							type = 'toggle',
							order = 1,
							name = "Enable",
							desc = "Show " .. addonName .. "'s party frames.",
							set = function(info, value)
								Set(info, value)
								addon:SafeCall("ConfigurePartyFrames")
							end
						},
						hideGrimPartyInRaid = {
							type = 'toggle',
							order = 2,
							name = "Hide Party in Raid",
							desc = "Hide GrimUI Party Frames in Raid.",
							set = function(info, value)
								Set(info, value)
								addon:SafeCall("ConfigurePartyFrames")
							end
						},
						hideBlizPartyFrames = {
							type = 'toggle',
							order = 3,
							name = "Hide Blizzard",
							desc = "Disable Blizzard's default party frames.",
							set = function(info, value)
								Set(info, value)
								addon:SafeCall("ConfigureBlizPartyFrames")
							end
						},
						stylePartyFrames = {
							type = 'select',
							order = 4,
							name = "Party Frames Style",
							desc = "Choose which style to use for party frames.",
							values = styles,
							set = function(info, value)
								Set(info, value)
								addon:SafeCall("StylePartyFrames")
							end
						},
					},
				},
			TargetFrame = {
					type = 'group', inline = true,
					order = -3,
					name = "Target Frame",
					args = {
						hideBlizTargetFrame = {
							type = 'toggle',
							order = 1,
							name = "Hide Blizzard",
							desc = "Hide Blizzard Target Frame.",
							set = function(info, value)
								Set(info, value)
								addon:SafeCall("ConfigureBlizTargetFrame")
							end
						},
					},
				},
			},
		},
		
		option5 = {
			name = "Chat",
			desc = "Chat Options.",
			type = "group",
			order = 5,
			args = {
				showCombatLog = {
					type = 'toggle',
					order = 2,
					name = "Show Combat Log",
					desc = "Enable the combat log window.",
					set = function(info, value)
						Set(info, value)
						addon:SafeCall("ConfigureCombatLog")
					end
				},
				showInfoLog = {
					type = 'toggle',
					order = 2,
					name = "Show Info Log",
					desc = "Enable the info log window.",
					set = function(info, value)
						Set(info, value)
						addon:SafeCall("ConfigureInfoLog")
					end
				},
				showChatTabs = {
					type = 'toggle',
					order = 3,
					name = "Show Chat Tabs",
					desc = "Show Chat Frame 2 and 6's Chat Tabs.",
					set = function(info, value)
						Set(info, value)
						addon:SafeCall("ConfigureChatTabs")
					end
				},
				ResetChat = {
					type = 'execute',
					order = -4,
					name = "Reset Chat",
					desc = "Reset the chat windows to " .. addonName .. "'s defaults.",
					func = function(info)
						addon:SafeCall("ResetChatFrames")
					end
				},
			},
		},
	option6 = {
			name = "Hud",
			desc = "Heads Up Display Options.",
			type = "group",
			order = 6,
			args = {
				hudShow = {
					type = 'toggle',
					order = 5,
					name = "Show Hud",
					desc = "Toggles on/off the GrimUI HUD.",
					set = function(info, value)
						Set(info, value)
						addon:SafeCall("SetupGHud")
					end
				},
				hudWidth = {
					type = 'range',
					order = 3,
					name = "Left/Right distance.",
					desc = "Set the distance left-right of the Hud bars.",
					min = -600, max = 600, step = 1,
					set = function(info, value)
						Set(info, value)
						addon:LayoutHudBars()
					end
				},
				hudHeight = {
					type = 'range',
					order = 3,
					name = "Top/Bottom distance.",
					desc = "Set the distance top-bottom of the Hud bars.",
					min = -600, max = 600, step = 1,
					set = function(info, value)
						Set(info, value)
						addon:LayoutHudBars()
					end
				},
				hudScale = {
					type = 'range',
					order = 3,
					name = "Scale.",
					desc = "Set the Hud Scale.",
					min = 0.1, max = 3.0, step = 0.01,
					set = function(info, value)
						Set(info, value)
						addon:GHudScale()
					end
				},
			}
		},
	option7 = {
			name = "DashBoard",
			desc = "DashBoard Options.",
			type = "group",
			order = 6,
			args = {
				DashBoard = {
					type = 'group', inline = true,
					order = -3,
					name = "DasrhBoard",
					args = {
						latencyShow = {
							type = 'toggle',
							order = 1,
							name = "FPS/Latency",
							desc = "Show DashBoard FPS and Latency frames.",
							set = function(info, value)
								Set(info, value)
								addon:SafeCall("DashLayout")
							end
						},
						speedShow = {
							type = 'toggle',
							order = 1,
							name = "Speed",
							desc = "Show DashBoard Speed frame.",
							set = function(info, value)
								Set(info, value)
								addon:SafeCall("DashLayout")
							end
						},
						clock24hr = {
							type = 'toggle',
							order = 1,
							name = "Clock 24hr mode",
							desc = "Toggle clock 12/24hr mode.",
							set = function(info, value)
								Set(info, value)
								addon:SafeCall("SetClockHrFormat")
							end
						},
						dashboardScale = {
							type = 'range',
							order = 3,
							name = "Scale",
							desc = "Set the DashBoard Scale.",
							min = 0.50, 
							max = 2.5, 
							step = 0.01,
							set = function(info, value)
								Set(info, value)
								addon:SafeCall("DashLayout")
							end
						},
					},
				},
			},
		},
	option8 = {
		name = "Frame Mover",
		desc = "Make Frames Move-Able.",
		type = "group", 
		order = 8,
		args = {
			addFrame = {
				type = 'input', 
				order = 1,
				name = "Add Frame",
				desc = "Add Frame to Moveable Frames List Usage: Frame True/False/Frame\nTrue - Move Parent Frame\nFalse - Move This Frame Only\nFrame - Move This Frame Instead",
				get = function(info)
					return ""
				end,
				set = function(info, value)
					info = GMoveableFrames
					local key, special = strsplit(" ", value)
					if special == "true" then
						special = true
					elseif special == "false" then
						special = false
					elseif special == "" then
						special = false
					end
					addon:AddMoveableFrame(key, special)
					UpdateFrameList()
				end
			},
			frameStack = {
					type = 'execute',
					order = 2,
					name = "Framestack",
					desc = "Show Framestack. /framestack",
					func = function(info)
						addon:RunSlashCmd("/framestack")
					end
			},
			ResetFrames = {
					type = 'execute',
					order = 3,
					name = "Reset Frames",
					desc = "Wipe all frame position data.",
					func = function(info)
					local movedFrames = addon.settings.movedFrames
					for name in pairs(movedFrames) do
						addon:UnlockFrame(name)
						movedFrames[name] = nil
					end
					addon:UnlockFrame("WatchFrame")
					addon:setupWatchFrame()
					addon:SafeCall("ResetPlayerFrame")
					addon:SafeCall("ResetPartyFrames")
					addon:SafeCall("ResetExpRepBar")
					addon:SafeCall("PlayerFrameRESetScale")
					addon:SafeCall("ResetDevBar")
					addon:SafeCall("ArtSetPointReset")
					addon:SafeCall("LFGEyeFix")
					addon:SafeCall("MiniMapPVPFix")
					addon:SafeCall("GSetAnchorPoint")
					addon:SafeCall("MiniMapReset")
				end
				},
			removeFrame = {
				type = 'multiselect',
				order = 4,
				name = "Remove Frame",
				desc = "Click to remove this frame from Moveable Frames List. Note: Position may not reset until after a UIReload.",
				get = addon.DoNothing,
				values = framelist,
				set = function(info, key, state)
					if state then
						addon:RemMoveableFrame(key)
						UpdateFrameList()
					end
				end
			}
		},
	},
		option19 = {
			name = "ToolTips",
			desc = "ToolTip Options.",
			type = "group",
			order = 19,
			args = {
				tooltipAnchor = {
					type = 'toggle',
					order = 1,
					name = "Anchor to Cursor",
					desc = "Anchors tooltips to the cursor.",
					set = function(info, value)
						Set(info, value)
					end
				},
				growDirection = {
					type = 'select',
					order = 2,
					name = "Expand Direction",
					desc = "Set what direction the Tooltip expands to.",
					values = growDirections,
					set = function(info, value)
						Set(info, value)
						addon:TTGrowDirection(tooltip)
					end
				},
				showTTanchor = {
					type = 'execute',
					order = 2,
					name = "ToolTip Anchor",
					desc = "Show/Hide ToolTip Anchor.",
					func = function(info)
						addon:ShowTTAnchor()
					end
				},
				tooltipIconSize = {
					type = 'range',
					order = 3,
					name = "Tooltip Icon Size",
					desc = "Set the size of icons added to tooltips.",
					min = 8, max = 32, step = 1
				},
				ShowOnToolTip = {
					type = 'group', inline = true,
					order = -3,
					name = "Show On ToolTip",
					args = {
						showToT = {
							type = 'toggle',
							order = 2,
							name = "ToT",
							desc = "Show Target of Target on Tooltip.",
							set = function(info, value)
								Set(info, value)
							end
						},
						showTitle = {
							type = 'toggle',
							order = 1,
							name = "Title",
							desc = "Show Title and Player Name on Tooltip.",
							set = function(info, value)
								Set(info, value)
							end
						},
						showRaidIcon = {
							type = 'toggle',
							order = 1,
							name = "Raid Icon",
							desc = "Show Raid Icons on Tooltip.",
							set = function(info, value)
								Set(info, value)
							end
						},
						showHealthBar = {
							type = 'toggle',
							order = 1,
							name = "Health Bar",
							desc = "Show Health Bar on Tooltip.",
							set = function(info, value)
								Set(info, value)
							end
						},
					showSpellIcon = {
							type = 'toggle',
							order = 1,
							name = "Spell Icon",
							desc = "Show Spell Icons on Tooltip.",
							set = function(info, value)
								Set(info, value)
							end
						},
					showItemIcon = {
							type = 'toggle',
							order = 1,
							name = "Item Icon",
							desc = "Show Item Icon on Tooltip.",
							set = function(info, value)
								Set(info, value)
							end
						},
					},
				},
			},
		},
		
		option80 = {
			name = "Maps",
			desc = "Worldmap, Minimap and Not So Minimap options.",
			type = "group",
			order = 20,
			args = {
				MinimapOpt = {
						type = 'group', inline = true,
						order = -3,
						name = "Minimap",
						args = {
							ShowMiniMap = {
								type = 'toggle',
								order = 1,
								name = "Show Minimap",
								desc = "Show/Hide the Minimap.",
								set = function(info, value)
									Set(info, value)
									addon:SafeCall("ShowHideMiniMap")
								end
							},
							GmapMask = {
							type = 'select',
							order = 1,
							name = "Minimap Shape",
							desc = "Choose the shape of the Minimap.",
							values = GmapShape,
							set = function(info, value)
								Set(info, value)
								addon:SafeCall("SetGMiniMap")
							end
							},
							NSmapMask = {
							type = 'select',
							order = 1,
							name = "Not So Minimap Shape",
							desc = "Choose the shape of the Not So Minimap.",
							values = NSmapShape,
							set = function(info, value)
								Set(info, value)
								addon:SafeCall("SetGMiniMap")
							end
							},
					
					},
				},
			},
		},
		
	option99 = {
			name = "Dev Tools",
			desc = "Development Tools.",
			type = "group",
			order = 21,
			args = {
				showDevBar = {
					type = 'toggle',
					order = 1,
					name = "GrimUI DevBar",
					desc = "GrimUI Development Bar, easy /framestack /reload button.",
					set = function(info, value)
						Set(info, value)
						addon:SafeCall("ShowDevBar")
					end
				},
				toggleTaintLog = {
					type = 'toggle',
					order = 2,
					name = "Taint Logging",
					desc = "Enable Taint Logging - MAY REQUIRE RELOAD.",
					set = function(info, value)
						Set(info, value)
						addon:SafeCall("ToggleTaintLog")
					end
				},
				BlizBuildInfo = {
					type = 'execute',
					order = 3,
					name = "Blizzard Build Info",
					desc = "Retrive current Blizzard build infomration.",
					func = function(info)
						addon:SafeCall("BlizBuildInfo")
					end
				},
			},
		},
	},
}

--[[-----------------------------------------------------------------------------
Initialize
-------------------------------------------------------------------------------]]
LibStub('AceConfigRegistry-3.0'):RegisterOptionsTable(addonName, options)

addon.configPanel:AssignOptions(addonName)
--addon.configPanel:SetDesc("These options allow you to change the appearance and behavior of " .. addonName .. ".")
addon.configPanel:SetInfo("Version: |cff33ff99" .. GetAddOnMetadata(addonName, 'Version'))



