#!/usr/bin/env python3
#
# Format a spec
#

import jsontemplate
import pscheduler
import sys

from validate import spec_is_valid


try:
   format = sys.argv[1]
except IndexError:
   format = 'text/plain'

json = pscheduler.json_load(exit_on_error=True, max_schema=1)

valid, message = spec_is_valid(json)

if not valid:
    pscheduler.fail(message)


if format == 'text/plain':

   template = """
Starting Comment ... {.section starting-comment}{starting-comment}{.or}Not Specified{.end}
Duration ........... {.section duration}{duration}{.or}Not Specified{.end}
Parting Comment .... {.section parting-comment}{parting-comment}{.or}Not Specified{.end}
   """

elif format == 'text/html':

   template = """
<table border="0">
<tr><td><b>Starting Comment</b></td><td>
{.section starting-comment}{starting-comment}{.or}<i>Not Specified</i>{.end}
</td></tr>
<tr><td><b>Duration</b></td><td>
{.section duration}{duration}{.or}<i>Not Specified</i>{.end}
</td></tr>
<table border="0">
<tr><td><b>Parting Comment</b></td><td>
{.section parting-comment}{parting-comment}{.or}<i>Not Specified</i>{.end}
</td></tr>
</table>
   """

else:

   pscheduler.fail("Unsupported format '%s'" % format)


# TODO: Should probably handle exceptions in a nicer way.
print(jsontemplate.expand(template, json).strip())
