module IHP.IDE.CodeGen.View.NewAction where

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

data NewActionView = NewActionView
    { NewActionView -> Either Text [GeneratorAction]
plan :: Either Text [GeneratorAction]
    , NewActionView -> Text
actionName :: Text
    , NewActionView -> Text
controllerName :: Text
    , NewActionView -> Text
applicationName :: Text
    , NewActionView -> Bool
doGenerateView :: Bool
    , NewActionView -> [Text]
controllers :: [Text]
    , NewActionView -> [Text]
applications :: [Text]
    }

instance View NewActionView where
    html :: NewActionView -> Html
html NewActionView { Bool
[Text]
Either Text [GeneratorAction]
Text
applications :: [Text]
controllers :: [Text]
doGenerateView :: Bool
applicationName :: Text
controllerName :: Text
actionName :: Text
plan :: Either Text [GeneratorAction]
$sel:applications:NewActionView :: NewActionView -> [Text]
$sel:controllers:NewActionView :: NewActionView -> [Text]
$sel:doGenerateView:NewActionView :: NewActionView -> Bool
$sel:applicationName:NewActionView :: NewActionView -> Text
$sel:controllerName:NewActionView :: NewActionView -> Text
$sel:actionName:NewActionView :: NewActionView -> Text
$sel:plan:NewActionView :: NewActionView -> Either Text [GeneratorAction]
.. } = [hsx|
        <div class="generators">
            <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={NewActionAction}>
                    <div 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="Action name"
                            class="form-control"
                            autofocus="autofocus"
                            value={actionName}
                            />
                        <button class="btn btn-primary" type="submit">Preview</button>
                    </div>
                    <div class="generator-options">
                        <input
                            type="checkbox"
                            name="doGenerateView"
                            id="doGenerateView"
                            />
                        <label class="pl-1" for="doGenerateView">With View</label>
                    </div>
                </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={CreateActionAction} class="d-flex">
                    <div class="object-name flex-grow-1">{controllerName}.{actionName}</div>

                    <input type="hidden" name="name" value={actionName}/>
                    <input type="hidden" name="controllerName" value={controllerName}/>
                    <input type="hidden" name="applicationName" value={applicationName}/>
                    <input type="hidden" name="doGenerateView" value={(if doGenerateView then "on" else "off") :: Text}/>

                    <button class="btn btn-primary" type="submit">Generate</button>
                </form>
                <div class="generator-options">
                    <form method="POST" action={NewActionAction}>
                        <input
                            type="checkbox"
                            name="doGenerateView"
                            id="doGenerateView"
                            checked = {doGenerateView}
                            onchange = "this.form.submit()"
                            />
                        <label class="pl-1" for="doGenerateView">With View</label>
                        <input type="hidden" name="name" value={actionName}/>
                        <input type="hidden" name="controllerName" value={controllerName}/>
                    </form>
                </div>
            |]

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