"""A dummy backend for use in tests.

This backend implements the backend API in the simplest way possible.  It is
used in tests of the frontends.

The backend handles URIs starting with ``dummy:``.

**Dependencies:**

- None

**Settings:**

- None
"""

from __future__ import unicode_literals

import pykka

from mopidy.backends import base
from mopidy.models import Playlist


class DummyBackend(pykka.ThreadingActor, base.Backend):
    def __init__(self, audio):
        super(DummyBackend, self).__init__()

        self.library = DummyLibraryProvider(backend=self)
        self.playback = DummyPlaybackProvider(audio=audio, backend=self)
        self.playlists = DummyPlaylistsProvider(backend=self)

        self.uri_schemes = ['dummy']


class DummyLibraryProvider(base.BaseLibraryProvider):
    def __init__(self, *args, **kwargs):
        super(DummyLibraryProvider, self).__init__(*args, **kwargs)
        self.dummy_library = []

    def find_exact(self, **query):
        return Playlist()

    def lookup(self, uri):
        matches = filter(lambda t: uri == t.uri, self.dummy_library)
        if matches:
            return matches[0]

    def refresh(self, uri=None):
        pass

    def search(self, **query):
        return Playlist()


class DummyPlaybackProvider(base.BasePlaybackProvider):
    def __init__(self, *args, **kwargs):
        super(DummyPlaybackProvider, self).__init__(*args, **kwargs)
        self._time_position = 0

    def pause(self):
        return True

    def play(self, track):
        """Pass a track with URI 'dummy:error' to force failure"""
        self._time_position = 0
        return track.uri != 'dummy:error'

    def resume(self):
        return True

    def seek(self, time_position):
        self._time_position = time_position
        return True

    def stop(self):
        return True

    def get_time_position(self):
        return self._time_position


class DummyPlaylistsProvider(base.BasePlaylistsProvider):
    def create(self, name):
        playlist = Playlist(name=name)
        self._playlists.append(playlist)
        return playlist

    def delete(self, playlist):
        self._playlists.remove(playlist)

    def lookup(self, uri):
        return filter(lambda p: p.uri == uri, self._playlists)

    def refresh(self):
        pass

    def rename(self, playlist, new_name):
        self._playlists[self._playlists.index(playlist)] = \
            playlist.copy(name=new_name)

    def save(self, playlist):
        self._playlists.append(playlist)

@@ -2032,16 +2032,39 @@
 st(name=
+name, uri='dummy:%25s' %25 
 name)%0A  
@@ -2144,35 +2144,91 @@
 elete(self, 
-playlist):%0A
+uri):%0A        playlist = self.lookup(uri)%0A        if playlist:%0A    
         self
@@ -2296,60 +2296,108 @@
     
-return filter(lambda p: p.uri == uri, self._
+for playlist in self._playlists:%0A            if playlist.uri == uri:%0A                return 
 playlist
 s)%0A%0A
@@ -2392,18 +2392,16 @@
 playlist
-s)
 %0A%0A    de
@@ -2443,13 +2443,11 @@
 def 
-renam
+sav
 e(se
@@ -2462,18 +2462,8 @@
 list
-, new_name
 ):%0A 
@@ -2461,37 +2461,35 @@
 ylist):%0A        
-self.
+old
 _playlists%5Bself.
@@ -2485,24 +2485,31 @@
 list
-s%5B
+ = 
 self.
-_
+lookup(
 playlist
 s.in
@@ -2508,103 +2508,172 @@
 list
-s.index(playlist)%5D = %5C%0A            playlist.copy(name=new_name)%0A%0A    def save(self, playlist):%0A
+.uri)%0A%0A        if old_playlist is not None:%0A            index = self._playlists.index(old_playlist)%0A            self._playlists%5Bindex%5D = playlist%0A        else:%0A    
     
@@ -2689,28 +2689,53 @@
 _playlists.append(playlist)%0A
+%0A        return playlist%0A

Update dummy backend's playlists provider implementation