local SCRIPT_TITLE = 'Groups move 2 next bar V1.0'

--[[

Synthesizer V Studio Pro Script
 
lua file name: GroupsMove2NextBar.lua

Usage: Move selected groups to next bar

Update: 1 - Creation

2026 - JF AVILES
--]]

-- Generated by JFA TranslateScripts.lua
function getTranslations(langCode)
	return getArrayLanguageStrings()[langCode]
end

-- Generated by JFA TranslateScripts.lua
function getArrayLanguageStrings()
	return {
		["en-us"] = {
			{"Please select groups first on Arrangement view!", "Please select groups first on Arrangement view!"},
		},
	}
end

function getClientInfo()
	return {
		name = SV:T(SCRIPT_TITLE),
		category = "_JFA_Groups",
		author = "JFAVILES",
		versionNumber = 1,
		minEditorVersion = 65540
	}
end

-- Define a class "NotesObject"
NotesObject = {
	project = nil,
	timeAxis = nil,
	editor = nil,
	hostinfo = nil,
	osType = "",
	osName = "",
	hostName = "",
	languageCode = "", 
	hostVersion = "",
	hostVersionNumber = 0,
	activeCurrentTrack = nil,
	groupRef = nil,
	groupsSelected = nil,
	direction = 1		-- Previous measure bar (-1 previous, +1 = next)
}

-- Constructor method for the NotesObject class
function NotesObject:new()
    local notesObject = {}
    setmetatable(notesObject, self)
    self.__index = self
	
    self.project = SV:getProject()
	self:getHostInformations()
	self.timeAxis = self.project:getTimeAxis()
	self.editor = SV:getMainEditor()
	self.activeCurrentTrack = self.editor:getCurrentTrack()
	self.groupRef = self.editor:getCurrentGroup()
	self.groupsSelected = SV:getArrangement():getSelection():getSelectedGroups()	
	
    return self
end

-- Show message dialog
function NotesObject:show(message)
	SV:showMessageBox(SV:T(SCRIPT_TITLE), message)
end

-- Get host informations
function NotesObject:getHostInformations()
	self.hostinfo = SV:getHostInfo()
	self.osType = self.hostinfo.osType  -- "macOS", "Linux", "Unknown", "Windows"
	self.osName = self.hostinfo.osName
	self.hostName = self.hostinfo.hostName
	self.languageCode = self.hostinfo.languageCode
	self.hostVersion = self.hostinfo.hostVersion
	self.hostVersionNumber = self.hostinfo.hostVersionNumber
end

-- Get first mesure after first note (or next position + n)
function NotesObject:getFirstMesure(notePos, nextPos)
	local measurePos = 0
	local measureBlick = 0
	local measureFirst = self.timeAxis:getMeasureAt(notePos) + nextPos
	local checkExistingMeasureMark = self.timeAxis:getMeasureMarkAt(measureFirst)
	
	if checkExistingMeasureMark ~= nil then
		if checkExistingMeasureMark.position == measureFirst then
			measurePos = checkExistingMeasureMark.position
			measureBlick = checkExistingMeasureMark.positionBlick
		else 
			self.timeAxis:addMeasureMark(measureFirst, 
						checkExistingMeasureMark.numerator, 
						checkExistingMeasureMark.denominator)
			local measureMark = self.timeAxis:getMeasureMarkAt(measureFirst)
			measurePos = measureMark.position
			measureBlick = measureMark.positionBlick
			self.timeAxis:removeMeasureMark(measureFirst)
		end
	else
		-- Temporary measure mark addition
		self.timeAxis:addMeasureMark(measureFirst, 4, 4)
		local measureMark = self.timeAxis:getMeasureMarkAt(measureFirst)
		measurePos = measureMark.position
		measureBlick = measureMark.positionBlick
		self.timeAxis:removeMeasureMark(measureFirst)
	end
	return measureBlick
end

-- Get notes
function NotesObject:getNotes(groupNotes)
	local notes = {}
	for iNote = 1, groupNotes:getNumNotes() do
		table.insert(notes, groupNotes:getNote(iNote))
	end
	return notes
end

-- Group move
function NotesObject:groupMove(group)
	local groupNotes = group:getTarget()
	local startPos = group:getOnset()
	local newPosBar = self:getFirstMesure(startPos, self.direction)
	local diff = newPosBar - startPos
	
	group:setTimeRange(newPosBar, group:getDuration())
	
	local arrNotes = self:getNotes(groupNotes)
	for i, note in ipairs(arrNotes) do
		note:setOnset(note:getOnset() + diff)
	end
end


-- Start process
function NotesObject:start()	
	-- Groups to move
	for i, group in ipairs(self.groupsSelected) do
		self:groupMove(group)
	end
end

function main()

	local notesObject = NotesObject:new()
	-- Check groups selected
	if #notesObject.groupsSelected == 0 then
		notesObject:show(SV:T("Please select groups first on Arrangement view!"))
	else
		notesObject:start()
	end
	SV:finish()
end