local L = AceLibrary("AceLocale-2.2"):new("RangeHelper")

RangeHelper.options = {
	type = "group",
	args = {
		type = {
			type = "text",
			order = 1,
			name = L["Spell type"],
			desc = L["Spell type."],
			get = function() return RangeHelper:GetSpellType() end,
			set = function(v)
				RangeHelper:SetSpellType(v)
			end,
			validate = { L["Action Bars"], L["Spellbook"], L["Interact Only"] },
		},
		format = {
			type = "text",
			order = 2,
			name = L["Format"],
			desc = L["Format."],
			get = function() return RangeHelper.db.profile.format end,
			set = function(v)
				RangeHelper.db.profile.format = v
			end,
			validate = {
				["1"] = L["Within %dyd"],
				["2"] = L["%dyd - %dyd"],
				["3"] = L["%d - %d"],
			},
		},
		name = {
			type = "toggle",
			order = 3,
			name = L["Show spell name"],
			desc = L["Show spell name."],
			get = function() return RangeHelper.db.profile.spellname end,
			set = function(v)
				RangeHelper.db.profile.spellname = v
			end,
		},
		lock = {
			type = "toggle",
			order = 4,
			name = L["Lock frame"],
			desc = L["Lock frame."],
			get = function() return RangeHelper.db.profile.lock end,
			set = function(v)
				RangeHelper:SetFrameLock(v)
			end,
		},
		interact = {
			type = "text",
			order = 5,
			name = L["Show interact"],
			desc = L["Show interact."],
			get = function() return RangeHelper.db.profile.interact end,
			set = function(v)
				RangeHelper.db.profile.interact = v
			end,
			validate = {
				["1"] = L["All"],
				["2"] = L["Smart"],
				["3"] = L["None"],
			},
		},
		rangelimit = {
			type = "range",
			order = 6,
			name = L["Range limit"],
			desc = L["Range limit."],
			min = 30,
			max = 100,
			step = 1,
			get = function() return RangeHelper.db.profile.rangelimit end,
			set = function(v)
				RangeHelper.db.profile.rangelimit = v
				RangeHelper:ResetRangeSet()
			end,
		}
	},
}

-- exception, custom
-- CHECK BELOW

RH2OFFSET = 6
local order = RH2OFFSET + 2
local opt = nil

-- font
order = order + 1
RangeHelper.options.args.font = {}
opt = RangeHelper.options.args.font

opt.type = "group"
opt.order = order
opt.name = L["Set font"]
opt.desc = L["Set font."]
opt.args = {}

opt.args.name = {
	type = "text",
	order = 1,
	name = L["Font name"],
	desc = L["Font name."],
	get = function() return RangeHelper.db.profile.font.name end,
	set = function(v)
		RangeHelper.db.profile.font.name = v
		RangeHelper:SetFont()
	end,
	validate = { "Fonts\\2002.TTF", "Fonts\\2002b.TTF", "Fonts\\K_Damage.TTF", "Fonts\\K_Pagetext.TTF" },
}
opt.args.size = {
	type = "range",
	order = 2,
	name = L["Font size"],
	desc = L["Font size."],
	min = 10,
	max = 45,
	step = 1,
	get = function() return RangeHelper.db.profile.font.size end,
	set = function(v)
		RangeHelper.db.profile.font.size = v
		RangeHelper:SetFont()
	end,
}
opt.args.flags = {
	type = "group",
	order = 3,
	name = L["Flags"],
	desc = L["Flags."],
	args = {
		OUTLINE = {
			type = "toggle",
			order = 1,
			name = "OUTLINE",
			desc = L["OUTLINE"],
			get = function() return RangeHelper.db.profile.font.flags.OUTLINE end,
			set = function(v)
				RangeHelper.db.profile.font.flags.OUTLINE = v
				RangeHelper:SetFont()
			end,
		},
		THICKOUTLINE = {
			type = "toggle",
			order = 2,
			name = "THICKOUTLINE",
			desc = L["THICKOUTLINE"],
			get = function() return RangeHelper.db.profile.font.flags.THICKOUTLINE end,
			set = function(v)
				RangeHelper.db.profile.font.flags.THICKOUTLINE = v
				RangeHelper:SetFont()
			end,
		},
		MONOCHROME = {
			type = "toggle",
			order = 3,
			name = "MONOCHROME",
			desc = L["MONOCHROME"],
			get = function() return RangeHelper.db.profile.font.flags.MONOCHROME end,
			set = function(v)
				RangeHelper.db.profile.font.flags.MONOCHROME = v
				RangeHelper:SetFont()
			end,
		},
	},
}

-- point
order = order + 1
RangeHelper.options.args.point = {}
opt = RangeHelper.options.args.point

opt.type = "group"
opt.order = order
opt.name = L["Set point"]
opt.desc = L["Set point."]
opt.args = {}

opt.args.point = {
	type = "text",
	order = 1,
	name = L["Point"],
	desc = L["Point."],
	get = function() return RangeHelper.db.profile.point.point end,
	set = function(v)
		RangeHelper.db.profile.point.point = v
		RangeHelper:SetPoint()
	end,
	validate = { "TOPLEFT", "TOP", "TOPRIGHT", "LEFT", "CENTER", "RIGHT", "BOTTOMLEFT", "BOTTOM", "BOTTOMRIGHT" },
}
opt.args.relativeto = {
	type = "text",
	order = 2,
	name = L["Relative frame"],
	desc = L["Relative frame."],
	get = function() return RangeHelper.db.profile.point.relativeto end,
	set = function(v)
		RangeHelper.db.profile.point.relativeto = v
		RangeHelper:SetPoint()
	end,
	usage = L["<frame>"],
}
opt.args.relativepoint = {
	type = "text",
	order = 3,
	name = L["Relative point"],
	desc = L["Relative point."],
	get = function() return RangeHelper.db.profile.point.relativepoint end,
	set = function(v)
		RangeHelper.db.profile.point.relativepoint = v
		RangeHelper:SetPoint()
	end,
	validate = { "TOPLEFT", "TOP", "TOPRIGHT", "LEFT", "CENTER", "RIGHT", "BOTTOMLEFT", "BOTTOM", "BOTTOMRIGHT" },
}
opt.args.x = {
	type = "text",
	order = 4,
	name = L["X coordinate"],
	desc = L["X coordinate."],
	get = function() return RangeHelper.db.profile.point.x end,
	set = function(v)
		RangeHelper.db.profile.point.x = tonumber(v)
		RangeHelper:SetPoint()
	end,
	usage = L["<coordinates>"],
}
opt.args.y = {
	type = "text",
	order = 5,
	name = L["Y coordinate"],
	desc = L["Y coordinate."],
	get = function() return RangeHelper.db.profile.point.y end,
	set = function(v)
		RangeHelper.db.profile.point.y = tonumber(v)
		RangeHelper:SetPoint()
	end,
	usage = L["<coordinates>"],
}
opt.args.justifyH = {
	type = "text",
	order = 6,
	name = L["JustifyH"],
	desc = L["JustifyH."],
	get = function() return RangeHelper.db.profile.point.justifyH end,
	set = function(v)
		RangeHelper.db.profile.point.justifyH = v
		RangeHelper:SetPoint()
	end,
	validate = { "LEFT", "CENTER", "RIGHT" },
}

-- color
order = order + 1
RangeHelper.options.args.color = {}
opt = RangeHelper.options.args.color

opt.type = "group"
opt.order = order
opt.name = L["Set colors"]
opt.desc = L["Set colors."]
opt.args = {}

opt.args.assist = {
	type = "color",
	order = 1,
	name = L["Within assist"],
	desc = L["Within assist."],
	get = function() return unpack(RangeHelper.db.profile.color["Within assist"]) end,
	set = function(r, g, b)
		RangeHelper.db.profile.color["Within assist"] = { r, g, b }
	end,
}
opt.args.assistX = {
	type = "color",
	order = 2,
	name = L["Out of assist"],
	desc = L["Out of assist."],
	get = function() return unpack(RangeHelper.db.profile.color["Out of assist"]) end,
	set = function(r, g, b)
		RangeHelper.db.profile.color["Out of assist"] = { r, g, b }
	end,
}
opt.args.attack = {
	type = "color",
	order = 3,
	name = L["Within attack"],
	desc = L["Within attack."],
	get = function() return unpack(RangeHelper.db.profile.color["Within attack"]) end,
	set = function(r, g, b)
		RangeHelper.db.profile.color["Within attack"] = { r, g, b }
	end,
}
opt.args.attackX = {
	type = "color",
	order = 4,
	name = L["Out of attack"],
	desc = L["Out of attack."],
	get = function() return unpack(RangeHelper.db.profile.color["Out of attack"]) end,
	set = function(r, g, b)
		RangeHelper.db.profile.color["Out of attack"] = { r, g, b }
	end,
}
opt.args.interact = {
	type = "color",
	order = 5,
	name = L["Within interact"],
	desc = L["Within interact."],
	get = function() return unpack(RangeHelper.db.profile.color["Within interact"]) end,
	set = function(r, g, b)
		RangeHelper.db.profile.color["Within interact"] = { r, g, b }
	end,
}
opt.args.interactX = {
	type = "color",
	order = 6,
	name = L["Out of interact"],
	desc = L["Out of interact."],
	get = function() return unpack(RangeHelper.db.profile.color["Out of interact"]) end,
	set = function(r, g, b)
		RangeHelper.db.profile.color["Out of interact"] = { r, g, b }
	end,
}

-- troubleshooting
order = order + 1
RangeHelper.options.args.troubleshooting = {}
opt = RangeHelper.options.args.troubleshooting

opt.type = "group"
opt.order = order
opt.name = L["Troubleshooting"]
opt.desc = L["Troubleshooting."]
opt.args = {}

opt.args.list = {
	type = "execute",
	order = 1,
	name = L["Show list"],
	desc = L["Show list."],
	func = function()
		RangeHelper:ListRangeSet(false)
	end,
}
opt.args.detail = {
	type = "execute",
	order = 2,
	name = L["Show list in detail"],
	desc = L["Show list in detail."],
	func = function()
		RangeHelper:ListRangeSet(true)
	end,
}
opt.args.reset = {
	type = "execute",
	order = 3,
	name = L["Reset"],
	desc = L["Reset."],
	func = function()
		RangeHelper:ResetRangeSet()
	end,
}

function RangeHelper:GetExtraOptions()
	local order = RH2OFFSET
	local opt = nil
	
	-- exception
	order = order + 1
	RangeHelper.options.args.exception = {}
	opt = RangeHelper.options.args.exception
	
	opt.type = "group"
	opt.order = order
	opt.name = L["Exception list"]
	opt.desc = L["Exception list."]
	opt.args = {}
	
	opt.args.add = {
		type = "text",
		order = 1,
		name = L["Add exception"],
		desc = L["Add exception."],
		get = false,
		set = function(v)
			RangeHelper:AddException(v)
		end,
		usage = L["<name>"],
	}
	
	opt.args.remove = {
		type = "text",
		order = 2,
		name = L["Remove exception"],
		desc = L["Remove exception."],
		get = function() return "" end,
		set = function(v)
			RangeHelper:RemoveException(v)
		end,
		current = "",
		disabled = function() return #RangeHelper.db.profile.exception == 0 end,
		validate = RangeHelper.db.profile.exception,
	}
	
	opt.args.clear = {
		type = "execute",
		order = 3,
		name = L["Clear exception"],
		desc = L["Clear exception."],
		func = function()
			RangeHelper:ClearException()
		end,
	}
	
	-- custom
	order = order + 1
	RangeHelper.options.args.custom = {}
	opt = RangeHelper.options.args.custom
	
	opt.type = "group"
	opt.order = order
	opt.name = L["Custom list"]
	opt.desc = L["Custom list."]
	opt.args = {}
	
	opt.args.add = {
		type = "text",
		order = 1,
		name = L["Add custom"],
		desc = L["Add custom."],
		get = false,
		set = function(v)
			RangeHelper:AddCustom(v)
		end,
		usage = L["<name,range>"],
	}
	
	opt.args.remove = {
		type = "text",
		order = 2,
		name = L["Remove custom"],
		desc = L["Remove custom."],
		get = function() return "" end,
		set = function(v)
			RangeHelper:RemoveCustom(v)
		end,
		current = "",
		disabled = function() return #RangeHelper.db.profile.custom == 0 end,
		validate = RangeHelper.db.profile.custom,
	}
	
	opt.args.clear = {
		type = "execute",
		order = 3,
		name = L["Clear custom"],
		desc = L["Clear custom."],
		func = function()
			RangeHelper:ClearCustom()
		end,
	}
end

--RangeHelper:GetExtraOptions()
-- CHECK OnInitialize

RangeHelper:RegisterChatCommand({ "/rh2", "/rangehelper2" }, RangeHelper.options )
