import abjad
from abjadext import rmakers


def from_score(score):
    abjad.override(score).bar_line.stencil = False
    abjad.override(score).bar_number.stencil = False
    abjad.override(score).beam.positions = abjad.SchemePair((4, 4))
    abjad.override(score).spacing_spanner.strict_grace_spacing = True
    abjad.override(score).spacing_spanner.strict_note_spacing = True
    abjad.override(score).spacing_spanner.uniform_stretching = True
    abjad.override(score).stem.length = 8.25
    abjad.override(score).text_script.outside_staff_padding = 1
    abjad.override(score).time_signature.stencil = False
    abjad.override(score).tuplet_bracket.bracket_visibility = True
    abjad.override(score).tuplet_bracket.minimum_length = 3
    abjad.override(score).tuplet_bracket.outside_staff_padding = 1.5
    abjad.override(score).tuplet_bracket.padding = 1.5
    abjad.override(score).tuplet_bracket.springs_and_rods = abjad.Scheme(
        "ly:spanner::set-spacing-rods", verbatim=True
    )
    abjad.override(score).tuplet_bracket.staff_padding = 2.25
    abjad.override(score).tuplet_number.text = abjad.Scheme(
        "tuplet-number::calc-fraction-text", verbatim=True
    )
    moment = abjad.SchemeMoment((1, 24))
    abjad.setting(score).auto_beaming = False
    abjad.setting(score).proportional_notation_duration = moment
    abjad.setting(score).tuplet_full_length = True
    lilypond_file = abjad.LilyPondFile.new(score)
    lilypond_file.layout_block.indent = 0
    string = "#'((basic-distance . 28)(minimum-distance . 1)"
    string += "(padding . 4)(stretchability . 1))"
    scheme = abjad.Scheme(string, verbatim=True)
    lilypond_file.paper_block.system_system_spacing = scheme
    return lilypond_file


def from_selection(selection, divisions):
    voice = abjad.Voice()
    voice.append(selection)
    voice.remove_commands.append("Forbid_line_break_engraver")
    staff = abjad.Staff([voice], lilypond_type="RhythmicStaff")
    score = abjad.Score([staff])
    lilypond_file = from_score(score)
    return lilypond_file


def example():
    score = abjad.Score()
    divisions = abjad.sequence([(3, 8), (5, 4), (1, 4), (13, 16)])
    counts = abjad.sequence([1, 2, 3])
    selector = abjad.select().tuplets()[:-1]
    selector = selector.map(abjad.select().note(-1))
    for i in range(12):
        my_talea = rmakers.talea(counts, 16, extra_counts=[0, 1, 1])
        voice = abjad.Voice()
        talea_rhythm_maker = rmakers.stack(
            my_talea,
            rmakers.force_rest(abjad.select().logical_ties().get([0, -1])),
            rmakers.beam(),
            rmakers.extract_trivial(),
            rmakers.rewrite_meter(),
        )
        selections = talea_rhythm_maker(divisions)
        voice.append(selections)
        staff = abjad.Staff(lilypond_type="RhythmicStaff", name=str(i))
        staff.append(voice)
        score.append(staff)
        divisions = divisions.rotate(1)
        counts = counts.rotate(1)
    lilypond_file = from_score(score)
    abjad.show(lilypond_file)


__all__ = [
    "example",
    "from_score",
    "from_selection",
]
