#!/usr/bin/python

import argparse, json
import song_generator
from generator import write_mid, generate

def main():
    parser = argparse.ArgumentParser(description = 'Parser for ninopianino')
    sub_parsers = parser.add_subparsers()

    song_gen = sub_parsers.add_parser('generate_song', description = 'Generates a completely random song without using a template. ')
    song_gen.set_defaults(sub = 'song_gen')

    template_gen = sub_parsers.add_parser('use_template', description = 'Loads a json file and generates a song with it. How the json template looks is documented in the readme. ')
    template_gen.set_defaults(sub = 'template_gen')

    song_gen.add_argument('--soundfont', help = "Path to the soundfont used to render the mid. If soundfont isn't supplied, the command won't work. ", required = True)
    song_gen.add_argument('--output', help = "Path where the song should be saved to. Defaults to /tmp. ", default = '/tmp/')

    template_gen.add_argument('--input', help = 'Path to the json template. ', required = True)
    template_gen.add_argument('--soundfont', help = 'Path to the soundfont. Will not work without a soundfont. ', required = True)
    template_gen.add_argument('--output', help = 'Path to the output. Defaults to /tmp/template_gen.wav.', default = '/tmp/template_gen')
    template_gen.add_argument('--no_tracks', help = 'Number of tracks. Default is 100. ', default = 100)


    args = parser.parse_args()

    if args.sub == 'song_gen':
        song_generator.generate_song(instruments_range = [1, 2, 4, 20, 21, 23, 24, 27, 34, 37, 39, 45, 47, 56, 68], segment_instruments_range = [1, 5, 8, 10, 11, 17, 24, 26, 28, 32, 33, 35, 36, 38, 39, 41, 45, 46, 52, 53, 54, 58, 68, 70, 72, 75, 76, 77, 78, 85], soundfont = args.soundfont, generate_dir = args.output)

    elif args.sub == 'template_gen':
        output = args.output
        blocks = json.loads(open(args.input, 'r').read())
        no_tracks = args.no_tracks
  
        mid = generate(blocks, no_tracks)
        write_mid(mid, args.output, args.soundfont)



main()
