import click
from classer.classifiers import *
from classer.history import History


@click.group()
def cli():
    '''Organize a directory by classifying files into different places.'''

    pass


@cli.command()
@click.argument('exprs', nargs=-1)
@click.argument('src', type=click.Path(exists=True))
@click.argument('dst', type=click.Path())
@click.option('--autoclean', '-c', is_flag=True,
              help='Automatically remove empty directories.')
@click.option('--recursive/--no-recursive', '-r/-R', default=True,
              help='Recursively/No-recursively scan directories.')
@click.option('--since',
              help='Oldest modification time.')
@click.option('--until',
              help='Latest modification time.')
@click.option('--larger', type=click.INT,
              help='Minimum size in bytes.')
@click.option('--smaller', type=click.INT,
              help='Maximum size in bytes.')
@click.option('--exclude', '-x', multiple=True,
              help='Glob pattern to exclude directories.')
@click.option('--ask', 'duplicate', flag_value='ask', default=True,
              help='Ask for action on duplicate.')
@click.option('--rename', 'duplicate', flag_value='rename',
              help='Always rename on duplicate.')
@click.option('--overwrite', 'duplicate', flag_value='overwrite',
              help='Always overwrite on duplicate.')
@click.option('--ignore', 'duplicate', flag_value='ignore',
              help='Always ignore on duplicate.')
def manuel(exprs, src, dst, **options):
    '''Manually classify files.'''

    history = History()

    with open(history.new(), 'w') as f:
        worker = Classifier(exprs, src, dst, f, **options)
        worker.classify()


@cli.command()
@click.argument('path', type=click.Path(exists=True))
def auto(path):
    '''Automatically classify files based on a criteria file.'''

    history = History()

    with open(history.new(), 'w') as f:
        worker = AutoClassifier(path, f)
        worker.classify()


@cli.command()
@click.option('--autoclean', '-c', is_flag=True,
              help='Automatically remove empty directories.')
@click.option('--ask', 'duplicate', flag_value='ask', default=True,
              help='Ask for action on duplicate.')
@click.option('--rename', 'duplicate', flag_value='rename',
              help='Always rename on duplicate.')
@click.option('--overwrite', 'duplicate', flag_value='overwrite',
              help='Always overwrite on duplicate.')
@click.option('--ignore', 'duplicate', flag_value='ignore',
              help='Always ignore on duplicate.')
def undo(**options):
    '''Undo the last run of classer.'''

    history = History()
    history.update()

    try:
        with open(history.get_latest()) as f:
            worker = ReverseClassifier(f, **options)
            worker.classify()
    except FileNotFoundError:
        print('There is no history')


@cli.command()
@click.option('--n', default=0)
@click.option('--remove', default=1)
@click.option('--clear', '-c', is_flag=True)
def histoire(**options):
    history = History()
    history.update()

    if options.get('remove'):
        history.remove(options['remove'])
    elif options.get('clear'):
        history.clear()
    else:
        history.print(options.get('n'))

@@ -1987,32 +1987,63 @@
 %0A@cli.command()%0A
+@click.otion('--n', default=1)%0A
 @click.option('-
@@ -2698,25 +2698,99 @@
 ry.update()%0A
-%0A
+    entries = list(history.get())%0A%0A    for i in range(options.pop(n)):%0A    
     try:%0A   
@@ -2786,16 +2786,20 @@
    try:%0A
+    
         
@@ -2812,36 +2812,31 @@
 pen(
-history.get_latest()
+entries%5B-i - 1%5D
 ) as f:%0A
     
@@ -2823,32 +2823,36 @@
 %5B-i - 1%5D) as f:%0A
+    
             work
@@ -2892,32 +2892,36 @@
 ns)%0A            
+    
 worker.classify(
@@ -2918,24 +2918,28 @@
 .classify()%0A
+    
     except F
@@ -2941,20 +2941,13 @@
 ept 
-FileNotFound
+Index
 Erro
@@ -2961,36 +2961,18 @@
     
-print('There is no history')
+    return
 %0A%0A%0A@

Add option to undo many times