module IHP.IDE.SchemaDesigner.View.Schema.Code where

import IHP.ViewPrelude
import IHP.IDE.ToolServer.Types
import IHP.IDE.ToolServer.Routes ()

data CodeView = CodeView
    { CodeView -> Text
schema :: Text
    , CodeView -> Maybe ByteString
error :: Maybe ByteString
    }

instance View CodeView where
    html :: (?context::ControllerContext, ?view::CodeView) => CodeView -> Html
html CodeView { Maybe ByteString
Text
$sel:schema:CodeView :: CodeView -> Text
$sel:error:CodeView :: CodeView -> Maybe ByteString
schema :: Text
error :: Maybe ByteString
.. } = [hsx|
        <div class="editor-container h-100" style="position: relative">
            <div class="toolbox" style="position: absolute; top: 16px; right: 16px; z-index: 10000">
                <button id="save-button" class="btn btn-secondary" data-submit-url={submitUrl} style="transition: all 0.2s">Save</button>
            </div>
            <div id="editor" class="h-100">
                <div class="code-editor-container h-100"></div>
                <textarea class="source-code d-none">{preEscapedToHtml schema}</textarea>
            </div>
        </div>
        {errorDiv}
    |]
        where
            submitUrl :: Text
submitUrl = SchemaController -> Text
forall controller. HasPath controller => controller -> Text
pathTo SchemaController
SaveCodeAction
            errorDiv :: Html
errorDiv = case Maybe ByteString
error of
                Maybe ByteString
Nothing -> Html
forall a. Monoid a => a
mempty
                Just ByteString
error -> String -> Html
forall a. ToMarkup a => a -> Html
preEscapedToHtml [plain|
                        <div class="error-box">
                            <pre class="text-white p-5">#{error}</pre>
                        </div>
                    |]