module IHP.IDE.CodeGen.View.Generators where

import IHP.ViewPrelude
import IHP.IDE.ToolServer.Types
import IHP.IDE.ToolServer.Layout
import IHP.IDE.CodeGen.Types
import qualified Data.Text as Text
import IHP.IDE.ToolServer.Helper.View

data GeneratorsView = GeneratorsView

instance View GeneratorsView where
    html :: GeneratorsView -> Html
html GeneratorsView
GeneratorsView = [hsx|
        <div class="generators">
            <div class="container pt-5">
                {renderFlashMessages}
                <div class="generators-list">
                    {generator "Controller" (pathTo NewControllerAction) copyIcon}
                    {generator "Action" (pathTo NewActionAction) copyIcon}
                    {generator "View" (pathTo NewViewAction) copyIcon}
                    {generator "Mail" (pathTo NewMailAction) copyIcon}
                    {generator "Background Job" (pathTo NewJobAction) cogsIcon}
                    {generator "Script" (pathTo NewScriptAction) copyIcon}
                    {generator "Migration" (pathTo NewMigrationAction) dataIcon}
                    {generator "Application" (pathTo NewApplicationAction) copyIcon}
                </div>
            </div>
        </div>
    |]
        where
            generator :: Text -> Text -> Html -> Html
            generator :: Text
-> Text
-> ((?context::ControllerContext) => Html)
-> (?context::ControllerContext) => Html
generator Text
name Text
path (?context::ControllerContext) => Html
icon = [hsx|
                <a href={path} class="generator">
                    <div class="generator-icon">{icon}</div>
                    <div class="generator-name">{name}</div>
                </a>
            |]

renderPlan :: Either a mono -> Html
renderPlan (Left a
error) = [hsx|{error}|]
renderPlan (Right mono
actions) = [hsx|<div class="generator-actions">{forEach actions renderGeneratorAction}</div>|]

renderGeneratorAction :: GeneratorAction -> Html
renderGeneratorAction CreateFile { Text
$sel:fileContent:CreateFile :: GeneratorAction -> Text
$sel:filePath:CreateFile :: GeneratorAction -> Text
fileContent :: Text
filePath :: Text
.. } = [hsx|
    <div class="generator-action CreateFile">
        <div class="file-path">{filePath}</div>
        <div class="file-content">{Text.strip fileContent}</div>
    </div>
|]
renderGeneratorAction AppendToFile { Text
fileContent :: Text
filePath :: Text
$sel:fileContent:CreateFile :: GeneratorAction -> Text
$sel:filePath:CreateFile :: GeneratorAction -> Text
.. } = [hsx|
    <div class="generator-action AppendToFile">
        <div class="file-path">Append to {filePath}</div>
        <div class="file-content">{Text.strip fileContent}</div>
    </div>
|]
renderGeneratorAction AppendToMarker { Text
$sel:marker:CreateFile :: GeneratorAction -> Text
fileContent :: Text
filePath :: Text
marker :: Text
$sel:fileContent:CreateFile :: GeneratorAction -> Text
$sel:filePath:CreateFile :: GeneratorAction -> Text
.. } = [hsx|
    <div class="generator-action AppendToFile">
        <div class="file-path">Append to {filePath}</div>
        <div class="file-content">{Text.strip fileContent}</div>
    </div>
|]

renderGeneratorAction AddImport { Text
fileContent :: Text
filePath :: Text
$sel:fileContent:CreateFile :: GeneratorAction -> Text
$sel:filePath:CreateFile :: GeneratorAction -> Text
.. } = [hsx|
    <div class="generator-action AddImport">
        <div class="file-path">Append to {filePath}</div>
        <div class="file-content">{Text.strip fileContent}</div>
    </div>
|]

renderGeneratorAction AddAction { Text
fileContent :: Text
filePath :: Text
$sel:fileContent:CreateFile :: GeneratorAction -> Text
$sel:filePath:CreateFile :: GeneratorAction -> Text
.. } = [hsx|
    <div class="generator-action AddImport">
        <div class="file-path">Append to {filePath}</div>
        <div class="file-content">{Text.strip fileContent}</div>
    </div>
|]

renderGeneratorAction AddMountToFrontController { Text
$sel:applicationName:CreateFile :: GeneratorAction -> Text
applicationName :: Text
filePath :: Text
$sel:filePath:CreateFile :: GeneratorAction -> Text
.. } = [hsx|
    <div class="generator-action AddMountToFrontController">
        <div class="file-path">Mount to FrontController {filePath}</div>
        <div class="file-content">{Text.strip applicationName}</div>
    </div>
|]

renderGeneratorAction AddToDataConstructor { Text
$sel:dataConstructor:CreateFile :: GeneratorAction -> Text
fileContent :: Text
filePath :: Text
dataConstructor :: Text
$sel:fileContent:CreateFile :: GeneratorAction -> Text
$sel:filePath:CreateFile :: GeneratorAction -> Text
.. } = [hsx|
    <div class="generator-action AddToDataConstructor">
        <div class="file-path">Append to {filePath}</div>
        <div class="file-content">{Text.strip fileContent}</div>
    </div>
|]

renderGeneratorAction EnsureDirectory {} = Html
forall a. Monoid a => a
mempty
renderGeneratorAction RunShellCommand {} = Html
forall a. Monoid a => a
mempty