module Bamboo.Theme.MiniHTML5.Helper where

import Hack
import Hack.Contrib.Utils
import MPS.Env
import MPS.Heavy (unescape_xml)
import Prelude ()

import Data.Default
import Hack.Contrib.Constants
import Hack
import Hack.Contrib.Response hiding (header)
import MPS (u2b)
import Data.ByteString.Lazy.Char8 (ByteString)
import Data.ByteString.Lazy.UTF8 (fromString)
import Text.XHtml.Strict (showHtmlFragment, HTML)
import qualified Text.HTML.Moe as M
import Text.HTML.Moe.Type (Attribute)

slashed_script_name :: Env -> String
slashed_script_name = script_name > ("/" /)

doc_type :: String
doc_type = "<!doctype html>"

xml_header :: String
xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"

home_nav :: String
home_nav    = "Home"

href :: String -> Attribute
href = u2b > escape_uri > M.href

html_response :: ByteString -> Response
html_response x = 
  def 
    .set_status 200
    .set_content_type _TextHtmlUTF8
    .set_body x

render_rss :: String -> ByteString
render_rss = (xml_header ++) > fromString

rss_response :: ByteString -> Response
rss_response = 
    html_response 
  > set_content_type "application/rss+xml"

show_html :: (HTML a) => a -> String
show_html = showHtmlFragment > unescape_xml



{-
import Bamboo.Env
import qualified Data.ByteString.Char8 as B
import Data.ByteString.UTF8 (fromString)
import Data.ByteString.Lazy (fromChunks)
import Data.Default
import Hack.Contrib.Constants
import Hack.Contrib.Response
import Text.XHtml.Strict hiding (p, meta, body)
import qualified Prelude as P
import qualified Text.XHtml.Strict as Html
import MPS.Heavy (unescape_xml)


id :: String -> HtmlAttr
id = identifier

css_link      :: String -> Html
js_link       :: String -> Html
js_src        :: String -> Html
rss_link      :: String -> Html
favicon_link  :: String -> Html

css_link l     = itag "link" ! [rel "stylesheet", thetype "text/css", href l]
js_link l      = itag "script" ! [thetype "text/javascript", src l]
js_src s       = tag "script" ! [thetype "text/javascript"] << s
favicon_link l = itag "link" ! [rel "icon", thetype "image/png", href l]

rss_link l = itag "link" ! 
  [rel "alternate", thetype "application/rss+xml", href l, title "RSS 2.0"]

div_id  :: String -> Html -> Html 
div_id s = thediv ! [id s]

div_class :: String -> Html -> Html
div_class s = thediv ! [theclass s]

div_class_id :: String -> String -> Html -> Html
div_class_id x y = thediv ! [theclass x, id y]

meta_tag :: Html
meta_tag  = Html.meta ! 
  [httpequiv "Content-Type", content "text/html; charset=utf-8"]

ie_tag  :: (Show a) => a -> Html
ie6_tag :: (Show a) => a -> Html
ie7_tag :: (Show a) => a -> Html

ie_tag x  = ("<!--[if IE]>" ++ x.show ++ "<![endif]-->").primHtml
ie6_tag x = ("<!--[if lt IE 7]>" ++ x.show ++ "<![endif]-->").primHtml
ie7_tag x = ("<!--[if IE 7]>" ++ x.show ++ "<![endif]-->").primHtml


xml_header :: String
xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"

doc_type :: String
doc_type = "<!doctype html>"

render_html :: Html -> B.ByteString
render_html = renderHtml > unescape_xml > fromString

output_html :: Html -> IO Response
output_html = render_html > output_plain_html

render_rss :: String -> B.ByteString
render_rss = (xml_header ++) > fromString

output_plain_html :: B.ByteString -> IO Response
output_plain_html x = 
  def 
    .set_status 200
    .set_body utf8
    .set_content_type _TextHtmlUTF8
    .return
  where
    utf8 = [x].fromChunks

output_plain_rss :: B.ByteString -> IO Response
output_plain_rss = output_plain_html
  > (^ set_content_type "application/rss+xml")
  

html_if :: Bool -> Html -> Html
html_if b x = if b then x else empty_html

-- html alias

img         :: Html
space_html  :: Html

img        = image
space_html = primHtml "&#160;"


span        :: Html -> Html
div         :: Html -> Html
d           :: Html -> Html
ul          :: Html -> Html

span       = thespan
div        = thediv
d          = div
ul         = ulist


klass :: String -> HtmlAttr
klass = theclass

c, i:: String -> Html -> Html
c x = d ! [klass x]
i x = d ! [id x]

ic, ci:: String -> String -> Html -> Html
ic x y = d ! [id x, klass y]
ci x y = ic y x

link :: String -> Html -> HotLink
link = hotlink

-}