#!/usr/bin/env python
import os
import optparse
import sys

import tron
from tron import commands
from tron.commands import display
from tron.commands.client import Client


def parse_options():
    parser = optparse.OptionParser(
        "usage: %prog [options] [<job | job run | action>]",
        version="%%prog %s" % tron.__version__)
    parser.add_option("--verbose", "-v", action="count", dest="verbose",
                      help="Verbose logging", default=0)
    parser.add_option("--numshown", "-n", type="int", dest="num_displays",
                      help="Max number of jobs/job-runs shown", default=10)
    parser.add_option("--server", action="store", dest="server",
                      help="Server URL to connect to", default=None)
    parser.add_option("--hide-preface", "-z", action="store_false",
                      dest="display_preface", help="Don't display preface",
                      default=True)
    parser.add_option("--color", "-c", action="store_true",
                      dest="display_color", help="Display in color",
                      default=None)
    parser.add_option("--nocolor", action="store_false",
                      dest="display_color", help="Display without color",
                      default=None)
    parser.add_option("--stdout", "-o", action="count", dest="stdout",
                      help="Solely displays stdout", default=0)
    parser.add_option("--stderr", "-e", action="count", dest="stderr",
                      help="Solely displays stderr", default=0)
    parser.add_option("--warn", "-w", action="count", dest="warn",
                      help="Solely displays warnings and errors", default=0)
    parser.add_option("--events", action="store_true", dest="show_events",
                      help="Show events for the specified entity",
                      default=False)
    parser.add_option("--save", "-s", action="store_true", dest="save_config",
                      help="Save options used on this job for next time.",
                      default=False)

    (options, args) = parser.parse_args(sys.argv)
    return options, args[1:]


def console_height():
    return int(os.popen('stty size', 'r').read().split()[0])

def display_events(data):
    return display.DisplayEvents().format(data)

def view_all(options, client):
    """Retreive jobs and services and display them."""
    if options.show_events:
        return display_events(client.events())

    return "".join([
        display.DisplayServices().format(client.services()),
        '\n',
        display.DisplayJobs(options).format(client.jobs())
    ])


def view_job(options, job_name, client):
    """Retrieve details of the specified job and display"""
    if options.show_events:
        return display_events(client.job_events(job_name))

    job_content = client.job(job_name)
    return display.DisplayJobs(options).format_job(job_content)


def view_job_run(options, job_run_id, client):
    if options.show_events:
        return display_events(client.action_events(job_run_id))

    actions = client.job_runs(job_run_id)
    display_action = display.DisplayActions(options)
    return display_action.format(actions)


def view_action_run(options, act_run_id, client):
    display_action = display.DisplayActions(options)
    return display_action.format_action_run(client.action(act_run_id))


def view_service(options, service_name, client):
    """Retrieve details of the specified service and display"""
    if options.show_events:
        return display_events(client.service_events(service_name))

    service_content = client.service(service_name)
    return display.DisplayServices().format_details(service_content)

def get_view_output(name, options):
    client = Client(options)
    content = client.index()

    level = name.count('.')
    object_name = name.split('.')[0]

    if object_name in content['jobs']:
        if not level:
            return view_job(options, name, client)
        elif level == 1:
            return view_job_run(options, name, client)
        return view_action_run(options, name, client)

    if object_name in content['services']:
        if level:
            return
        return view_service(options, name, client)


def main():
    """run tronview"""
    options, args = parse_options()
    commands.setup_logging(options)
    commands.load_config(options)

    display.Color.enabled = options.display_color
    client = Client(options)
    client.status()

    if not args:
        output = view_all(options, client)
    else:
        output = get_view_output(args[0], options)

    if not output:
        print >>sys.stderr, "What is a %s?" % args[0]
        sys.exit(1)

    if len(output.split('\n')) > console_height() and sys.stdout.isatty():
        display.view_with_less(output, options.display_color)
    else:
        print output

    if options.save_config:
        commands.save_config(options)

if __name__ == '__main__':
    try:
        main()
    except Exception, e:
        print >>sys.stderr, e
