# -*- coding: UTF-8 -*-

import simplejson
from jukebox.jukebox_core.tests.api import ApiTestBase

class ApiQueueTest(ApiTestBase):
    def testIndexEmpty(self):
        result = simplejson.loads(
            self.httpGet(
                "/api/v1/queue"
            ).content
        )
        self.assertEquals(len(result["itemList"]), 0)

    def testAddAndIndex(self):
        song = self.addSong(artist=self.addArtist())

        # check that song is not in queue
        result = simplejson.loads(
            self.httpGet(
                "/api/v1/songs"
            ).content
        )
        self.assertEquals(len(result["itemList"]), 1)
        self.assertEquals(result["itemList"][0]["id"], song.id)
        self.assertFalse(result["itemList"][0]["queued"])

        # add to queue
        response = self.httpPost(
            "/api/v1/queue",
            {"id": song.id}
        )
        content = simplejson.loads(
            response.content
        )
        self.assertEqual(response.status_code, 201)
        self.assertEqual(content["id"], song.id)

        # check queue
        result = simplejson.loads(
            self.httpGet(
                "/api/v1/queue"
            ).content
        )
        self.assertEquals(len(result["itemList"]), 1)
        self.assertEquals(result["itemList"][0]["id"], song.id)

        # check that song is marked as queued
        result = simplejson.loads(
            self.httpGet(
                "/api/v1/songs"
            ).content
        )
        self.assertEquals(len(result["itemList"]), 1)
        self.assertEquals(result["itemList"][0]["id"], song.id)
        self.assertTrue(result["itemList"][0]["queued"])

    def testDeleteAndIndex(self):
        song = self.addSong(artist=self.addArtist())

        # add to queue
        response = self.httpPost(
            "/api/v1/queue",
            {"id": song.id}
        )
        content = simplejson.loads(
            response.content
        )
        self.assertEqual(response.status_code, 201)
        self.assertEqual(content["id"], song.id)

        # check queue
        result = simplejson.loads(
            self.httpGet(