import argparse
import textwrap

from .base import RegisteredCommandBase, RegisteredSubCommandBase
from ..parsers import object_id_parser, superevent_id_parser, graceid_parser


###############################################################################
# Base command
###############################################################################
registry = []
class UpdateCommand(RegisteredCommandBase):
    name = "update"
    description = textwrap.dedent("""\
        Update an event with a new event file, update a superevent's
        parameters, or update an existing event or superevent signoff
    """)
    subcommands = registry


###############################################################################
# Subcommands - registered to base command automatically
###############################################################################
class UpdateChildBase(RegisteredSubCommandBase):
    _registry = registry


class UpdateEventCommand(UpdateChildBase):
    name = "event"
    description = "Update an event by uploading a new event data file"
    parent_parsers = (graceid_parser,)

    def add_custom_arguments(self, parser):
        parser.add_argument("filename", type=str,
            help="Path to new event file")
        return parser

    def run(self, client, args):
        return client.replaceEvent(args.graceid, args.filename)


class UpdateSignoffCommand(UpdateChildBase):
    name = "signoff"
    description = textwrap.dedent("""\
        Update a superevent signoff with a new status and/or comment.
        Event signoff is not presently implemented.
    """)
        
    parent_parsers = (superevent_id_parser,)

    def add_custom_arguments(self, parser):
        parser.add_argument("signoff_type", type=str,
            help=("Signoff type (do '{prog} show signoff_types') to see "
            "options").format(prog=self.base_prog))
        parser.add_argument("instrument", type=str, nargs='?',
            help=("Instrument code (do {prog} show instruments to see "
            "options). Required for operator signoffs.").format(
            prog=self.base_prog))
        parser.add_argument("--status", type=str,
            help=("Signoff status (do '{prog} show signoff_statuses') to see "
            "options").format(prog=self.base_prog))
        parser.add_argument("--comment", type=str,
            help="Comment to update the signoff with")
        return parser

    def run(self, client, args):
        instrument = args.instrument or '' # Convert None to ''
        return client.update_signoff(args.superevent_id, args.signoff_type,
            status=args.status, comment=args.comment,
            instrument=instrument)


class UpdateSupereventCommand(UpdateChildBase):
    name = "superevent"
    description = "Update a superevent's parameters"
    parent_parsers = (superevent_id_parser,)

    def add_custom_arguments(self, parser):
        parser.add_argument('--t-start', type=float,
            help="New t_start value for superevent")
        parser.add_argument('--t-0', type=float,
            help="New t_0 value for superevent")
        parser.add_argument('--t-end', type=float,
            help="New t_end value for superevent")
        parser.add_argument('--preferred-event', type=str,
            help="Graceid of new preferred event for superevent")
        return parser

    def run(self, client, args):
        return client.updateSuperevent(args.superevent_id, t_0=args.t_0,
            t_start=args.t_start, t_end=args.t_end,
            preferred_event=args.preferred_event)
