"""
    lager.flash.commands

    Commands for flashing a DUT
"""
import itertools
import click
from ..context import get_default_gateway
from ..util import stream_output
from ..paramtypes import BinfileType

def do_flash(session, gateway, hexfile, binfile, preverify, verify, force=False):
    """
        Perform the actual flash operation
    """
    files = list(zip(itertools.repeat('hexfile'), [open(path, 'rb') for path in hexfile]))
    files.extend(
        zip(itertools.repeat('binfile'), [open(binf.path, 'rb') for binf in binfile])
    )
    files.extend(
        zip(itertools.repeat('binfile_address'), [binf.address for binf in binfile])
    )
    files.append(('preverify', preverify))
    files.append(('verify', verify))
    files.append(('force', force))

    return session.flash_dut(gateway, files=files)


@click.command()
@click.pass_context
@click.option('--gateway', required=False, help='ID of gateway to which DUT is connected')
@click.option(
    '--hexfile',
    multiple=True, type=click.Path(exists=True),
    help='Hexfile(s) to flash. May be passed multiple times; files will be flashed in order.')
@click.option(
    '--binfile',
    multiple=True, type=BinfileType(exists=True),
    help='Binfile(s) to flash. Syntax: --binfile `<filename>,<address>` '
         'May be passed multiple times; files will be flashed in order.')
@click.option(
    '--preverify/--no-preverify',
    help='If true, only flash target if image differs from current flash contents',
    default=True, show_default=True)
@click.option('--verify/--no-verify', help='Verify image successfully flashed', default=True, show_default=True)
@click.option('--force', is_flag=True)
def flash(ctx, gateway, hexfile, binfile, preverify, verify, force):
    """
        Flash a DUT connected to a gateway with 1 or more bin or hex files
    """
    if gateway is None:
        gateway = get_default_gateway(ctx)

    session = ctx.obj.session

    resp = do_flash(session, gateway, hexfile, binfile, preverify, verify, force)
    stream_output(resp)
