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

# Copyright (C) 2021 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 json
from re import search

import requests

from xivo_agid import agid


def strip_prefix(number, prefix):
    if number.startswith(prefix):
        return number[len(prefix):]
    return number


def get_request(agi, url, value):
    token = agi.config['configmgt']['token']
    res = requests.get('http://localhost:9100/%s/%s' % (url, value),
                       headers={
                           'Accept': 'application/json',
                           'X-Auth-Token': token
                       }, timeout=1.25)
    return res


def get_meeting_room_by_number(agi, number):
    prefix = "**"
    regexp = "\*{2}\d*"
    configmgt_ws = 'configmgt/api/2.0/meetingrooms/number'

    if not search(regexp, number):
        agi.dp_break('Error number is not in required format: %s' % number)

    number = strip_prefix(number, prefix)
    try:
        response = get_request(agi, configmgt_ws, number)
    except requests.Timeout as e:
        agi.dp_break('Error requesting the webservice (Timeout): %s' % e)
    except Exception as e:
        agi.dp_break('Error requesting the webservice: %s' % e)
    else:
        if response.status_code == 200:
            res = json.loads(response.content)
            meeting_room_name = res['name']
            agi.set_variable('XIVO_MEETING_ROOM', meeting_room_name)
        else:
            agi.dp_break('Error requesting the webservice: %s' % response.text)


def get_meeting_room_by_id(agi, meeting_room_id):
    configmgt_ws = 'configmgt/api/2.0/meetingrooms'
    try:
        response = get_request(agi, configmgt_ws, meeting_room_id)
    except requests.Timeout as e:
        agi.dp_break('Error requesting the webservice (Timeout): %s' % e)
    except Exception as e:
        agi.dp_break('Error requesting the webservice: %s' % e)
    else:
        if response.status_code == 200:
            res = json.loads(response.content)
            meeting_room_name = res['name']
            agi.set_variable('XIVO_MEETING_ROOM', meeting_room_name)
        else:
            agi.dp_break('Error requesting the webservice: %s' % response.text)


def incoming_meetingroom_set_features(agi, cursor, args):
    meeting_room_id = agi.get_variable('XIVO_DST_ROOM_ID')
    if not meeting_room_id:
        meeting_room_number = agi.get_variable('XIVO_DST_ROOM_NUM')
        get_meeting_room_by_number(agi, meeting_room_number)
    else:
        get_meeting_room_by_id(agi, meeting_room_id)


agid.register(incoming_meetingroom_set_features)
