-- lx format
local format = require("core.format")

format.register({
	name = "Cue Sheet",
	shortname = "CUE",
	author = "liqube",

	extensions = { "cue" },

	patterns = {
		-- REM DISCID line
		{
			pattern = "(REM)(%s+DISCID)(%s+)([%da-fA-F]+)",
			type = { "comment", "meta", "normal", "hex" },
			line_start = true,
		},

		-- REM line with subkey
		{
			pattern = "(REM)(%s+)([%a_][%w_%-]*)",
			type = { "comment", "normal", "meta" },
			line_start = true,
		},

		-- Plain REM line fallback
		{ pattern = "REM.-\n", type = "comment", line_start = true },

		-- Quoted strings
		{ pattern = { '"', '"', "\\" }, type = "string" },

		-- Track type with slash form
		{ pattern = "[%a%d]+/%d+", type = "keyword2" },

		-- Timestamp
		{ pattern = "%d%d:%d%d:%d%d", type = "number" },

		-- ISRC code
		{ pattern = "[%u%d][%u%d][%u%d][%u%d][%u%d][%u%d][%u%d][%d%d%d%d%d]", type = "keyword2" },

		-- Catalog number
		{ pattern = "%d%d%d%d%d%d%d%d%d%d%d%d%d", type = "number" },

		-- Plain numbers
		{ pattern = "%d+", type = "number" },

		-- Generic identifiers
		{ pattern = "[%a_][%w_%-]*", type = "symbol" },
	},

	line_patterns = {
		-- FILE line
		{
			pattern = "(FILE)",
			type = { "keyword" },
			consume = false,
		},

		-- TRACK line
		{
			pattern = "(TRACK)",
			type = { "keyword" },
			consume = false,
		},

		-- INDEX line
		{
			pattern = "(INDEX)",
			type = { "keyword" },
			consume = false,
		},

		-- PREGAP line
		{
			pattern = "(PREGAP)",
			type = { "keyword" },
			consume = false,
		},

		-- POSTGAP line
		{
			pattern = "(POSTGAP)",
			type = { "keyword" },
			consume = false,
		},

		-- TITLE line
		{
			pattern = "(TITLE)",
			type = { "keyword" },
			consume = false,
		},

		-- PERFORMER line
		{
			pattern = "(PERFORMER)",
			type = { "keyword" },
			consume = false,
		},

		-- SONGWRITER line
		{
			pattern = "(SONGWRITER)",
			type = { "keyword" },
			consume = false,
		},

		-- CATALOG line
		{
			pattern = "(CATALOG)",
			type = { "keyword" },
			consume = false,
		},

		-- ISRC line
		{
			pattern = "(ISRC)",
			type = { "keyword" },
			consume = false,
		},

		-- FLAGS line
		{
			pattern = "(FLAGS)",
			type = { "keyword" },
			consume = false,
		},

		-- CDTEXTFILE line
		{
			pattern = "(CDTEXTFILE)",
			type = { "keyword" },
			consume = false,
		},
	},

	outline_patterns = {
		-- File entry
		{ pattern = "%s*FILE%s+", line_start = true },

		-- Track entry
		{ pattern = "%s*TRACK%s+%d+%s+[%a%d/]+", line_start = true },

		-- Disc title
--		{ pattern = "%s*TITLE%s+\".-\"", line_start = true },

		-- Disc performer
--		{ pattern = "%s*PERFORMER%s+\".-\"", line_start = true },
	},

	symbols = {
		-- Core commands
		["REM"] = "comment",
		["FILE"] = "keyword",
		["TRACK"] = "keyword",
		["INDEX"] = "keyword",
		["PREGAP"] = "keyword",
		["POSTGAP"] = "keyword",
		["TITLE"] = "keyword",
		["PERFORMER"] = "keyword",
		["SONGWRITER"] = "keyword",
		["CATALOG"] = "keyword",
		["ISRC"] = "keyword",
		["FLAGS"] = "keyword",
		["CDTEXTFILE"] = "keyword",

		-- Common track and file types
		["AUDIO"] = "keyword2",
		["CDG"] = "keyword2",
		["WAVE"] = "keyword2",
		["MP3"] = "keyword2",
		["AIFF"] = "keyword2",
		["BINARY"] = "keyword2",
		["MOTOROLA"] = "keyword2",

		-- Common track mode types
		["MODE1/2048"] = "keyword2",
		["MODE1/2352"] = "keyword2",
		["MODE2/2336"] = "keyword2",
		["MODE2/2352"] = "keyword2",
		["CDI/2336"] = "keyword2",
		["CDI/2352"] = "keyword2",

		-- Common flag values
		["DCP"] = "keyword2",
		["4CH"] = "keyword2",
		["PRE"] = "keyword2",
		["SCMS"] = "keyword2",

		-- Common REM subkeys
		["GENRE"] = "meta",
		["DATE"] = "meta",
		["DISCID"] = "meta",
		["COMMENT"] = "meta",
		["REPLAYGAIN_ALBUM_GAIN"] = "meta",
		["REPLAYGAIN_ALBUM_PEAK"] = "meta",
		["REPLAYGAIN_TRACK_GAIN"] = "meta",
		["REPLAYGAIN_TRACK_PEAK"] = "meta",
	},
})
