{- |
Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
License    : LGPL-2.1
Maintainer : Iñaki García Etxebarria (inaki@blueleaf.cc)

The GParameter struct is an auxiliary structure used
to hand parameter name\/value pairs to 'GI.GObject.Objects.Object.objectNewv'.
-}

#define ENABLE_OVERLOADING (MIN_VERSION_haskell_gi_overloading(1,0,0) \
       && !defined(__HADDOCK_VERSION__))

module GI.GObject.Structs.Parameter
    (

-- * Exported types
    Parameter(..)                           ,
    newZeroParameter                        ,
    noParameter                             ,


 -- * Properties
-- ** name #attr:name#
{- | the parameter name
-}
    clearParameterName                      ,
    getParameterName                        ,
#if ENABLE_OVERLOADING
    parameter_name                          ,
#endif
    setParameterName                        ,


-- ** value #attr:value#
{- | the parameter value
-}
    getParameterValue                       ,
#if ENABLE_OVERLOADING
    parameter_value                         ,
#endif




    ) where

import Data.GI.Base.ShortPrelude
import qualified Data.GI.Base.ShortPrelude as SP
import qualified Data.GI.Base.Overloading as O
import qualified Prelude as P

import qualified Data.GI.Base.Attributes as GI.Attributes
import qualified Data.GI.Base.ManagedPtr as B.ManagedPtr
import qualified Data.GI.Base.GClosure as B.GClosure
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GValue as B.GValue
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
import qualified Data.GI.Base.Properties as B.Properties
import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map
import qualified Foreign.Ptr as FP
import qualified GHC.OverloadedLabels as OL


-- | Memory-managed wrapper type.
newtype Parameter = Parameter (ManagedPtr Parameter)
instance WrappedPtr Parameter where
    wrappedPtrCalloc = callocBytes 32
    wrappedPtrCopy = \p -> withManagedPtr p (copyBytes 32 >=> wrapPtr Parameter)
    wrappedPtrFree = Just ptr_to_g_free

-- | Construct a `Parameter` struct initialized to zero.
newZeroParameter :: MonadIO m => m Parameter
newZeroParameter = liftIO $ wrappedPtrCalloc >>= wrapPtr Parameter

instance tag ~ 'AttrSet => Constructible Parameter tag where
    new _ attrs = do
        o <- newZeroParameter
        GI.Attributes.set o attrs
        return o


-- | A convenience alias for `Nothing` :: `Maybe` `Parameter`.
noParameter :: Maybe Parameter
noParameter = Nothing

{- |
Get the value of the “@name@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' parameter #name
@
-}
getParameterName :: MonadIO m => Parameter -> m (Maybe T.Text)
getParameterName s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 0) :: IO CString
    result <- SP.convertIfNonNull val $ \val' -> do
        val'' <- cstringToText val'
        return val''
    return result

{- |
Set the value of the “@name@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.set' parameter [ #name 'Data.GI.Base.Attributes.:=' value ]
@
-}
setParameterName :: MonadIO m => Parameter -> CString -> m ()
setParameterName s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 0) (val :: CString)

{- |
Set the value of the “@name@” field to `Nothing`.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.clear' #name
@
-}
clearParameterName :: MonadIO m => Parameter -> m ()
clearParameterName s = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 0) (FP.nullPtr :: CString)

#if ENABLE_OVERLOADING
data ParameterNameFieldInfo
instance AttrInfo ParameterNameFieldInfo where
    type AttrAllowedOps ParameterNameFieldInfo = '[ 'AttrSet, 'AttrGet, 'AttrClear]
    type AttrSetTypeConstraint ParameterNameFieldInfo = (~) CString
    type AttrBaseTypeConstraint ParameterNameFieldInfo = (~) Parameter
    type AttrGetType ParameterNameFieldInfo = Maybe T.Text
    type AttrLabel ParameterNameFieldInfo = "name"
    type AttrOrigin ParameterNameFieldInfo = Parameter
    attrGet _ = getParameterName
    attrSet _ = setParameterName
    attrConstruct = undefined
    attrClear _ = clearParameterName

parameter_name :: AttrLabelProxy "name"
parameter_name = AttrLabelProxy

#endif


{- |
Get the value of the “@value@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' parameter #value
@
-}
getParameterValue :: MonadIO m => Parameter -> m GValue
getParameterValue s = liftIO $ withManagedPtr s $ \ptr -> do
    let val = ptr `plusPtr` 8 :: (Ptr GValue)
    val' <- (newBoxed GValue) val
    return val'

#if ENABLE_OVERLOADING
data ParameterValueFieldInfo
instance AttrInfo ParameterValueFieldInfo where
    type AttrAllowedOps ParameterValueFieldInfo = '[ 'AttrGet]
    type AttrSetTypeConstraint ParameterValueFieldInfo = (~) (Ptr GValue)
    type AttrBaseTypeConstraint ParameterValueFieldInfo = (~) Parameter
    type AttrGetType ParameterValueFieldInfo = GValue
    type AttrLabel ParameterValueFieldInfo = "value"
    type AttrOrigin ParameterValueFieldInfo = Parameter
    attrGet _ = getParameterValue
    attrSet _ = undefined
    attrConstruct = undefined
    attrClear _ = undefined

parameter_value :: AttrLabelProxy "value"
parameter_value = AttrLabelProxy

#endif



#if ENABLE_OVERLOADING
instance O.HasAttributeList Parameter
type instance O.AttributeList Parameter = ParameterAttributeList
type ParameterAttributeList = ('[ '("name", ParameterNameFieldInfo), '("value", ParameterValueFieldInfo)] :: [(Symbol, *)])
#endif

#if ENABLE_OVERLOADING
type family ResolveParameterMethod (t :: Symbol) (o :: *) :: * where
    ResolveParameterMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveParameterMethod t Parameter, O.MethodInfo info Parameter p) => OL.IsLabel t (Parameter -> p) where
#if MIN_VERSION_base(4,10,0)
    fromLabel = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#else
    fromLabel _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#endif

#endif