#lang racket/base

; Templates for the pages
(require
  "../webcontainer/weblets.rkt"
  "../webcontainer/weblet-parameter.rkt"
  "sitemap.rkt"
  racket/date
  )

(provide
  make-nav-header
  pages:template
  pages:adaptable-template)

; Make the header
(define (make-nav-header param)
  (define path (weblet-parameter-path param))
  `(header
    ; Current page
    (nav ,@(build-main-headers path))
    ; Child pages of current page
    (nav ,@(build-secondary-headers path))))

; Get the avatar icon for the author
(define (get-avatar author)
  (case author
    (("Feufochmar")
     "/images/feufochmar.png")
    (("feuforeve.fr")
     "/images/feuforeve.png")
    (("ArnYtron3000")
     "/images/arnytron3000.png")
    (("MechaMaskedOwl")
     "/images/mecha-masked-owl.png")
    (else
      #f)))

; Main template for pages
(define (pages:template
          #:title title ; Title
          #:content content ; Content, may be a function taking a weblet-parameter
          #:author author ; Author of the page
          #:date [date #f] ; Date
          #:stylesheets [stylesheets (list)] ; Stylesheets to use on the page
          #:scripts [scripts (list)] ; Scripts to use of the page
          #:on-load [on-load #f] ; the onload attribute of the body of the page
          #:error-code [error-code 200] ; Error code returned
          )
  (define avatar (get-avatar author))
  (html-page-weblet
    #:error-code error-code
    #:body
      (lambda (param)
        `(html
            (head
              (title ,title)
              (meta ((charset "UTF-8")))
              (link ((href "/css/feuforeve.css")(rel "stylesheet")(type "text/css")(media "all")))
              ,@(map
                (lambda (x)
                  `(link ((href ,x)(rel "stylesheet")(type "text/css")(media "all"))))
                stylesheets)
              ,@(map
                (lambda (x)
                  `(script ((src ,x)) " "))
                scripts)
              )
            (body (,@(if on-load `((onload ,on-load)) '()))
              ,(make-nav-header param)
              (main
                (div ((class "author-date"))
                  ,(if avatar `(span (img ((src ,avatar)))(br)) "")
                  (span ((class "author")) ,author) (br)
                  ,(if date date ""))
                (h1 ,title)
                (hr)
                ,(apply-or-return content param))
              (footer ,(string-append "©2015-" (number->string (date-year (current-date))) " Feufochmar"))
              )))))

; Adaptable template
; Similar to pages:template, but the argument is a function taking the weblet parameters and returning a hash containing the arguments for pages:template
(define (pages:adaptable-template content)
  (lambda (param)
    (define result (content param))
    ((pages:template
       #:title (hash-ref result 'title "Untitled page")
       #:content (hash-ref result 'content)
       #:author (hash-ref result 'author "feuforeve.fr")
       #:date (hash-ref result 'date #f)
       #:stylesheets (hash-ref result 'stylesheets (list))
       #:scripts (hash-ref result 'scripts (list))
       #:on-load (hash-ref result 'on-load #f)
       #:error-code (hash-ref result 'error-code 200))
     param)))
