"""
automua™ is a trademark of "Gaspard d'Hautefeuille" and may not be used 
by third parties without the prior written permission of the author.

Copyright © 2019-2022 Ralph Seichter

This file is part of automua.

automua is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

automua is distributed 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with automua. If not, see <https://www.gnu.org/licenses/>.
"""
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import SubElement

from automua import DomainNotFound
from automua import InvalidServerType
from automua import NoProviderForDomain
from automua.generators import ConfigGenerator
from automua.generators import branded_id
from automua.generators import xml_to_string
from automua.ldap import LookupResult
from automua.ldap import STATUS_SUCCESS
from automua.model import Domain
from automua.model import Provider
from automua.model import Server

SERVER_TYPE_MAP = {
    'imap': ['incoming', 'imap'],
    'pop': ['incoming', 'pop3'],
    'smtp': ['outgoing', 'smtp'],
}


class MozillaGenerator(ConfigGenerator):
    def server_element(self, parent: Element, server: Server, override_uid: str = None) -> None:
        direction = SERVER_TYPE_MAP[server.type][0]
        type_attrib = SERVER_TYPE_MAP[server.type][1]
        element = SubElement(parent, f'{direction}Server', attrib={'type': type_attrib})
        SubElement(element, 'hostname').text = server.name
        SubElement(element, 'port').text = str(server.port)
        SubElement(element, 'socketType').text = server.socket_type
        SubElement(element, 'username').text = self.pick_one(server.user_name, override_uid)
        SubElement(element, 'authentication').text = server.authentication

    def client_config(self, local_part, domain_part: str, display_name: str) -> str:
        root_element = Element('clientConfig', attrib={'version': '1.1'})
        domain: Domain = Domain.query.filter_by(name=domain_part).first()
        if not domain:
            raise DomainNotFound(f'Domain "{domain_part}" not found')
        if domain.ldapserver:
            lookup_result: LookupResult = self.ldap_lookup(f'{local_part}@{domain_part}', domain.ldapserver)
        else:
            lookup_result = LookupResult(STATUS_SUCCESS, display_name, None)
        provider: Provider = domain.provider
        if not provider:  # pragma: no cover (db constraints prevent this during testing)
            raise NoProviderForDomain(f'No provider for domain "{domain_part}"')
        provider_element = SubElement(root_element, 'emailProvider', attrib={'id': branded_id(provider.id)})
        SubElement(provider_element, 'identity')  # Deliberately left empty
        for provider_domain in provider.domains:
            SubElement(provider_element, 'domain').text = provider_domain.name
        SubElement(provider_element, 'displayName').text = provider.name
        SubElement(provider_element, 'displayShortName').text = provider.short_name
        for server in self.servers_by_prio(domain.servers):
            if server.type not in SERVER_TYPE_MAP:
                raise InvalidServerType(f'Invalid server type "{server.type}"')
            self.server_element(provider_element, server, lookup_result.uid)
        return xml_to_string(root_element)
