{-# language OverloadedStrings #-}
{-# language ScopedTypeVariables #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module TestMVC where

import           MVC.Extended
import Data.Time.Extended

import           Control.Applicative
import           Control.Concurrent (threadDelay)
import           Control.Concurrent.Async
import           Control.Error
import           Control.Lens hiding (each)
import           Control.Monad
import           Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as C
import           MVC
import qualified MVC.Prelude as MVC
import           Pipes.Network.TCP as TCP
import qualified Pipes.Prelude as Pipes
import           Test.QuickCheck
import           Test.QuickCheck.Monadic
import           Test.Tasty
import           Test.Tasty.HUnit
import           Control.Monad.State.Strict (State)
import qualified System.IO as IO
-- import qualified Control.Monad.Managed as M

instance Arbitrary ByteString where
    arbitrary = C.pack <$> (arbitrary :: Gen String) 


-- | pure model components
demo :: (Eq a) => a -> (a->b) -> Pipe a b (State ()) ()
demo in' f = until' in' >-> Pipes.map f
  where
    until' stop' = do
        a <- await
        yield a
        when (a /= stop') (until' stop')


t1 :: IO ()
t1 = runMVC () (asPipe (demo "q" id)) vc'
  where
    vc' = (,) <$> pure (contramap C.unpack MVC.stdoutLines) <*>
          MVC.producer Single (each $ (("1"<>) . C.singleton) <$> ['a'..'z']) <>
          MVC.producer Single (each $ (("2"<>) . C.singleton) <$> ['a'..'z'])


inFile :: FilePath -> Managed IO.Handle
inFile filePath = managed (IO.withFile filePath IO.ReadMode)

{-
testZip = do
    h1 <- IO.openFile "../iqfeed/data/replay-timed-out.log" IO.ReadMode 
    h2 <- IO.openFile "../iqfeed/data/timed-out.log" IO.ReadMode
    runEffect $ Pipes.zip (Pipes.fromHandle h1 >-> Pipes.map (read::String -> Timed In)) (Pipes.fromHandle h2 >-> Pipes.map (read::String -> Timed In)) >-> Pipes.map (\(a0,a1) -> (_teEvent a0) == (_teEvent a1)) >-> Pipes.print
-}

unitTests :: TestTree
unitTests = Test.Tasty.testGroup "TestMVC - unit tests"
    [ testCase "controller ordering" (True @=? True)
    ]
