"""List file storage snapshots."""
# :license: MIT, see LICENSE for more details.

import click
import SoftLayer
from SoftLayer.CLI import columns as column_helper
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer.CLI import helpers

COLUMNS = [
    column_helper.Column('id', ('id',), mask='id'),
    column_helper.Column('name', ('notes',), mask='notes'),
    column_helper.Column('created', ('snapshotCreationTimestamp',),
                         mask='snapshotCreationTimestamp'),
    column_helper.Column('size_bytes', ('snapshotSizeBytes',),
                         mask='snapshotSizeBytes'),
]

DEFAULT_COLUMNS = [
    'id',
    'name',
    'created',
    'size_bytes'
]


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('volume_id')
@click.option('--sortby', help='Column to sort by',
              default='created')
@click.option('--columns',
              callback=column_helper.get_formatter(COLUMNS),
              help=f"Columns to display. Options: {', '.join(column.name for column in COLUMNS)}",
              default=','.join(DEFAULT_COLUMNS))
@environment.pass_env
def cli(env, volume_id, sortby, columns):
    """List file storage snapshots.

    Example::
    slcli file snapshot-list 12345678 --sortby id
    This command lists all snapshots of volume with ID 12345678 and sorts them by ID.
"""
    file_manager = SoftLayer.FileStorageManager(env.client)
    resolved_id = helpers.resolve_id(file_manager.resolve_ids, volume_id, 'Volume Id')
    snapshots = file_manager.get_file_volume_snapshot_list(
        resolved_id,
        mask=columns.mask()
    )

    table = formatting.Table(columns.columns)
    table.sortby = sortby

    for snapshot in snapshots:
        table.add_row([value or formatting.blank()
                       for value in columns.row(snapshot)])

    env.fout(table)
