"""Inspect audio-only format tokens or explicitly prepare WAV settings. Never render."""
import argparse
from resolve_api_connection import connect
from resolve_api_delivery import (cli_write, cli_flags, current, audio_catalog,
                                  output_path, apply_settings, print_result)


def main():
    p = argparse.ArgumentParser(description=__doc__)
    cli_write(p)
    p.add_argument('--step', choices=['catalog', 'configure'], default='catalog')
    p.add_argument('--codec', help='Exact value from GetAudioRenderCodecs("wav")')
    p.add_argument('--directory'); p.add_argument('--name')
    p.add_argument('--sample-rate', type=int, choices=[44100,48000,96000], default=48000)
    p.add_argument('--bit-depth', type=int, choices=[16,24], default=24)
    a = p.parse_args()
    if a.step == 'catalog' and a.apply: p.error('--apply is not valid for the read-only catalogue')
    if a.step == 'configure' and not all((a.codec, a.directory, a.name)):
        p.error('configure requires --codec, --directory and --name')

    def work():
        if a.step == 'configure':
            output_path(a.directory, a.name)  # Validate local destination before native connection.
            return apply_settings(connect(), mode='audio', codec=a.codec, directory=a.directory,
                                  name=a.name, sample_rate=a.sample_rate, bit_depth=a.bit_depth,
                                  **cli_flags(a))
        project, timeline, pid, tid = current(connect(), a.project_id, a.timeline_id)
        return {'status':'observed','project_id':pid,'timeline_id':tid,
                'catalog':audio_catalog(project),'mutations_sent':0}
    return print_result(work)


if __name__ == "__main__":
    raise SystemExit(main())
