async void chimneyMpdServer_OnAdd(object sender, ResponseEventArgs e)
        {
            bool suc = true;

            string uri = string.Empty;

            File file = null;

            bool addasUri = false;

            if(e.arguments.Count > 0)
            {
                uri = e.arguments[0];

                file = await Dbconnection.FindAsync<File>(o => o.RelativePath.Equals(uri));

                if (file == null) addasUri = true;
            }

            if (file != null || addasUri)
            {
                CurrentPlaylist newCurrentPlaylistItem = new CurrentPlaylist()
                {
                    FileId = (addasUri) ? -1 : file.FileId,
                    IsUri = addasUri,
                    Uri = (addasUri) ? uri : file.FilePath,
                    Bitrate = (addasUri) ? 0 : file.Bitrate,
                    PositionId = await Dbconnection.ExecuteScalarAsync<int>("SELECT COUNT(*) FROM CurrentPlaylist")
                };

                await Dbconnection.InsertAsync(newCurrentPlaylistItem);
            }
            else
            {
                //
                // Get the uri from arguments, if empty uri is empty
                //
                var directory = (e.arguments.Count > 0) ? await Dbconnection.Table<Directory>().Where(o => o.RelativePath.Equals(e.arguments[0])).FirstAsync() : null;

                //
                // Get the sub directories for the the uri directories
                //
                var subDirQuery = Dbconnection.Table<Directory>().Where(o => o.RelativePath.StartsWith(directory.RelativePath) || directory.RelativePath.Equals(string.Empty));
                var subDirectories = await subDirQuery.ToListAsync();
                foreach (Directory subdir in subDirectories)
                {
                    //
                    // Get all files in the current uri
                    //
                    var subfiles = await Dbconnection.QueryAsync<File>("SELECT * FROM Files WHERE Files.DirectoryId = " + subdir.DirectoryId);
                    foreach (File subfile in subfiles)
                    {
                        CurrentPlaylist newCurrentPlaylistItem = new CurrentPlaylist()
                        {
                            FileId = subfile.FileId,
                            PositionId = await Dbconnection.ExecuteScalarAsync<int>("SELECT COUNT(*) FROM CurrentPlaylist")
                        };

                        await Dbconnection.InsertAsync(newCurrentPlaylistItem);
                    }
                }
            }


            if (suc) chimneyMpdServer.AppendResponse(string.Empty, e.id, e.position);
            else
            {
                string errorfile = string.Empty;
                if (e.arguments.Count > 0) errorfile = e.arguments.First<string>();
                chimneyMpdServer.ErrorResponse(MPDKeyWords.Response.ACK + " [[email protected]] {add} could not add file:" + " \"" + errorfile + "\"", e.id, e.position);
            }

            if (OnIdleEvent != null) OnIdleEvent(this, new IdleEventArgs("playlist"));

        }
        async void chimneyMpdServer_OnLoad(object sender, ResponseEventArgs e)
        {
            string playlistName = string.Empty;

            int start = 0;
            int end = 0;

            bool suc = false;

            if (e.arguments.Count > 0)
            {
                playlistName = e.arguments[0];

                if (e.arguments.Count > 1)
                {
                    string[] par = e.arguments[1].Split(new char[] { ':' });
                    suc = int.TryParse(par[1], out start);

                    if (suc && par.Length > 1) suc = int.TryParse(par[2], out end);
                    else end = start;
                }
            }

            var playlistToAddFrom = await Dbconnection.FindAsync<Playlist>(o => o.Name == playlistName);

            if(!suc && playlistToAddFrom != null)
            {
                end = await Dbconnection.ExecuteScalarAsync<int>("SELECT COUNT(*) FROM PlaylistFiles WHERE PlaylistId = " + playlistToAddFrom.PlaylistId);
            }

            if(playlistToAddFrom != null)
            {
                var plFiles = await Dbconnection.QueryAsync<PlaylistFile>("SELECT * FROM PlaylistFiles WHERE PlaylistId = " + playlistToAddFrom.PlaylistId
                    + " AND Postion >= " + start + " AND Position < " + end);


                foreach(PlaylistFile plFile in plFiles)
                {
                    CurrentPlaylist newCurrentPlaylistItem = new CurrentPlaylist()
                    {
                        FileId = (plFile.IsUri) ? -1 : plFile.FileId,
                        IsUri = plFile.IsUri,
                        Uri = plFile.Uri,
                        PositionId = await Dbconnection.ExecuteScalarAsync<int>("SELECT COUNT(*) FROM CurrentPlaylist")
                    };

                    await Dbconnection.InsertAsync(newCurrentPlaylistItem);
                }
            }

            chimneyMpdServer.AppendResponse(string.Empty, e.id, e.position);

            if (OnIdleEvent != null) OnIdleEvent(this, new IdleEventArgs("playlist"));
        }
        async void chimneyMpdServer_OnAddId(object sender, ResponseEventArgs e)
        {
            bool suc = false;

            int id = 0;

            File file = null;
  
            if (e.arguments.Count > 0)
            {
                string uri = e.arguments[0];
                //var files = await Dbconnection.QueryAsync<File>("SELECT * FROM Files WHERE RelativePath = \"" + uri + "\"");
                //file = (files.Count > 0) ? files[0] : null;

                file = await Dbconnection.FindAsync<File>(o => o.RelativePath.Equals(uri));

                //var query = Dbconnection.Table<File>().Where(o => o.RelativePath.Equals(uri));
                //file = await query.FirstAsync();
            }

            int position = -1;

            if (e.arguments.Count > 1) suc = int.TryParse(e.arguments[1], out position);
            else suc = false;

            position = (suc) ? position : -1;

            if (file != null)
            {
                int currentPlaylistCount = await Dbconnection.ExecuteScalarAsync<int>("SELECT COUNT(*) FROM CurrentPlaylist");

                position = (position <= currentPlaylistCount && position >= 0) ? position : currentPlaylistCount;

                if (position != currentPlaylistCount)
                {
                    var affectedFiles = await Dbconnection.QueryAsync<CurrentPlaylist>("UPDATE CurrentPlaylist SET PositionId=PositionId+1 WHERE PositionId >= " + position);
                }

                CurrentPlaylist newCurrentPlaylistItem = new CurrentPlaylist()
                {
                    FileId = file.FileId,
                    PositionId = position,
                    Bitrate = file.Bitrate,
                    IsUri = false,
                    Uri = file.FilePath
                };

                await Dbconnection.InsertAsync(newCurrentPlaylistItem);

                suc = true;

                id = file.FileId;
            }
          
            if (suc) 
            {
                chimneyMpdServer.AppendResponse("Id: " + id + "\n", e.id, e.position);
            }
            else
            {
                string errorfile = string.Empty;
                if (e.arguments.Count > 0) errorfile = e.arguments.First<string>();
                chimneyMpdServer.ErrorResponse(MPDKeyWords.Response.ACK + " [[email protected]] {add} could not add file:" + " \"" + errorfile + "\"", e.id, e.position);
            }

            if (OnIdleEvent != null) OnIdleEvent(this, new IdleEventArgs("playlist"));

        }