{-# LANGUAGE MultiParamTypeClasses #-}

-- |
-- Module     : Simulation.Aivika.Experiment.Base.ExperimentSpecsView
-- Copyright  : Copyright (c) 2012-2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 8.0.1
--
-- The module defines 'ExperimentSpecsView' that shows the 
-- experiment specs.
--

module Simulation.Aivika.Experiment.Base.ExperimentSpecsView 
       (ExperimentSpecsView(..),
        defaultExperimentSpecsView) where

import Control.Monad
import Control.Monad.Trans

import Data.Monoid

import Simulation.Aivika.Experiment.Types
import Simulation.Aivika.Experiment.Base.WebPageRenderer
import Simulation.Aivika.Experiment.Base.HtmlWriter
import Simulation.Aivika.Experiment.Base.ExperimentWriter
import Simulation.Aivika.Experiment.Base.ExperimentSpecsWriter

-- | Defines the 'View' that shows the experiment specs.
data ExperimentSpecsView =
  ExperimentSpecsView { ExperimentSpecsView -> String
experimentSpecsTitle       :: String,
                        -- ^ The title for the view.
                        ExperimentSpecsView -> String
experimentSpecsDescription :: String,
                        -- ^ The description for the view.
                        ExperimentSpecsView -> ExperimentSpecsWriter
experimentSpecsWriter      :: ExperimentSpecsWriter
                        -- ^ It shows the specs.
                      }
  
-- | This is the default view.
defaultExperimentSpecsView :: ExperimentSpecsView
defaultExperimentSpecsView :: ExperimentSpecsView
defaultExperimentSpecsView =  
  ExperimentSpecsView { experimentSpecsTitle :: String
experimentSpecsTitle       = String
"Experiment Specs",
                        experimentSpecsDescription :: String
experimentSpecsDescription = String
"It shows the experiment specs.",
                        experimentSpecsWriter :: ExperimentSpecsWriter
experimentSpecsWriter      = ExperimentSpecsWriter
defaultExperimentSpecsWriter }

instance ExperimentView ExperimentSpecsView (WebPageRenderer a) where  
  
  outputView :: ExperimentSpecsView -> ExperimentGenerator (WebPageRenderer a)
outputView ExperimentSpecsView
v = 
    let reporter :: Experiment
-> p
-> p
-> ExperimentWriter (ExperimentReporter (WebPageRenderer a))
reporter Experiment
exp p
renderer p
dir =
          do ExperimentSpecsViewState
st <- ExperimentSpecsView
-> Experiment -> ExperimentWriter ExperimentSpecsViewState
newExperimentSpecs ExperimentSpecsView
v Experiment
exp
             let context :: ExperimentContext (WebPageRenderer a)
context =
                   forall a. WebPageWriter -> ExperimentContext (WebPageRenderer a)
WebPageContext forall a b. (a -> b) -> a -> b
$
                   WebPageWriter { reporterWriteTOCHtml :: Int -> HtmlWriter ()
reporterWriteTOCHtml = ExperimentSpecsViewState -> Int -> HtmlWriter ()
experimentSpecsTOCHtml ExperimentSpecsViewState
st,
                                   reporterWriteHtml :: Int -> HtmlWriter ()
reporterWriteHtml    = ExperimentSpecsViewState -> Int -> HtmlWriter ()
experimentSpecsHtml ExperimentSpecsViewState
st }
             forall (m :: * -> *) a. Monad m => a -> m a
return ExperimentReporter { reporterInitialise :: ExperimentMonad (WebPageRenderer a) ()
reporterInitialise = forall (m :: * -> *) a. Monad m => a -> m a
return (),
                                         reporterFinalise :: ExperimentMonad (WebPageRenderer a) ()
reporterFinalise   = forall (m :: * -> *) a. Monad m => a -> m a
return (),
                                         reporterSimulate :: ExperimentData -> Composite ()
reporterSimulate   = forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Monoid a => a
mempty,
                                         reporterContext :: ExperimentContext (WebPageRenderer a)
reporterContext    = forall {a}. ExperimentContext (WebPageRenderer a)
context }
    in ExperimentGenerator { generateReporter :: Experiment
-> WebPageRenderer a
-> ExperimentEnvironment (WebPageRenderer a)
-> ExperimentMonad
     (WebPageRenderer a) (ExperimentReporter (WebPageRenderer a))
generateReporter = forall {p} {p} {a}.
Experiment
-> p
-> p
-> ExperimentWriter (ExperimentReporter (WebPageRenderer a))
reporter }
  
-- | The state of the view.
data ExperimentSpecsViewState =
  ExperimentSpecsViewState { ExperimentSpecsViewState -> ExperimentSpecsView
experimentSpecsView       :: ExperimentSpecsView,
                             ExperimentSpecsViewState -> Experiment
experimentSpecsExperiment :: Experiment }
  
-- | Create a new state of the view.
newExperimentSpecs :: ExperimentSpecsView -> Experiment -> ExperimentWriter ExperimentSpecsViewState
newExperimentSpecs :: ExperimentSpecsView
-> Experiment -> ExperimentWriter ExperimentSpecsViewState
newExperimentSpecs ExperimentSpecsView
view Experiment
exp =
  forall (m :: * -> *) a. Monad m => a -> m a
return ExperimentSpecsViewState { experimentSpecsView :: ExperimentSpecsView
experimentSpecsView       = ExperimentSpecsView
view,
                                    experimentSpecsExperiment :: Experiment
experimentSpecsExperiment = Experiment
exp }
       
-- | Get the HTML code.     
experimentSpecsHtml :: ExperimentSpecsViewState -> Int -> HtmlWriter ()     
experimentSpecsHtml :: ExperimentSpecsViewState -> Int -> HtmlWriter ()
experimentSpecsHtml ExperimentSpecsViewState
st Int
index =
  do ExperimentSpecsViewState -> Int -> HtmlWriter ()
header ExperimentSpecsViewState
st Int
index
     let writer :: ExperimentSpecsWriter
writer = ExperimentSpecsView -> ExperimentSpecsWriter
experimentSpecsWriter (ExperimentSpecsViewState -> ExperimentSpecsView
experimentSpecsView ExperimentSpecsViewState
st)
         write :: ExperimentSpecsWriter -> Experiment -> HtmlWriter ()
write  = ExperimentSpecsWriter
-> ExperimentSpecsWriter -> Experiment -> HtmlWriter ()
experimentSpecsWrite ExperimentSpecsWriter
writer
         exp :: Experiment
exp    = ExperimentSpecsViewState -> Experiment
experimentSpecsExperiment ExperimentSpecsViewState
st
     ExperimentSpecsWriter -> Experiment -> HtmlWriter ()
write ExperimentSpecsWriter
writer Experiment
exp

header :: ExperimentSpecsViewState -> Int -> HtmlWriter ()
header :: ExperimentSpecsViewState -> Int -> HtmlWriter ()
header ExperimentSpecsViewState
st Int
index =
  do String -> HtmlWriter () -> HtmlWriter ()
writeHtmlHeader3WithId (String
"id" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
index) forall a b. (a -> b) -> a -> b
$
       String -> HtmlWriter ()
writeHtmlText (ExperimentSpecsView -> String
experimentSpecsTitle forall a b. (a -> b) -> a -> b
$ ExperimentSpecsViewState -> ExperimentSpecsView
experimentSpecsView ExperimentSpecsViewState
st)
     let description :: String
description = ExperimentSpecsView -> String
experimentSpecsDescription forall a b. (a -> b) -> a -> b
$ ExperimentSpecsViewState -> ExperimentSpecsView
experimentSpecsView ExperimentSpecsViewState
st
     forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
description) forall a b. (a -> b) -> a -> b
$
       HtmlWriter () -> HtmlWriter ()
writeHtmlParagraph forall a b. (a -> b) -> a -> b
$
       String -> HtmlWriter ()
writeHtmlText String
description

-- | Get the TOC item     
experimentSpecsTOCHtml :: ExperimentSpecsViewState -> Int -> HtmlWriter ()
experimentSpecsTOCHtml :: ExperimentSpecsViewState -> Int -> HtmlWriter ()
experimentSpecsTOCHtml ExperimentSpecsViewState
st Int
index =
  HtmlWriter () -> HtmlWriter ()
writeHtmlListItem forall a b. (a -> b) -> a -> b
$
  String -> HtmlWriter () -> HtmlWriter ()
writeHtmlLink (String
"#id" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
index) forall a b. (a -> b) -> a -> b
$
  String -> HtmlWriter ()
writeHtmlText (ExperimentSpecsView -> String
experimentSpecsTitle forall a b. (a -> b) -> a -> b
$ ExperimentSpecsViewState -> ExperimentSpecsView
experimentSpecsView ExperimentSpecsViewState
st)