module IHP.IDE.CodeGen.View.NewController where

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

data NewControllerView = NewControllerView
    { NewControllerView -> Either Text [GeneratorAction]
plan :: Either Text [GeneratorAction]
    , NewControllerView -> Text
controllerName :: Text
    , NewControllerView -> Text
applicationName :: Text
    , NewControllerView -> [Text]
applications :: [Text]
    , NewControllerView -> Bool
pagination :: Bool
    }

instance View NewControllerView where
    html :: NewControllerView -> Html
html NewControllerView { Bool
[Text]
Either Text [GeneratorAction]
Text
pagination :: Bool
applications :: [Text]
applicationName :: Text
controllerName :: Text
plan :: Either Text [GeneratorAction]
$sel:pagination:NewControllerView :: NewControllerView -> Bool
$sel:applications:NewControllerView :: NewControllerView -> [Text]
$sel:applicationName:NewControllerView :: NewControllerView -> Text
$sel:controllerName:NewControllerView :: NewControllerView -> Text
$sel:plan:NewControllerView :: NewControllerView -> Either Text [GeneratorAction]
.. } = [hsx|
        <div class="generators">
            {renderFlashMessages}
            <div class="container pt-5">
                <div class="code-generator new-controller">
                    {if isEmpty then renderEmpty else renderPreview}
                    {unless isEmpty (renderPlan plan)}
                </div>
            </div>
        </div>
    |]
        where
            renderEmpty :: Html
renderEmpty = [hsx|
                <form method="POST" action={NewControllerAction} class="d-flex">
                    {when (length applications /= 1) renderApplicationSelector}
                    <input
                        type="text"
                        name="name"
                        placeholder="Controller name"
                        class="form-control"
                        autofocus="autofocus"
                        value={controllerName}
                        />
                    <button class="btn btn-primary" type="submit">Preview</button>
                </form>|]

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

                    <input type="hidden" name="name" value={controllerName}/>
                    <input type="hidden" name="applicationName" value={applicationName}/>
                    <input type="hidden" name="pagination" value={inputValue pagination}/>

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

                <div class="generator-options">
                    <form method="POST" action={NewControllerAction}>
                        <input type="hidden" name="name" value={controllerName}/>
                        <input type="hidden" name="applicationName" value={applicationName}/>
                        <input
                            type="checkbox"
                            name="pagination"
                            id="pagination"
                            checked={pagination}
                            onchange="this.form.submit()"
                            />
                        <label class="pl-1" for="pagination">Pagination</label>
                    </form>
                </div>
            |]
            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>|]
            isEmpty :: Bool
isEmpty = Text -> Bool
forall mono. MonoFoldable mono => mono -> Bool
null Text
controllerName