local SCRIPT_TITLE = 'Group size V2.0'

--[[

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

Usage: Run this script to adjust size to the last note inside
During recording more notes could be inserted than the auto created group of notes,
so this script expand current group size to the last existing note inside.

Note: Compatible with the Synthesizer V version 2.1.1

2025 - 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 a group first!", "Please select a group first!"},
		},
	}
end

function getClientInfo()
	return {
		name = SV:T(SCRIPT_TITLE),
		category = "_JFA_Groups",
		author = "JFAVILES",
		versionNumber = 2,
		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,
}

-- 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()
	
    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

-- Start check current group
function NotesObject:getObjectProperties(obj)
	local result = ""
	for k, v in pairs(obj) do
		if obj[k] ~= nil then
			result = result .. k .. "=" .. tostring(v) .. "\r"
			if type(v) == "table" then
				result = result .. self:getObjectProperties(v)  .. "\r"
			end
		end
	end
	return result
end

-- Get first mesure before first note
function NotesObject:getFirstMesure(notePos)
	local measurePos = 0
	local measureBlick = 0
	local measureFirst = self.timeAxis:getMeasureAt(notePos)
	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

-- Start process
function NotesObject:start()
	local groupNotes = self.groupRef:getTarget()
	local firstNotePos = groupNotes:getNote(1):getOnset()
	local lastNoteDuration = groupNotes:getNote(groupNotes:getNumNotes()):getDuration()
	local lastNotePos = groupNotes:getNote(groupNotes:getNumNotes()):getOnset()
	
	-- Get first mesure bar
	local firstMeasureBar = self:getFirstMesure(firstNotePos) + 1 
	local durationBetweenFirstLastNote = lastNotePos - firstMeasureBar -- or firstNotePos if you prefer
	local newGap = durationBetweenFirstLastNote + lastNoteDuration
	
	self.groupRef:setTimeRange(firstMeasureBar, newGap)
end

function main()

	local notesObject = NotesObject:new()
	-- Is current group
	if #notesObject.groupRef == nil then
		notesObject:show(SV:T("Please select a group first!"))
	else
		notesObject:start()
	end
	SV:finish()
end