#!/home/thomasl/tools/matrix-cli/matrix-cli/venv/bin/python
import sys
import yaml
import logging
import argparse

from matrix_cli import MatrixAdmin, MatrixStreamLogger

class Config:

    def __init__(self):
        with open('config.yaml', 'r') as config_file:
            config = yaml.safe_load(config_file)
            self.USER = config['user']
            self.LOG_ROOM = config['logRoomId']

CONFIG = Config()

log_formatter = logging.Formatter("%(asctime)s %(message)s")
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)

file_handler = logging.FileHandler('admin_audit.log')
file_handler.setFormatter(log_formatter)
root_logger.addHandler(file_handler)

console_handler = logging.StreamHandler()
console_handler.setFormatter(log_formatter)
root_logger.addHandler(console_handler)

PARK_ALIASES = 'park-aliases'
SHUTDOWN_ROOMS = 'shutdown-rooms'
DEACTIVATE_USERS = 'deactivate-users'
DELETE_ALIASES = 'delete-aliases'
DELIST_ROOMS = 'delist-rooms'
ASSIGN_ALIAS = 'assign-alias'
RESOLVE_ALIASES = 'resolve-aliases'
TEST = 'test'

MUTATING = [PARK_ALIASES,
            SHUTDOWN_ROOMS,
            DEACTIVATE_USERS,
            DELETE_ALIASES,
            DELIST_ROOMS,
            ASSIGN_ALIAS,
            TEST]
NON_MUTATING = [RESOLVE_ALIASES]
COMMANDS = MUTATING + NON_MUTATING

parser = argparse.ArgumentParser(description='Matrix Command Line Admin Tool. Wrapper for the Synapse Admin HTTP APIs. Logs all actions to a local file and a Matrix room.')
parser.add_argument('command', help='One of: %s' % ', '.join(COMMANDS), nargs='+')
parser.add_argument('--reason', help='The reason for the administrative action that\'s been taken; recorded in the log.', nargs='+')
parser.add_argument('--message', help='Optional parameter. When deactivating a room you can specify the message written to the new room. Defaults to "This room has been removed from the matrix.org homeserverr due to violating the terms of use.".',nargs='+')

args = parser.parse_args()
command = args.command[0]
command_args = args.command[1:]

if command not in COMMANDS:
    parser.error('%s is not a recognised command\nPlease choose from: %s' % (command, ', '.join(COMMANDS)))

if command in MUTATING:
    if args.reason:
        reason = ' '.join(args.reason)
    else:
        parser.error('Mutating commands require a --reason')

if command == 'assign-alias' and len(command_args) != 2:
    parser.error('assign-alias requires two arguments: an alias and a room id (the order isn\'t important)')

message = ' '.join(args.message) if args.message else None

admin = MatrixAdmin(
    CONFIG.USER['mxid'],
    CONFIG.USER['password']
)
log_room = CONFIG.LOG_ROOM

matrix_handler = MatrixStreamLogger(admin._client, log_room)
matrix_handler.setFormatter(log_formatter)
root_logger.addHandler(matrix_handler)

if command == RESOLVE_ALIASES:
    for alias in command_args:
        if sys.stdout.isatty():
            print('%s -> %s' % (alias, admin.resolve_alias(alias)))
        else:
            print(admin.resolvee_alias(alias))
elif command == ASSIGN_ALIAS:
    if command_args[0].startswith('#') and command_args[1].startswith('!'):
        admin.assign_alias(command_args[1], command_args[0], reason)
    else:
        admin.assign_alias(command_args[0], command_args[1], reason)
elif command == DELIST_ROOMS:
    for room in command_args:
        admin.delist_room(room, reason)
elif command == DELETE_ALIASES:
    for alias in command_args:
        admin.delete_alias(alias, reason)
elif command == PARK_ALIASES:
    for alias in command_args:
        admin.park_alias(alias, reason)
elif command == SHUTDOWN_ROOMS:
    for room in command_args:
        admin.shutdown_room_and_quarantine_media(room,
                                                 reason,
                                                 message=message)
elif command == TEST:
    for value in command_args:
        print('Testing: ' + value)
    print(reason)
elif command == DEACTIVATE_USERS:
    for user in command_args:
        admin.deactivate_user(user, reason)
