# -*- 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
import unittest
from mock import Mock, patch, call
from xivo_agid.modules import incoming_meetingroom_set_features


class TestSetDialplanVars(unittest.TestCase):

    def setUp(self):
        self.queue = Mock()
        self.cursor = Mock()
        self.queue.name = "meeting_room_1"
        self._client = Mock().return_value

    @patch('requests.get')
    def test_get_meeting_room_name(self, mock_get):
        agi = Mock()
        agi.config = {'confd': {'client': self._client},
                           'configmgt': {'token': 'abcdefgh', 'host': 'localhost', 'port': 9100}}

        agi.get_variable.return_value = 1
        mockresponse = Mock()
        mock_get.return_value = mockresponse
        mockresponse.status_code = 200
        mockresponse.content = json.dumps({
            'name': 'meeting_room_1'})

        expected_calls = [
            call('XIVO_MEETING_ROOM', 'meeting_room_1'),
        ]

        incoming_meetingroom_set_features.incoming_meetingroom_set_features(agi, self.cursor, None)

        mock_get.assert_called_once_with('http://localhost:9100/configmgt/api/2.0/meetingrooms/1',
                                         headers={'Accept': 'application/json', 'X-Auth-Token': 'abcdefgh'},
                                         timeout=1.25)

        self.assertEqual(agi.set_variable.call_args_list, expected_calls)

    @patch('requests.get')
    def test_get_meeting_room_by_number(self, mock_get):
        def get_variable(value):
            if value == "XIVO_DST_ROOM_ID":
                return ''
            if value == "XIVO_DST_ROOM_NUM":
                return "**1050"

        agi = Mock()
        agi.get_variable = Mock(side_effect=get_variable)

        agi.config = {'confd': {'client': self._client},
                      'configmgt': {'token': 'abcdefgh', 'host': 'localhost', 'port': 9100}}

        mockresponse = Mock()
        mock_get.return_value = mockresponse
        mockresponse.status_code = 200
        mockresponse.content = json.dumps({
            'name': 'meeting_room_1'})

        expected_calls = [
            call('XIVO_MEETING_ROOM', 'meeting_room_1'),
        ]

        incoming_meetingroom_set_features.incoming_meetingroom_set_features(agi, self.cursor, None)

        mock_get.assert_called_once_with('http://localhost:9100/configmgt/api/2.0/meetingrooms/number/1050',
                                         headers={'Accept': 'application/json', 'X-Auth-Token': 'abcdefgh'},
                                         timeout=1.25)

        agi.set_variable.assert_has_calls(expected_calls)
