-- Copied from... default, I think, and only used on one screen
-- and it's a subscreen in Edit mode nobody thinks about

return function(Steps)
    local chartint = 1
    local Density = {}
    local streamMeasures = {}
    local PeakNPS = 0
    -- Keep track of the measure
    local measureCount = 0
    
    if Steps then
        for k,v in pairs( GAMESTATE:GetCurrentSong():GetAllSteps() ) do
            if v == Steps then
                chartint = k
                break
            end
        end
        Trace("[GetNPS] Loading Chart... ".. chartint)

        local TimingData = Steps:GetTimingData()
        local Chart = GAMESTATE:GetCurrentSong():GetNoteData(chartint)
        -- Keep track of the number of notes in the current measure while we iterate
        local NotesInThisMeasure = 0
        local NPSforThisMeasure = 0
        local DurationOfMeasureInSeconds = TimingData:GetElapsedTimeFromBeat((measureCount+1)*4) - TimingData:GetElapsedTimeFromBeat(measureCount*4)
        local MeasureMargin = TimingData:GetElapsedTimeFromBeat(measureCount*4)
        for k,v in pairs( Chart ) do
            local CurTime = TimingData:GetElapsedTimeFromBeat(v[1])
            if CurTime >= (DurationOfMeasureInSeconds+MeasureMargin) then
                NPSforThisMeasure = DurationOfMeasureInSeconds == 0 and 0 or math.round(NotesInThisMeasure/DurationOfMeasureInSeconds)
                PeakNPS = NPSforThisMeasure > PeakNPS and NPSforThisMeasure or PeakNPS
                if(NotesInThisMeasure >= 15) then
                    table.insert(streamMeasures, measureCount+1)
                end

                -- Reset stuff
                NotesInThisMeasure = 0
                Density[measureCount+1] = NPSforThisMeasure
                measureNotes = {}
                
                measureCount = measureCount + 1
                DurationOfMeasureInSeconds = TimingData:GetElapsedTimeFromBeat((measureCount+1)*4) - TimingData:GetElapsedTimeFromBeat(measureCount*4)
                MeasureMargin = TimingData:GetElapsedTimeFromBeat(measureCount*4)
            else
                if v[3] ~= "TapNoteType_Mine" and v[3] ~= "TapNoteType_Fake" then
                    NotesInThisMeasure = NotesInThisMeasure + 1
                end
            end
        end
        Density[measureCount+1] = NPSforThisMeasure
        Density[measureCount+2] = 0
    end
    return {PeakNPS,Density,streamMeasures,measureCount}
end
