module Handler.Workflow.Instance.New
  ( getAdminWorkflowInstanceNewR, postAdminWorkflowInstanceNewR
  , adminWorkflowInstanceNewR
  , getGlobalWorkflowInstanceNewR, postGlobalWorkflowInstanceNewR
  , getSchoolWorkflowInstanceNewR, postSchoolWorkflowInstanceNewR
  , workflowInstanceNewR
  ) where

import Import
import Handler.Utils
import Handler.Utils.Workflow.Form
import Utils.Workflow

import Handler.Workflow.Instance.Form

getAdminWorkflowInstanceNewR, postAdminWorkflowInstanceNewR :: Handler Html
getAdminWorkflowInstanceNewR = postAdminWorkflowInstanceNewR
postAdminWorkflowInstanceNewR = adminWorkflowInstanceNewR Nothing

adminWorkflowInstanceNewR :: Maybe WorkflowDefinitionId -> Handler Html
adminWorkflowInstanceNewR wdId = do
  cRoute <- getCurrentRoute
  (((_, instForm), instEncoding), act) <- runDB $ do
    form@((instRes, _), _) <- runFormPost $ workflowInstanceForm wdId Nothing

    act <- formResultMaybe instRes $ \WorkflowInstanceForm{..} -> do
      wifGraph' <- fromWorkflowGraphForm wifGraph
      workflowInstanceGraph <- insertSharedWorkflowGraph wifGraph'
      let wifScope' = wifScope
            & over _wisTerm   unTermKey
            & over _wisSchool unSchoolKey
            & over _wisCourse (view _SqlKey)
      instId <- insertUnique WorkflowInstance
        { workflowInstanceDefinition = wdId
        , workflowInstanceGraph
        , workflowInstanceScope = wifScope'
        , workflowInstanceName = wifName
        , workflowInstanceCategory = wifCategory
        }

      for_ instId $ \instId' -> iforM_ wifDescriptions $ \widLang (widTitle, widDesc) ->
        insert WorkflowInstanceDescription
          { workflowInstanceDescriptionInstance = instId'
          , workflowInstanceDescriptionLanguage = widLang
          , workflowInstanceDescriptionTitle = widTitle
          , workflowInstanceDescriptionDescription = widDesc
          }

      return . Just $ case instId of
        Nothing -> addMessageI Error MsgWorkflowInstanceCollision
        Just _
          | is _Just wdId -> do
              addMessageI Success MsgWorkflowDefinitionInstantiated
              redirect AdminWorkflowInstanceListR
          | otherwise -> do
              addMessageI Success MsgWorkflowInstanceCreated
              redirect AdminWorkflowInstanceListR
    
    return (form, act)

  forM_ act id

  let instWidget = wrapForm instForm def
        { formAction = SomeRoute <$> cRoute
        , formEncoding = instEncoding
        }

  siteLayoutMsg MsgWorkflowDefinitionInstantiateTitle $ do
    setTitleI MsgWorkflowDefinitionInstantiateTitle

    instWidget


getGlobalWorkflowInstanceNewR, postGlobalWorkflowInstanceNewR :: Handler Html
getGlobalWorkflowInstanceNewR = postGlobalWorkflowInstanceNewR
postGlobalWorkflowInstanceNewR = workflowInstanceNewR WSGlobal

getSchoolWorkflowInstanceNewR, postSchoolWorkflowInstanceNewR :: SchoolId -> Handler Html
getSchoolWorkflowInstanceNewR = postSchoolWorkflowInstanceNewR
postSchoolWorkflowInstanceNewR = workflowInstanceNewR . WSSchool

workflowInstanceNewR :: RouteWorkflowScope -> Handler Html
workflowInstanceNewR = error "not implemented"
