"""
The personal file management tool.

Usage:
    organize simulate
    organize run
    organize undo
    organize list
    organize config
    organize --help
    organize --version

Arguments:
    run             Applies the actions. No simulation
    undo            Undo the last organization run
    list            List available actions and filters
    config          Open configuration in %{EDITOR}

Options:
    --version       Show program version and exit.
    -h, --help      Show this screen and exit.
"""
import logging
from pathlib import Path

from organize import Rule
from organize import filters
from organize import actions

import appdirs
from docopt import docopt

__version__ = '0.0'
logging.basicConfig(level=logging.INFO)

# config folder
app_dirs = appdirs.AppDirs('organize')
config_dir = Path(app_dirs.user_config_dir)
config_dir.mkdir(parents=True, exist_ok=True)


def iter_actions(folders, rules: [Rule]):
    for folder in folders:
        for path in Path(folder).expanduser().glob('*.*'):
            for rule in rules:
                if rule.filter.matches(path):
                    yield (rule, path)
                    break


def main(folders, simulate, rules: [Rule]):
    for rule, path in iter_actions(folders=folders, rules=rules):
        file_attributes = rule.filter.parse(path)
        rule.action.run(
            path=path,
            file_attributes=file_attributes,
            simulate=simulate)


if __name__ == '__main__':
    args = docopt(__doc__, version=__version__, help=True)

    if args['list']:
        print('Available filters:')
        print(' - TODO')
        print('')
        print('Available actions:')
        print(' - TODO')
        print('')

    elif args['config']:
        print('Opening your config folder...')
        print(app_dirs.user_config_dir)
        import webbrowser
        webbrowser.open(config_dir.as_uri())

    elif args['undo']:
        print('Do you want to undo the last action? N / y / (s)how')

    else:
        main(
            simulate=args['simulate'],
            folders=['~/Desktop/__Inbox__'],
            rules=[
                Rule(
                    filter=filters.PaperVDI(),
                    action=actions.Move('~/Documents/VDI Nachrichten/VDI {year}-{month:02}-{day:02}.pdf')),
                Rule(
                    filter=filters.Invoice1and1(),
                    action=actions.Move('~/TF Cloud/Office/Rechnungen/1und1 {year}-{month:02}-{day:02}.pdf'))
            ])

@@ -1549,20 +1549,22 @@
 f args%5B'
-list
+config
 '%5D:%0A    
@@ -1578,71 +1578,174 @@
 nt('
-Available filters:')%0A        print(' - TODO')%0A        print('')
+Opening your config folder...')%0A        print(app_dirs.user_config_dir)%0A        import webbrowser%0A        webbrowser.open(config_dir.as_uri())%0A%0A    elif args%5B'list'%5D:
 %0A   
@@ -1766,22 +1766,22 @@
 ailable 
-action
+filter
 s:')%0A   
@@ -1824,190 +1824,85 @@
 '')%0A
-%0A
  
-   elif args%5B'config'%5D:%0A
        
- 
 print('
-Opening your config folder...')%0A        print(app_dirs.user_config_dir)%0A        import webbrowser%0A        webbrowser.open(config_dir.as_uri()
+Available actions:')%0A        print(' - TODO')%0A        print(''
 )%0A%0A 

handle config cmd first