#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
# Univention Office 365 - new Azure AD connection script
#
# Copyright 2017-2022 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.

from __future__ import print_function

import sys
import argparse
import logging

from univention.office365.connector.account_connector import AccountConnector
from univention.office365.logging2udebug import get_logger

conf_path = '/etc/univention-office365'


def list(args):
	adconnection_list = account_connector.get_adconnections()
	if adconnection_list:
		print("{0:<25} {1:<50} {2:<20}".format("Alias", "Path", "status"))
		for alias, status, path in adconnection_list:
			print("{0:<25} {1:<50} {2:<20}".format(alias, path, status))


def create(args):
	account_connector.create_new_adconnection(logger, args.alias, args.makedefault)


def rename(args):
	#AzureADConnectionHandler.rename_adconnection(args.oldalias, args.newalias)
	print("Not implemented")


def remove(args):
	account_connector.remove_adconnection(args.alias)


if __name__ == '__main__':
	logger = get_logger("office365", "o365")
	account_connector = AccountConnector(logger=logger)
	_handler = logging.StreamHandler(sys.stdout)
	_handler.setLevel(logging.INFO)
	logger.addHandler(_handler)

	parser = argparse.ArgumentParser(
		description='Manage Azure AD connection configuration for the Office 365 connector.',
	)
	subparsers = parser.add_subparsers()

	list_p = subparsers.add_parser('list')
	list_p.set_defaults(func=list)

	create_p = subparsers.add_parser('create')
	create_p.add_argument('alias', help='The alias name for the new adconnection, refer e.g. to the domainname or schoolname')
	create_p.add_argument('--makedefault', help='Configure the new adconnection as a default for the listener modules, also see UCR office365/defaultalias', action='store_true')
	create_p.set_defaults(func=create)

	remove_p = subparsers.add_parser('remove')
	remove_p.add_argument('alias', help='The alias of the Azure AD connection to be removed')
	remove_p.set_defaults(func=remove)

	rename_p = subparsers.add_parser('rename')
	rename_p.add_argument('oldalias', help='The alias of the Azure AD connection to be renamed')
	rename_p.add_argument('newalias', help='The new name for the alias')
	rename_p.set_defaults(func=rename)

	args = parser.parse_args()
	args.func(args)
