module IHP.IDE.CodeGen.View.NewJob where

import IHP.ViewPrelude
import IHP.IDE.ToolServer.Types
import IHP.IDE.CodeGen.Types
import IHP.IDE.CodeGen.View.Generators (renderPlan)

data NewJobView = NewJobView
    { NewJobView -> Either Text [GeneratorAction]
plan :: Either Text [GeneratorAction]
    , NewJobView -> Text
jobName :: Text
    , NewJobView -> Text
applicationName :: Text
    , NewJobView -> [Text]
applications :: [Text]
    }

instance View NewJobView where
    html :: (?context::ControllerContext, ?view::NewJobView) =>
NewJobView -> Html
html NewJobView { [Text]
Either Text [GeneratorAction]
Text
$sel:plan:NewJobView :: NewJobView -> Either Text [GeneratorAction]
$sel:jobName:NewJobView :: NewJobView -> Text
$sel:applicationName:NewJobView :: NewJobView -> Text
$sel:applications:NewJobView :: NewJobView -> [Text]
plan :: Either Text [GeneratorAction]
jobName :: Text
applicationName :: Text
applications :: [Text]
.. } = [hsx|
        <div class="generators">
            {renderFlashMessages}
            <div class="container pt-5">
                <div class="code-generator new-script">
                    {if isEmpty jobName then renderEmpty else renderPreview}
                    {unless (isEmpty jobName) (renderPlan plan)}
                </div>
            </div>
        </div>
    |]
        where
            renderEmpty :: Html
renderEmpty = [hsx|
                <form method="POST" action={NewJobAction} class="d-flex">
                    {when (length applications /= 1) renderApplicationSelector}
                    <input
                        type="text"
                        name="name"
                        placeholder="Job name"
                        class="form-control"
                        autofocus="autofocus"
                        value={jobName}
                        />
                    <button class="btn btn-primary" type="submit">Preview</button>
                </form>
            |]
            renderApplicationOptions :: Html
renderApplicationOptions = [Text] -> (Text -> Html) -> Html
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Text]
applications (\Text
x -> [hsx|<option selected={x == applicationName}>{x}</option>|])
            renderApplicationSelector :: Html
renderApplicationSelector = [hsx|
                <select
                    name="applicationName"
                    class="form-control select2-simple"
                    size="1"
                >
                    {renderApplicationOptions}
                </select>|]
            renderPreview :: Html
renderPreview = [hsx|
                <form method="POST" action={CreateJobAction} class="d-flex">
                    <div class="object-name flex-grow-1">{applicationName}.Job.{jobName}</div>

                    <input type="hidden" name="name" value={jobName}/>
                    <input type="hidden" name="applicationName" value={applicationName}/>

                    <button class="btn btn-primary" type="submit">Generate</button>
                </form>
            |]