module IHP.IDE.CodeGen.View.NewScript where

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

data NewScriptView = NewScriptView
    { NewScriptView -> Either Text [GeneratorAction]
plan :: Either Text [GeneratorAction]
    , NewScriptView -> Text
scriptName :: Text
    }

instance View NewScriptView where
    html :: NewScriptView -> Html
html NewScriptView { Either Text [GeneratorAction]
Text
scriptName :: Text
plan :: Either Text [GeneratorAction]
$sel:scriptName:NewScriptView :: NewScriptView -> Text
$sel:plan:NewScriptView :: NewScriptView -> Either Text [GeneratorAction]
.. } = [hsx|
        <div class="generators">
            {renderFlashMessages}
            <div class="container pt-5">
                <div class="code-generator new-script">
                    {if isEmpty then renderEmpty else renderPreview}
                    {unless isEmpty (renderPlan plan)}
                </div>
            </div>
        </div>
    |]
        where
            renderEmpty :: Html
renderEmpty = [hsx|<form method="POST" action={NewScriptAction} class="d-flex">
                    <input
                        type="text"
                        name="name"
                        placeholder="Script name"
                        class="form-control"
                        autofocus="autofocus"
                        value={scriptName}
                        />

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

            renderPreview :: Html
renderPreview = [hsx|
                <form method="POST" action={CreateScriptAction} class="d-flex">
                    <div class="object-name flex-grow-1">{scriptName}</div>

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

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


            isEmpty :: Bool
isEmpty = Text -> Bool
forall mono. MonoFoldable mono => mono -> Bool
null Text
scriptName