{-# LANGUAGE RebindableSyntax, GeneralizedNewtypeDeriving, TupleSections, FlexibleInstances, MultiParamTypeClasses, RankNTypes, ViewPatterns #-}
module SimpleH.Reactive (
  module SimpleH.Reactive.Time,
  module SimpleH.Reactive.TimeVal,

  -- * Reactive Events
  Event,_event,Reactive(..),

  -- ** Contructing events
  atTimes,mkEvent,
  withTime,times,
  mapFutures,

  -- ** Combining events
  (//),(<|*>),(<*|>),
               
  -- ** Filtering events
  groupE,mask,

  -- ** Real-world event synchronization
  sink,event,
  
  -- * Future values
  Future,_future,_time,_value,futureIO,
  ) where

import SimpleH
import Control.Concurrent
import SimpleH.Reactive.TimeVal
import System.IO.Unsafe (unsafeInterleaveIO)
import Data.List (group)
import SimpleH.Reactive.Time

-- |An event (a list of time-value pairs of increasing times)
newtype Event t a = Event { getEvent :: Compose OrdList (Future t) a }
                  deriving (Unit,Functor,Foldable,Traversable)
data Reactive t a = Reactive a (Event t a)
instance Ord t => Unit (Reactive t) where
  pure a = Reactive a zero
instance Functor (Reactive t) where 
  map f (Reactive a e) = Reactive (f a) (map f e)
instance Ord t => Applicative (Reactive t) where
  Reactive f fs <*> Reactive x xs = Reactive (f x) (cons f fs<*>cons x xs)
    where cons a = _event %%~ tail . ((minBound,a)^._future :)

instance (Ord t,Show t,Show a) => Show (Event t a) where show = show . at' _event
instance Ord t => Semigroup (Event t a) where
  (+) = warp2 (from _Event) (+)
instance Ord t => Monoid (Event t a) where zero = []^._event
instance Ord t => Applicative (Event t) where
  fe@(at' _event -> f:_) <*> xe@(at' _event -> x:_) =
    (traverse (at _state) e)^.._state & \st ->
    br ((f^._time)+(x^._time)) (snd (st (f^._value,x^._value)))
    where e = map (\f (_,x) -> ((f,x),f x)) fe
              + map (\x (f,_) -> ((f,x),f x)) xe
          br t (at' _event -> e) = (map (_time %- t) b + a)^._event
            where (b,a) = span (\f -> f^._time<t) (uniq e)
                  uniq = map last . group
  _ <*> _ = zero
instance Ord t => Monad (Event t) where
  join = map (at' _event) >>> at' _event >>> map (sequence >>> map join)
         >>> merge >>> at _event
    where merge [] = []
          merge [t] = t
          merge ([]:t) = merge t
          merge ((x:xs):ys:t) = x:merge (sum xs ys : t)
            where sum = warp2 _OrdList (+)

type EventRep t a = OrdList (Future t a)
_Event :: Iso (Event t a) (Event t' b) (EventRep t a) (EventRep t' b)
_Event = _Compose.iso Event getEvent
_event :: Iso (Event t a) (Event t' b) [Future t a] [Future t' b]
_event = _OrdList._Event
atTimes ts = (ts <&> \t -> (pure t,())^._future)^._event
mkEvent as = (as <&> at _future . (_1 %~ pure))^._event

{-| The \'splice\' operator. Occurs when @a@ occurs.

> at t: a // b = (a,before t: b)
-}
(//) :: Ord t => Event t a -> Event t b -> Event t (a, Event t b)
bs // es = mapAccum_ fun (bs^.._event) (es^.._event) ^. _event
  where fun b es = (ys,b & _value %~ (,xs^._event))
          where (xs,ys) = span (flip ltFut b) es
infixl 1 //

{-|
The \'over\' operator. Occurs only when @a@ occurs.

> at t: a <|*> (bi,b) = a <*> (minBound,bi):b
-}
(<*|>) :: Ord t => Event t (a -> b) -> Reactive t a -> Event t b
fs <*|> Reactive a as = (traverse tr (fs // as) ^.. _state <&> snd) a
  where tr (f,as) = traverse_ put as >> f<$>get
infixl 2 <*|>
(<|*>) :: Ord t => Reactive t (a -> b) -> Event t a -> Event t b
f <|*> a = (&)<$>a<*|>f
infixr 1 <|*>

-- |Group the occurences of an event by equality. Occurs when the first occurence of a group occurs. 
groupE = _event %%~ groupE . (+repeat (Future (maxBound,undefined)))
  where groupE fs = (f & _value %- xs):(z & _time %~ (sum (at _time<$>xs)+)):zs
          where (xs,ys) = span ((==f^._value) . at _value) fs ; f = head fs
                ~(z:zs) = groupE ys
                sum = foldl' (+) zero

mapFutures f = _event %%~ map f
withTime = mapFutures (\(Future f) -> Future (_1%~timeVal <$> listen f))
times = map2 fst withTime

mask m e = (m // e) `withNext` (True,zero) >>= \((b,_),(_,e)) -> guard b >> e

-- |Sinks an action event into the Real World. Each action is executed 
sink l = for_ (withTime l) $ \(Since t,v) -> waitTill t >> v
event m = at _event<$>event' zero
  where event' t = unsafeInterleaveIO $ do
          f <- futureIO (timeVal t `seq` m)
          fs <- event' (f^._time)
          return (f:fs)

-- |A Future value (a value with a timestamp)
newtype Future t a = Future (Time t,a)
                   deriving (Show,Functor,Unit,Applicative,Traversable,Foldable,Monad,Semigroup,Monoid)
instance Ord t => Eq (Future t a) where f == f' = compare f f'==EQ
instance Ord t => Ord (Future t a) where compare = cmpFut
instance Ord t => Orderable (Future t a) where
  inOrder (Future (t,a)) (Future (t',b)) = (Future (tx,x),Future (ty,y),z)
    where (tx,ty,z) = inOrder t t'
          ~(x,y) = if z then (a,b) else (b,a)
_future :: Iso (Future t a) (Future t' b) (Time t,a) (Time t',b)
_future = iso Future (\(Future ~(t,a)) -> (t,a))
_time :: Lens (Time t) (Time t') (Future t a) (Future t' a)
_time = from _future._1
_value :: Lens a b (Future t a) (Future t b)
_value = from _future._2
cmpFut :: Ord t => Future t a -> Future t b -> Ordering
cmpFut a b = compare (a^._time) (b^._time)
ltFut a b = cmpFut a b == LT
futureIO :: IO a -> IO (Future Seconds a)
futureIO m = do
  val <- newEmptyMVar
  forkIO $ putMVar val =<< m 
  time <- timeIO (readMVar val)
  return (Future (time,readMVar val^._thunk))