"""Read the actual start label. Optional parsing uses an explicitly selected clock."""
import argparse
import json
from resolve_api_connection import connect
from resolve_api_timecode import Clock, read_clock, timecode_to_frames, validate_clock


def main():
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument("--rate")
    p.add_argument("--mode", choices=("ndf", "df"))
    a = p.parse_args()
    if bool(a.rate) != bool(a.mode):
        p.error("--rate and --mode must be supplied together")
    state = read_clock(connect())
    result = {"state": state, "operation": "read_only", "parsed": False}
    if a.rate:
        clock = Clock(a.rate, a.mode)
        validate_clock(state, clock)
        result.update({"parsed": True, "rate": str(clock.rate), "mode": clock.mode,
                       "frames_since_tc_zero": timecode_to_frames(state["start_timecode_raw"], clock),
                       "rate_origin": "explicit_user_selection_checked_against_label"})
    # The parsed label's count is NOT substituted for the API's start_frame.
    print(json.dumps(result, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
