# -*- coding: utf-8 -*-
# Copyright (C) 2015-2016 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/>

from hamcrest import assert_that
from hamcrest import equal_to
from hamcrest import has_item
from hamcrest import has_property
from mock import Mock
from unittest import TestCase
from uuid import uuid4

from xivo_dird import database

from ..xivo_meetingroom_backend import MeetingroomBackend

import logging
logger = logging.getLogger(__name__)

SOME_UUID = str(uuid4())

ROOM_1 = {
    'id': 1,
    'name': 'room 1',
    'display_name': 'display 1',
    'number': 'aaaa',
    'user-pin': '111',
    'uuid': '11111111-1111-1111-1111-111111111111',
    'room_type': 'static',
    'user_id': 11
}
ROOM_2 = {
    'id': 2,
    'name': 'room 2',
    'display_name': 'display 2',
    'number': 'bbbb',
    'user-pin': '2',
    'uuid': '22222222-2222-2222-2222-222222222222',
    'room_type': 'personal',
    'user_id': 22
}


class TestMeetingroomBackend(TestCase):

    def setUp(self):
        self._source = MeetingroomBackend()
        self._search_engine = Mock(database.MeetingroomSearchEngine)
        self._source.load({'config': {'name': 'meetingroom'}}, search_engine=self._search_engine)

    def test_search_name_static(self):
        self._search_engine.find.return_value = [ROOM_1.copy()]

        result = self._source.search('room', {'xivo_user_uuid': ROOM_1['user_id']})

        self._search_engine.find.assert_called_once_with(term='room', user_uuid=ROOM_1['user_id'])

        expected = ROOM_1.copy()
        expected.pop('room_type')
        assert_that(result, has_item(has_property('fields', expected)))
        assert_that(result, has_item(has_property('is_personal', False)))
        assert_that(result, has_item(has_property('is_deletable', False)))

    def test_search_name_personal(self):
        self._search_engine.find.return_value = [ROOM_2.copy()]

        result = self._source.search('room', {'xivo_user_uuid': ROOM_2['user_id']})

        self._search_engine.find.assert_called_once_with(term='room', user_uuid=ROOM_2['user_id'])

        expected = ROOM_2.copy()
        expected.pop('room_type')
        assert_that(result, has_item(has_property('fields', expected)))
        assert_that(result, has_item(has_property('is_personal', True)))
        assert_that(result, has_item(has_property('is_deletable', False)))

    def test_first_match(self):
        self._search_engine.find_first.return_value = [ROOM_2.copy()]

        result = self._source.first_match('bbbb', {'xivo_user_uuid': ROOM_2['user_id']})

        self._search_engine.find_first.assert_called_once_with(term='bbbb', user_uuid=ROOM_2['user_id'], limit=1)

        expected = ROOM_2.copy()
        expected.pop('room_type')
        assert_that(result, has_property('fields', expected))
        assert_that(result, has_property('is_personal', True))
        assert_that(result, has_property('is_deletable', False))

    def test_list(self):
        self._search_engine.find.return_value = [ROOM_1.copy()]

        result = self._source.list([ROOM_1['uuid']], {'token_infos': {'xivo_user_uuid': 11}})

        self._search_engine.find.assert_called_once_with(ids=[ROOM_1['uuid']], user_uuid=11)

        expected = ROOM_1.copy()
        expected.pop('room_type')
        assert_that(result, has_item(has_property('fields', expected)))
        assert_that(result, has_item(has_property('is_personal', False)))
        assert_that(result, has_item(has_property('is_deletable', False)))
