'''
    Copyright (C) 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.

    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 unittest
import json
import os

from . import BaseTestCase


class TestLogs(BaseTestCase):
    @classmethod
    def setUpClass(self):
        self.base_url = '/mapservice/v1/logs'

    def test_get(self):
        """Test case getting log (src)
		"""
        headers = {'Accept': 'application/json'}
        response = self.client.open(
            '{}/{}'.format(self.base_url, self.db_content.test_log.id),
            method='GET',
            headers=headers
        )
        assert response.status_code == 200
        data = json.loads(response.data)
        assert len(data) == 2
        assert 'entries' in data
        assert 'metaData' in data
        metaData = data['metaData']
        assert "submitter" in metaData
        self.check_key(
            "submitter", self.db_content.test_log.submitter.username, metaData
        )
        assert 'water' in metaData
        self.check_key(
            "id", self.db_content.test_log.water.id, metaData['water']
        )
        self.check_key(
            "label", self.db_content.test_log.water.name, metaData['water']
        )
        assert 'device' in metaData
        self.check_key(
            "id", self.db_content.test_log.device.id, metaData['device']
        )
        self.check_key(
            "label", self.db_content.test_log.device.name, metaData['device']
        )
        assert 'gauge' in metaData
        self.check_key(
            "id", self.db_content.test_log.gauge.id, metaData['gauge']
        )
        self.check_key(
            "label", self.db_content.test_log.gauge.name, metaData['gauge']
        )
        self.check_key(
            "gaugeHeight", self.db_content.test_log.gauge_height, metaData
        )

    def test_get_by_manager(self):
        """Test case getting log by submitter (src)
		"""
        headers = {'Accept': 'application/json'}

        response = self.client.open(
            '{}?submitter=not_existent'.format(self.base_url),
            method='GET',
            headers=headers
        )
        assert response.status_code == 404, '{} != {}'.format(
            response.status_code, 404
        )

        response = self.client.open(
            '{}?submitter={}'.format(
                self.base_url, self.db_content.test_log.submitter.username
            ),
            method='GET',
            headers=headers
        )
        assert response.status_code == 200
        data = json.loads(response.data)
        assert len(data) == 1
        data = data[0]
        self.check_key(
            "submitter", self.db_content.test_log.submitter.username, data
        )
        assert 'water' in data
        self.check_key("id", self.db_content.test_log.water.id, data['water'])
        self.check_key(
            "label", self.db_content.test_log.water.name, data['water']
        )
        assert 'device' in data
        self.check_key(
            "id", self.db_content.test_log.device.id, data['device']
        )
        self.check_key(
            "label", self.db_content.test_log.device.name, data['device']
        )
        assert 'gauge' in data
        self.check_key("id", self.db_content.test_log.gauge.id, data['gauge'])
        self.check_key(
            "label", self.db_content.test_log.gauge.name, data['gauge']
        )
        self.check_key(
            "gaugeHeight", self.db_content.test_log.gauge_height, data
        )

    def test_create_log(self):
        """Test case create log (src)
		"""
        headers = {
            'X-API-KEY': 'test_key',
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }
        logCreation = {
            'water': 1,
            'device': 1,
            'gauge': 1,
            'gaugeHeight': 1.0,
            'comment': 'Log from unit test'
        }

        response = self.client.open(
            self.base_url,
            method='POST',
            headers=headers,
            data=json.dumps(logCreation),
            content_type='application/json'
        )
        assert response.status_code == 200
        data = response.get_json()
        assert len(data) >= 7
        self.check_key("logId", 2, data)
        assert 'water' in data
        self.check_key("id", logCreation['water'], data['water'])
        assert 'device' in data
        self.check_key("id", logCreation['device'], data['device'])
        assert 'gauge' in data
        self.check_key("id", logCreation['gauge'], data['gauge'])
        self.check_key("gaugeHeight", logCreation['gaugeHeight'], data)
        self.check_key("comment", logCreation['comment'], data)

        # could not be tested without running CELERY_BROKER
        #filename = '{}/data/nmea0183-2017-12-09.log'.format(os.path.dirname(os.path.abspath(__file__)))
        #fileContent = open(filename, 'rb')
        # headers["Accept"] = "application/geo+json"
        #response = self.client.post(
        #"{}/{}".format(self.base_url, data["logId"]),
        #data = fileContent,
        #headers = headers
        #)
        #assert response.status_code == 200


if __name__ == '__main__':
    unittest.main()
