#!/usr/bin/env python

import argparse
import logging
import time

import yaml

from metronetpy.__version__ import __version__
from metronetpy.bridge import MetronetBridge

_LOGGER = logging.getLogger()


def set_logging(debug):
    level = logging.DEBUG
    if debug:
        level = logging.DEBUG

    console = logging.StreamHandler()
    console.setLevel(level)
    formatter = logging.Formatter(
        "%(asctime)s %(name)-12s: %(levelname)-8s %(message)s", "%Y-%m-%d %H:%M:%S"
    )
    console.setFormatter(formatter)

    _LOGGER.addHandler(console)
    _LOGGER.setLevel(logging.DEBUG)


def callback(id, active):
    _LOGGER.info(f"Sensor {id} active: {active}")


def do_login(args):
    _LOGGER.info("LOGIN")
    bridge = MetronetBridge(args.username, args.password)
    if bridge.connect():
        _LOGGER.info("Logged In")
    else:
        _LOGGER.error("Failed to login")
        exit(1)
    return bridge


def do_config(args, bridge):
    sensors = bridge.get_sensors()
    _LOGGER.debug(sensors)

    # Remove the state information
    for sensor in sensors:
        del sensor["active"]

    config = {
        "metronet": {
            "username": "!secret metronet_username",
            "password": "!secret metronet_password",
            "sensors": sensors,
        }
    }

    with open(args.config, "w") as file:
        yaml.dump(config, file, default_flow_style=False, sort_keys=False)
    _LOGGER.info("Configuration file created")


def do_run(args, bridge):
    with open(args.config, "r") as file:
        cfg = yaml.load(file, Loader=yaml.FullLoader)
    bridge.load_config(cfg["metronet"]["sensors"])

    sensors = bridge.get_sensors()
    _LOGGER.info(sensors)

    _LOGGER.info("Regiter callback functions")
    for sensor in sensors:
        bridge.register_callback(sensor["id"], callback)

    _LOGGER.info("Begin main loop")
    bridge.main_loop()

    while True:
        time.sleep(1)

    bridge.stop()


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("command")
    parser.add_argument(
        "--config",
        default="configuration.yaml",
        metavar="FILE",
        help="Path to config file",
    )
    parser.add_argument("--debug", default=False, help="Enable debug")
    parser.add_argument("--username", help="Metronet Username")
    parser.add_argument("--password", help="Metronet Password")
    args = parser.parse_args()

    set_logging(args.debug)

    _login = False
    _config = False
    _run = False
    if args.command == "login":
        _login = True
    elif args.command == "config":
        _login = True
        _config = True
    elif args.command == "run":
        _login = True
        _run = True
    elif args.command == "version":
        _LOGGER.info(f"Metronetpy version {__version__}")
        exit(0)
    else:
        _LOGGER.error("Command args missing or not suppored")
        exit(2)

    _LOGGER.info("Starting")

    if args.username is None or args.password is None:
        _LOGGER.error("Missing username")
        exit(1)
    if args.password is None:
        _LOGGER.error("Missing password")
        exit(2)

    if _login:
        bridge = do_login(args)
    if _config:
        do_config(args, bridge)
    if _run:
        do_run(args, bridge)


if __name__ == "__main__":
    main()
