#    Copyright 2020 Jonas Waeber
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
import logging
import os
import sys
import unittest
from collections import Iterable
from typing import Dict

from pywaclient.api import AragornApiClient
from pywaclient.models.user import User

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)


class TestEndpoints(unittest.TestCase):

    def setUp(self):
        self.client = AragornApiClient(
            name='TEST APPLICATION',
            url='https://gitlab.com/SoulLink/world-anvil-api-client',
            application_key=os.environ.get('WA_APPLICATION_KEY'),
            authentication_token=os.environ.get('WA_AUTH_TOKEN'),
            version='0.1.0'
        )
        self.maxDiff = None
        self.user = User(self.client)

    def testAuthenticatedUser(self):
        self.assertEqual('42eb1d6a-021b-49b4-bbbb-f7ddf6b135a4', self.client.user.authenticated_user_id)

    def testUserEndpointWorlds(self):
        self.assertEqual('8f312eff-fdcc-49d1-9626-a98af28ada54', self.client.user.worlds()[0]['id'])

    def testUserEndpointManuscripts(self):
        self.assertEqual('6c63f740-5a47-455d-b51f-8750008c84e4', self.client.user.manuscripts()[0]['id'])

    def testWorldEndpointArticles(self):
        articles = self.client.world.articles(self.client.user.worlds()[0]['id'])
        self.assertTrue(isinstance(articles, Iterable))

    def testImageEndpointGetBinary(self):
        images = self.client.world.images(self.client.user.worlds()[0]['id'])
        byte_stream = self.client.image.get_binary(next(images)['id'])
        self.assertTrue(isinstance(byte_stream, Iterable))

    def testManuscriptEndpointExport(self):
        manuscript = self.client.user.manuscripts(self.client.user.authenticated_user_id)[0]
        manuscript_export = self.client.manuscript.export(manuscript['id'])
        self.assertTrue(isinstance(manuscript_export, Dict))

    def testArticleEndpointGet(self):
        article = self.client.article.get('4d8c93bc-bdb4-418c-b3d0-8c14fa1bf59c', load_all_properties="true")
        article2 = self.client.article.get('4d8c93bc-bdb4-418c-b3d0-8c14fa1bf59c')
        self.assertEqual(article, article2)

    def testManuscript(self):
        manuscript = [x for x in self.user.manuscripts()][0]
        user = manuscript.author
        self.assertEqual(user.id, self.user.id)

    def testArticle(self):
        world = [x for x in self.user.worlds()][0]
        article = [x for x in world.articles()][0]
        self.assertTrue(article.full_render != '')


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