module IHP.IDE.CodeGen.View.NewMail where

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

data NewMailView = NewMailView
    { NewMailView -> Either Text [GeneratorAction]
plan :: Either Text [GeneratorAction]
    , NewMailView -> Text
mailName :: Text
    , NewMailView -> Text
controllerName :: Text
    , NewMailView -> Text
applicationName :: Text
    , NewMailView -> [Text]
controllers :: [Text]
    , NewMailView -> [Text]
applications :: [Text]
    }

instance View NewMailView where
    html :: NewMailView -> Html
html NewMailView { [Text]
Either Text [GeneratorAction]
Text
applications :: [Text]
controllers :: [Text]
applicationName :: Text
controllerName :: Text
mailName :: Text
plan :: Either Text [GeneratorAction]
$sel:applications:NewMailView :: NewMailView -> [Text]
$sel:controllers:NewMailView :: NewMailView -> [Text]
$sel:applicationName:NewMailView :: NewMailView -> Text
$sel:controllerName:NewMailView :: NewMailView -> Text
$sel:mailName:NewMailView :: NewMailView -> Text
$sel:plan:NewMailView :: NewMailView -> 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={NewMailAction} class="d-flex">
                    {when (length applications /= 1) renderApplicationSelector}
                    <select 
                        name="controllerName"
                        class="form-control select2-simple"
                        size="1"
                    >
                        {renderControllerOptions}
                    </select>
                    <input
                        type="text"
                        name="name"
                        placeholder="Mail name"
                        class="form-control"
                        autofocus="autofocus"
                        value={mailName}
                        />
                    <button class="btn btn-primary" type="submit">Preview</button>
                </form>|]
            renderControllerOptions :: Html
renderControllerOptions = [Text] -> (Text -> Html) -> Html
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Text]
controllers (\Text
x -> [hsx|<option>{x}</option>|])
            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={CreateMailAction} class="d-flex">
                    <div class="object-name flex-grow-1">{controllerName}.Mail.{mailName}</div>

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

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

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