#!/usr/bin/env python3

import yaml
import json
import sys
import argparse
import configparser
import textwrap
import os

desc = """A tool that queries OpenManuscript databases 
"""
ep = """
Examples:
    omsquery --chapters
        Prints all chapter names in the default manuscript.

    omsquery --chapter <chapter name>
        Prints the chapter information about the named chapter 

    omsquery --manuscriptfile name.yaml --chapters
        Prints all chapter names in the named manuscript.

    omsquery --manuscriptfile name.yaml --current
        Prints the chapter information about the current chapter (state) 

"""
parser = argparse.ArgumentParser(
                    formatter_class=argparse.RawDescriptionHelpFormatter,
                    description=textwrap.dedent(desc),
                    epilog=textwrap.dedent(ep)
                    )

parser.add_argument( "--manuscriptfile", default=None,
    help="Read manuscript definition from this file.")
parser.add_argument( "--chapters", action="store_true", default=None,
    help="Chapter name for this operation")
parser.add_argument( "--chapter", default=None,
    help="Chapter name for this operation")
parser.add_argument( "--current", action="store_true", default=None,
    help="Find the current chapter for this operation")

args, remaining_argv = parser.parse_known_args()


#
# execute
#

# load defaults
defaults = {
    "settingsfile": "./.oms/query.yaml"
}
if os.path.isfile(defaults["settingsfile"]):
    with open( defaults["settingsfile"] ) as minputs:
        defaults = yaml.load( minputs, Loader=yaml.FullLoader )

if args.manuscriptfile == None:
    if "manuscriptfile" in defaults:
        args.manuscriptfile = defaults["manuscriptfile"] 
    else:
        print("ERROR: must provide manuscriptfile")
        exit(0)

data = None
with open( args.manuscriptfile ) as infile:
    if args.manuscriptfile.endswith( "json" ):
        data = json.load( infile )
    elif args.manuscriptfile.endswith( "yaml" ):
        data = yaml.load( infile, Loader=yaml.FullLoader ) 

data = data["manuscript"]

if args.chapters != None:
    for chapter in data["chapters"]:
        print("Chapter: {}".format(chapter["title"]))

elif args.chapter != None:
    for chapter in data["chapters"]:
        if args.chapter == chapter["title"]:
            print("Chapter: {}".format(chapter["title"]))
            print("         {}".format(chapter["scenes"]))
            print("")
            if "desc" in chapter:
                print(chapter["desc"])

elif args.current != None:
    found_current = False
    for chapter in data["chapters"]:
        if "state" in chapter:
            if chapter["state"] == "current":
                found_current = True
                print("Chapter: {}".format(chapter["title"]))
                print("         {}".format(chapter["scenes"]))
                print("")
                if "desc" in chapter:
                    print(chapter["desc"])

    if not found_current:
        print("No current chapter found")



