#!/usr/bin/python3
"""Commandline interface to feather"""

import argparse
import asyncio
import textwrap

from feather.config import Config
from feather.app import FeatherApp


def main():
    parser = argparse.ArgumentParser(
        prog="feather",
        description="file-based RSS reader client",
        formatter_class=argparse.RawTextHelpFormatter,
    )
    parser.add_argument(
        "action",
        choices=("sync", "sync-up", "sync-down", "daemon", "regenerate", "clear-data"),
        help=textwrap.dedent("""\
            sync: perform a full synchronization with the server
            sync-up: only synchronize local changes to the server (e.g. articles read locally)
            sync-down: only synchronize remote change from the server (e.g. new articles or articles read from another device)
            daemon: start in daemon mode (will keep performing synchronizations periodically until process is stopped)
            regenerate: regenerate all HTML files from the local data
            clear-data: remove all local data
        """),
    )
    args = parser.parse_args()

    config = Config()
    app = FeatherApp(config)

    if args.action == "sync":
        app.synchronize()
    elif args.action == "sync-up":
        app.synchronize_local_changes()
    elif args.action == "sync-down":
        app.synchronize_remote_changes()
    elif args.action == "daemon":
        try:
            asyncio.run(app.daemon())
        except KeyboardInterrupt:
            pass
    elif args.action == "regenerate":
        app.regenerate_files()
    elif args.action == "clear-data":
        app.clear_data()


if __name__ == "__main__":
    main()
