class TestSearchAndPlay(unittest.TestCase):
    def setUp(self):
        library_cache = LibraryCache()
        self.kodi = KodiInterface(library_cache)
        self.kodi.update_cache()
        wait_for_library_index(self.kodi)
        self.player = CustomPlayer()
        self.player.set_kodi(self.kodi)
        self.handler = Handler(self.kodi)

    def test_search_and_play_not_found(self):
        self.handler.handler({
            "type": "command",
            "commandType": "searchAndPlay",
            "filter": {
                'titles': ['some made up name'],
            }
        })
        run_one()

        current_item = self.player._get_current_item()

        self.assertIsNone(current_item)

    def test_search_and_play_tvshow(self):
        self.handler.handler({
            "type": "command",
            "commandType": "searchAndPlay",
            "filter": {
                'titles': ['How I Met Your Mother'],
                'season': '2',
                'episode': '1',
            }
        })
        run_one()

        current_item = self.player._get_current_item()

        self.assertEqual(current_item['type'], 'episode')
        self.assertEqual(current_item['tvshowid'], 21)
        self.assertEqual(current_item['id'], 423)
        self.assertEqual(current_item['season'], 2)
        self.assertEqual(current_item['episode'], 1)

    def test_search_and_play_tvshow_next_episode(self):
        self.handler.handler({
            "type": "command",
            "commandType": "searchAndPlay",
            "filter": {
                u'genres': [],
                u'episode': None,
                u'roles': [],
                u'season': None,
                u'mediaType': None,
                u'titles': [u'How I Met Your Mother', u'Goodbye How I Met Your Mother', u'How I Met Your Mother: Extras', u'How I Met Your Puppet Mother'],
                u'actors': [],
                u'collections': [],
            }
        })
        run_one()

        current_item = self.player._get_current_item()

        self.assertEqual(current_item['type'], 'episode')
        self.assertEqual(current_item['tvshowid'], 21)
        self.assertEqual(current_item['id'], 487)
        self.assertEqual(current_item['season'], 4)
        self.assertEqual(current_item['episode'], 23)

    def test_search_and_play_movie_by_title(self):
        self.handler.handler({
            "type": "command",
            "commandType": "searchAndPlay",
            "filter": {
                'titles': ['Maze Runner'],
            }
        })
        run_one()

        current_item = self.player._get_current_item()

        self.assertEqual(current_item['type'], 'movie')
        self.assertEqual(current_item['id'], 161)

    def test_search_and_play_movie_by_title_and_movie_mediatype(self):
        self.handler.handler({
            "type": "command",
            "commandType": "searchAndPlay",
            "filter": {
                'titles': ['Maze Runner'],
                'mediaType': 'movie',
            }
        })
        run_one()

        current_item = self.player._get_current_item()

        self.assertEqual(current_item['type'], 'movie')
        self.assertEqual(current_item['id'], 161)

    def test_search_and_play_movie_by_title_and_tvshow_mediatype(self):
        self.handler.handler({
            "type": "command",
            "commandType": "searchAndPlay",
            "filter": {
                'titles': ['Maze Runner'],
                'mediaType': 'tv show',
            }
        })
        run_one()

        current_item = self.player._get_current_item()

        self.assertIsNone(current_item)

    def test_search_and_play_movie_by_actor(self):
        self.handler.handler({
            "type": "command",
            "commandType": "searchAndPlay",
            "filter": {
                'actors': ['Jason Statham'],
            }
        })
        run_one()

        current_item = self.player._get_current_item()

        self.assertEqual(current_item['type'], 'movie')
        self.assertIn(current_item['id'], [202, 163, 48])

    def test_search_and_play_movie_by_title_and_actor(self):
        self.handler.handler({
            "type": "command",
            "commandType": "searchAndPlay",
            "filter": {
                'titles': ['Furious'],
                'actors': ['Jason Statham'],
            }
        })
        run_one()

        current_item = self.player._get_current_item()

        self.assertEqual(current_item['type'], 'movie')
        self.assertEqual(current_item['id'], 202)
class TestPreviousNext(unittest.TestCase):
    def setUp(self):
        library_cache = LibraryCache()
        self.kodi = KodiInterface(library_cache)
        self.kodi.update_cache()
        wait_for_library_index(self.kodi)
        self.player = CustomPlayer()
        self.player.set_kodi(self.kodi)
        self.handler = Handler(self.kodi)

    def test_no_current_item_previous(self):
        current_item = self.player._get_current_item()
        self.assertIsNone(current_item)

        self.assertFalse(self.kodi.previous_item())

        current_item = self.player._get_current_item()
        self.assertIsNone(current_item)

    def test_no_previous_item(self):
        self.kodi.find_and_play({
            'titles': ['How I Met Your Mother'],
            'season': '1',
            'episode': '1',
        })

        self.handler.handler({
            "type": "command",
            "commandType": "previous",
        })
        run_one()

        current_item = self.player._get_current_item()

        self.assertEqual(current_item['type'], 'episode')
        self.assertEqual(current_item['tvshowid'], 21)
        self.assertEqual(current_item['id'], 401)
        self.assertEqual(current_item['season'], 1)
        self.assertEqual(current_item['episode'], 1)

    def test_no_next_item(self):
        self.kodi.find_and_play({
            'titles': ['How I Met Your Mother'],
            'season': '6',
            'episode': '24',
        })

        self.handler.handler({
            "type": "command",
            "commandType": "next",
        })
        run_one()

        current_item = self.player._get_current_item()

        self.assertEqual(current_item['type'], 'episode')
        self.assertEqual(current_item['tvshowid'], 21)
        self.assertEqual(current_item['id'], 536)
        self.assertEqual(current_item['season'], 6)
        self.assertEqual(current_item['episode'], 24)

    def test_previous_item(self):
        self.kodi.find_and_play({
            'titles': ['How I Met Your Mother'],
            'season': '2',
            'episode': '2',
        })

        self.handler.handler({
            "type": "command",
            "commandType": "previous",
        })
        run_one()

        current_item = self.player._get_current_item()

        self.assertEqual(current_item['type'], 'episode')
        self.assertEqual(current_item['tvshowid'], 21)
        self.assertEqual(current_item['id'], 423)
        self.assertEqual(current_item['season'], 2)
        self.assertEqual(current_item['episode'], 1)

    def test_previous_item_cross_season(self):
        self.kodi.find_and_play({
            'titles': ['How I Met Your Mother'],
            'season': '2',
            'episode': '1',
        })

        self.handler.handler({
            "type": "command",
            "commandType": "previous",
        })
        run_one()

        current_item = self.player._get_current_item()

        self.assertEqual(current_item['type'], 'episode')
        self.assertEqual(current_item['tvshowid'], 21)
        self.assertEqual(current_item['id'], 422)
        self.assertEqual(current_item['season'], 1)
        self.assertEqual(current_item['episode'], 22)

    def test_next_item(self):
        self.kodi.find_and_play({
            'titles': ['How I Met Your Mother'],
            'season': '1',
            'episode': '1',
        })

        self.handler.handler({
            "type": "command",
            "commandType": "next",
        })
        run_one()

        current_item = self.player._get_current_item()

        self.assertEqual(current_item['type'], 'episode')
        self.assertEqual(current_item['tvshowid'], 21)
        self.assertEqual(current_item['id'], 402)
        self.assertEqual(current_item['season'], 1)
        self.assertEqual(current_item['episode'], 2)

    def test_next_item_cross_season(self):
        self.kodi.find_and_play({
            'titles': ['How I Met Your Mother'],
            'season': '1',
            'episode': '22',
        })

        self.handler.handler({
            "type": "command",
            "commandType": "next",
        })
        run_one()

        current_item = self.player._get_current_item()

        self.assertEqual(current_item['type'], 'episode')
        self.assertEqual(current_item['tvshowid'], 21)
        self.assertEqual(current_item['id'], 423)
        self.assertEqual(current_item['season'], 2)
        self.assertEqual(current_item['episode'], 1)