# -*- coding: utf-8 -*-

# Copyright (C) 2015 Avencall
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>

import logging

logger = logging.getLogger(__name__)

_URL_META_FORMAT = u'{{scheme}}://{{hostname}}:{{port}}/0.1/directories/{entry_point}/{{profile}}/{vendor}?{qs_prefix}xivo_user_uuid={{user_uuid}}{qs_suffix}'


def add_xivo_phonebook_url(raw_config, vendor, entry_point=u'input', qs_prefix=u'', qs_suffix=u''):
    url_format = _build_url_format(vendor, entry_point, qs_prefix, qs_suffix)
    add_xivo_phonebook_url_from_format(raw_config, url_format)


def _build_url_format(vendor, entry_point, qs_prefix, qs_suffix):
    if qs_prefix:
        qs_prefix = qs_prefix + u'&'
    if qs_suffix:
        qs_suffix = u'&' + qs_suffix
    return _URL_META_FORMAT.format(vendor=vendor,
                                   entry_point=entry_point,
                                   qs_prefix=qs_prefix,
                                   qs_suffix=qs_suffix)


def add_xivo_phonebook_url_from_format(raw_config, url_format):
    hostname = raw_config.get(u'X_xivo_phonebook_ip')
    if not hostname:
        return

    profile = raw_config.get(u'X_xivo_phonebook_profile')
    if not profile:
        logger.warning('Not adding XX_xivo_phonebook_url: no phonebook profile')
        return

    user_uuid = raw_config.get(u'X_xivo_user_uuid')
    if not user_uuid:
        logger.warning('Not adding XX_xivo_phonebook_url: no user uuid')
        return

    scheme = raw_config.get(u'X_xivo_phonebook_scheme', u'http')
    port = raw_config.get(u'X_xivo_phonebook_port', 9498)
    raw_config[u'XX_xivo_phonebook_url'] = url_format.format(scheme=scheme,
                                                             hostname=hostname,
                                                             port=port,
                                                             profile=profile,
                                                             user_uuid=user_uuid)
