import os
import webbrowser

import click
import flask

from slackviewer.app import app
from slackviewer.archive import \
    extract_archive, \
    get_users, \
    get_channels, \
    compile_channels


def envvar(name, default):
    """Create callable environment variable getter

    :param str name: Name of environment variable
    :param default: Default value to return in case it isn't defined
    """
    return lambda: os.environ.get(name, default)


def flag_ennvar(name):
    return os.environ.get(name) == '1'


def configure_app(archive='ghfr.zip', debug=False):
    app.debug = debug
    if app.debug:
        print("WARNING: DEBUG MODE IS ENABLED!")
    app.config["PROPAGATE_EXCEPTIONS"] = True

    path = extract_archive(archive)
    user_data = get_users(path)
    channel_data = get_channels(path)
    channels = compile_channels(path, user_data, channel_data)

    top = flask._app_ctx_stack
    top.channels = channels

    return app


# @click.command()
# @click.option('-p', '--port', default=envvar('SEV_PORT', '5000'),
#               type=click.INT, help="Host port to serve your content on")
# @click.option("-z", "--archive", type=click.Path(), required=True,
#               default=envvar('SEV_ARCHIVE', 'ghfr.zip'),
#               help="Path to your Slack export archive (.zip file or directory)")
# @click.option('-I', '--ip', default=envvar('SEV_IP', 'localhost'),
#               type=click.STRING, help="Host IP to serve your content on")
# @click.option('--no-browser', is_flag=True,
#               default=flag_ennvar("SEV_NO_BROWSER"),
#               help="If you do not want a browser to open "
#                    "automatically, set this.")
# @click.option('--debug', is_flag=True, default=flag_ennvar("FLASK_DEBUG"))
# def main(*args):
#     print(f'args are {args}')
#     # if not archive:
#     #     raise ValueError("Empty path provided for archive")

#     configure_app(app, 'ghfr.zip', False)

#     # if not no_browser:
#     #     webbrowser.open("http://{}:{}".format(ip, port))

#     # app.run(
#     #     host=ip,
#     #     port=port
#     # )

#     return app
