import click
from .nec_beamer import Nec_Beamer

@click.command()
@click.option("--ip_address", default=IP_ADDRESS, help="IP Address of the Beamer")
@click.option("--name", default=NAME, help="Name of the Beamer")
@click.option("--json/--no-json", default=False, help="output as JSON")
def turn_on(ip_address, name, json):
    """Turn on the Beamer"""
    nec = Nec_Beamer(name=name, ip_address=ip_address)
    nec.turn_on()
    nec.update()
    nec.print_status(json)


@click.command()
@click.option("--ip_address", default=IP_ADDRESS, help="IP Address of the Beamer")
@click.option("--name", default=NAME, help="Name of the Beamer")
@click.option("--json/--no-json", default=False, help="output as JSON")
def turn_off(ip_address, name, json):
    """Turn off the Beamer"""
    nec = Nec_Beamer(name=name, ip_address=ip_address)
    nec.turn_off()
    nec.update()
    nec.print_status(json)


@click.command()
@click.option("--ip_address", default=IP_ADDRESS, help="IP Address of the Beamer")
@click.option("--name", default=NAME, help="Name of the Beamer")
@click.option("--json/--no-json", default=False, help="output as JSON")
def rgb1(ip_address, name, json):
    """Select input rgb1 (VGA)"""
    nec = Nec_Beamer(name=name, ip_address=ip_address)
    nec.source_rgb1()
    nec.update()
    nec.print_status(json)

@click.command()
@click.option("--ip_address", default=IP_ADDRESS, help="IP Address of the Beamer")
@click.option("--name", default=NAME, help="Name of the Beamer")
@click.option("--json/--no-json", default=False, help="output as JSON")
def rgb2(ip_address, name, json):
    """Select input rgb2 (BNC)"""
    nec = Nec_Beamer(name=name, ip_address=ip_address)
    nec.source_rgb2()
    nec.update()
    nec.print_status(json)

@click.command()
@click.option("--ip_address", default=IP_ADDRESS, help="IP Address of the Beamer")
@click.option("--name", default=NAME, help="Name of the Beamer")
@click.option("--json/--no-json", default=False, help="output as JSON")
def rgb3(ip_address, name, json):
    """Select input rgb3 (DVI)"""
    nec = Nec_Beamer(name=name, ip_address=ip_address)
    nec.source_rgb3()
    nec.update()
    nec.print_status(json)


@click.command()
@click.option("--ip_address", default=IP_ADDRESS, help="IP Address of the Beamer")
@click.option("--name", default=NAME, help="Name of the Beamer")
@click.option("--json/--no-json", default=False, help="output as JSON")
def status(ip_address, name, json):
    """Update the Beamer"""
    nec = Nec_Beamer(name=name, ip_address=ip_address)
    nec.update()
    nec.print_status(json)


@click.command()
@click.option("--ip_address", default=IP_ADDRESS, help="IP Address of the Beamer")
@click.option("--name", default=NAME, help="Name of the Beamer")
@click.option("--json/--no-json", default=False, help="output as JSON")
def composite(ip_address, name, json):
    """Select input comp (Component)"""
    nec = Nec_Beamer(name=name, ip_address=ip_address)
    nec.source_comp()
    nec.update()
    nec.print_status(json)


@click.command()
@click.option("--ip_address", default=IP_ADDRESS, help="IP Address of the Beamer")
@click.option("--name", default=NAME, help="Name of the Beamer")
@click.option("--json/--no-json", default=False, help="output as JSON")
def s_video(ip_address, name, json):
    """Select input svid (S-Video)"""
    nec = Nec_Beamer(name=name, ip_address=ip_address)
    nec.source_svid()
    nec.update()
    nec.print_status(json)


@click.command()
@click.option("--ip_address", default=IP_ADDRESS, help="IP Address of the Beamer")
@click.option("--name", default=NAME, help="Name of the Beamer")
@click.option("--json/--no-json", default=False, help="output as JSON")
def video(ip_address, name, json):
    """Select input video (Video)"""
    nec = Nec_Beamer(name=name, ip_address=ip_address)
    nec.source_vidn()
    nec.update()
    nec.print_status(json)

# add a verbose option to cli group

@click.group()
@click.option("--verbose/--no-verbose", default=False, help="Logging with verbosity")
@click.option("--debug/--no-debug", default=False, help="Logging with debug")
@click.option("--json/--no-json", default=False, help="output as JSON")
@click.pass_context
def cli(ctx, verbose, debug, json):
    """ Control a NEC Beamer via LAN

    This script is used to control a NEC Beamer available via LAN.
    It is tested with a NEC NP2000.

    This script uses the NEC Beamer's web interface to control it.

    Web Interface is Copyright (C) NEC Viewtechnology, Ltd. 2002-2006


    """
    setup_verbose_logging(verbose, debug)

def setup_verbose_logging(verbose=False, debug=False):
    # print logging messages to console
    ch = logging.StreamHandler()
    _logger.addHandler(ch)

    # if verbose, add verbosity to _logger
    if debug:
        _logger.setLevel(logging.DEBUG)
        _logger.debug("Verbose mode activated")
    elif verbose:
        _logger.setLevel(logging.INFO)
        _logger.info("Logging level set to INFO")
    else:
        # deactivate console logging
        # and suppress all messages
        _logger.setLevel(logging.CRITICAL)
        _logger.debug("Verbose mode deactivated")

@click.group()
def legacy_input():
    """ Select legacy input Source """
    pass


@ click.group()
def select_input():
    """ Select input Source """
    pass


cli.add_command(turn_on)
cli.add_command(turn_off)
cli.add_command(status)
cli.add_command(rgb1)
cli.add_command(rgb2)
cli.add_command(rgb3)

# add commands to subgroup
legacy_input.add_command(composite)
legacy_input.add_command(s_video)
legacy_input.add_command(video)

select_input.add_command(rgb1)
select_input.add_command(rgb2)
select_input.add_command(rgb3)


# add subgroup to main group
cli.add_command(legacy_input)
cli.add_command(select_input)

if __name__ == "__main__":
    cli()
