-- Copyright 2010 Evgeniy Vodolazskiy (waterlaz@gmail.com)
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-- Lesser General Public License for more details.

module Network.XMMS.Client (
    Connection,
    Result,
    XMMSCV(..),
    SeekMode(..),
    init,
    connect,
    getLastError,
    quit,
    broadcastQuit,
    userConfigDirGet,
    resultWait,
    propdictToDict,
    resultGetValue,
    resultNotifierSet,
    resultGetConnection
)where

import Network.XMMS.Constants


import Network.XMMS.Types
import Network.XMMS.Utilities

import Foreign
import Foreign.Ptr
import Foreign.C.Types
import Network.XMMS.UTF8Strings
import Foreign.ForeignPtr
import Foreign.Marshal.Alloc
import Prelude hiding (init)
import Network.XMMS.Result
import Network.XMMS.Value



--xmmsc_connection_t  *xmmsc_init  (const char *clientname)
foreign import ccall unsafe "xmmsclient/xmmsclient.h xmmsc_init"
    xmmsc_init :: CString -> IO (Ptr C_xmmsc_connection)
-- |Initializes a connection. With the coresponding name
init :: String -> IO Connection
init name = do
    c_name <- newCString name
    ptr <- xmmsc_init c_name
    free c_name
    newForeignPtr xmmsc_unref ptr



--int xmmsc_connect (xmmsc_connection_t *c, const char *ipcpath)
foreign import ccall unsafe "xmmsclient/xmmsclient.h xmmsc_connect" 
    xmmsc_connect :: Ptr C_xmmsc_connection -> CString -> IO CInt
-- | Connects to the XMMS server.
connect :: Connection -> String -> IO Int
connect connection ipcpath =
    withForeignPtr connection ( \ptrCon -> do
        res <- if ipcpath == ""
                then xmmsc_connect ptrCon nullPtr
                else do c_ipcpath <- newCString ipcpath
                        xmmsc_connect ptrCon c_ipcpath
        return $ fromIntegral res)


--Set the disconnect callback. 	
--void 	xmmsc_disconnect_callback_set (xmmsc_connection_t *c, xmmsc_disconnect_func_t callback, void *userdata)
--void 	xmmsc_disconnect_callback_set_full (xmmsc_connection_t *c, xmmsc_disconnect_func_t callback, void *userdata, xmmsc_user_data_free_func_t free_func)


--char * 	xmmsc_get_last_error (xmmsc_connection_t *c)
foreign import ccall unsafe "xmmsclient/xmmsclient.h xmmsc_get_last_error" 
    xmmsc_get_last_error :: Ptr C_xmmsc_connection -> IO CString
-- |Returns a string that descibes the last error.
getLastError :: Connection -> IO String
getLastError connection =
    withForeignPtr connection ( \ptrCon -> do
        c_errorMsg <- xmmsc_get_last_error ptrCon
        safePeekUTF8String c_errorMsg)

 	



--Set locking functions for a connection.
--void 	xmmsc_lock_set (xmmsc_connection_t *c, void *lock, void(*lockfunc)(void *), void(*unlockfunc)(void *))

    

--xmmsc_result_t * 	xmmsc_quit (xmmsc_connection_t *c)
foreign import ccall unsafe "xmmsclient/xmmsclient.h xmmsc_quit" 
    xmmsc_quit :: Ptr C_xmmsc_connection -> IO (Ptr C_xmmsc_result)
-- |Tell the server to quit.
quit :: Connection -> IO Result
quit = wrapCallResult xmmsc_quit


--xmmsc_result_t * 	xmmsc_broadcast_quit (xmmsc_connection_t *c)
foreign import ccall unsafe "xmmsclient/xmmsclient.h xmmsc_broadcast_quit" 
    xmmsc_broadcast_quit :: Ptr C_xmmsc_connection -> IO (Ptr C_xmmsc_result)
-- |Request the quit broadcast.
broadcastQuit :: Connection -> IO Result
broadcastQuit = wrapCallResult xmmsc_broadcast_quit



--const char * 	xmmsc_userconfdir_get (char *buf, int len)
foreign import ccall unsafe "xmmsclient/xmmsclient.h xmmsc_userconfdir_get" 
    xmmsc_userconfdir_get :: Ptr CChar -> CInt -> IO (Ptr CChar)
-- |Get the absolute path to the user config dir. 
-- 
-- /The Int parameter is the buffer length and should probably go away... /


userConfigDirGet :: Int -> IO String
userConfigDirGet len = do
    let c = (castCharToCChar 'c') :: CChar
    bufPtr <- mallocBytes (len * sizeOf c)
    xmmsc_userconfdir_get bufPtr (fromIntegral len)
    res <- safePeekUTF8String bufPtr
    free bufPtr
    return res