#!/usr/bin/env perl
use strict;
use warnings;

# Show two octaves of a chord on a full-length fretboard.

# For author testing only:
#use lib map { "$ENV{HOME}/sandbox/$_/lib" } qw(MIDI-Chord-Guitar Music-FretboardDiagram);

use Data::Dumper::Compact qw(ddc);
use MIDI::Chord::Guitar;
use Music::FretboardDiagram;

my $note   = shift || 'C3'; # a note in ISO format: <name><octave>
my $flavor = shift || '';   # chord: 'm'=minor, ''=major, '7'=7th, etc.
my $frets  = shift || 16;   # number of frets to display

(my $name = $note) =~ s/^([A-G][b#]?)\d+$/$1/;

my $mcg = MIDI::Chord::Guitar->new(
    # For author testing only:
    #voicing_file => '/home/gene/sandbox/MIDI-Chord-Guitar/share/midi-guitar-chord-voicings.csv'
);

my $fingerings = $mcg->fingering($note, $flavor);
my @specs = map { _get_spec($_) } @$fingerings;

$fingerings = $mcg->fingering(++$note, $flavor);
push @specs, map { _get_spec($_) } @$fingerings;

@specs = sort { $a->[0] <=> $b->[0] } @specs;
warn ddc(\@specs);

my $dia = Music::FretboardDiagram->new(
  frets    => $frets,
  chord    => \@specs,
  absolute => 1,
  outfile  => $0,
  horiz    => 1,
  size     => 50,
  showname => "$name$flavor",
  verbose  => 1,
);

$dia->draw;

sub _get_spec {
    my ($fingering) = @_;

    my ($chord, $posn) = split /-/, $fingering;

    $chord =~ s/[oO0xX]/-/g; # don't show open or muted strings

    return [ $posn, $chord ];
}
