{-# LANGUAGE TypeFamilies, UndecidableInstances #-}

module PrioritySync.Internal.Receipt
    (Receipt(..))
    where

import PrioritySync.Internal.UserData
import PrioritySync.Internal.RoomGroup
import PrioritySync.Internal.ClaimContext
import Control.Concurrent.STM

-- | Get a notification when a claim is approved or scheduled.
data Receipt c = Receipt {
    receipt_base_context :: c,
    receipt_entering_callback, receipt_exiting_callback :: ClaimHandle c -> STM () }

type instance UserData (Receipt c) = UserData c

instance (RoomGroup c) => RoomGroup (Receipt c) where
    roomsOf r = roomsOf $ receipt_base_context r

instance (ClaimContext c) => ClaimContext (Receipt c) where
    type ClaimHandle (Receipt c) = ClaimHandle c
    approveClaimsEntering r cs =
        do result <- approveClaimsEntering (receipt_base_context r) cs
           receipt_entering_callback r $ result
           return result
    approveClaimsExiting r cs =
        do result <- approveClaimsExiting (receipt_base_context r) cs
           receipt_exiting_callback r $ result
           return result
    waitingAction r = waitingAction $ receipt_base_context r

