#lang racket/base

; ArnYtron3000 pages
(require
  json
  racket/date
  "templates.rkt"
  "../generators/quotations.rkt"
  "../webcontainer/weblets.rkt"
  "../webcontainer/weblet-parameter.rkt")

(provide
  pages:arnytron
  pages:arnytron-raw
  pages:arnytron-json
  pages:arnytron-about
  pages:arnytron-real)

; The generator
(define *ArnYtron3000* (jsexpr->quotation-generator (read-json (open-input-file "static/data/greetings.json"))))

; Main page
(define pages:arnytron
  (pages:template
    #:title "ArnYtron3000"
    #:author "ArnYtron3000"
    #:content
      (lambda (param)
        `(article
          (p "Aujourd'hui, ArnYtron3000 vous salue par ces mots :" (br)
             (strong ,(quotation-quote (quotation-generate *ArnYtron3000*)))
             )))))

; Raw output
(define pages:arnytron-raw
  (raw-data-weblet
    #:body
      (lambda (param)
        (string->bytes/utf-8 (string-append (quotation-quote (quotation-generate *ArnYtron3000*)) "\n")))))

; Json output
(define pages:arnytron-json
  (raw-data-weblet
    #:content-type #"application/json;charset=utf-8"
    #:body
      (lambda (param)
        (jsexpr->bytes
          (quotation->jsexpr
            (quotation-generate
              *ArnYtron3000*
              #:date (parameterize ((date-display-format 'iso-8601)) (date->string (current-date)))
              #:author "ArnYtron3000"
              ))))))

; About page
(define pages:arnytron-about
  (pages:template
    #:title "À propos d'ArnYtron3000"
    #:author "Feufochmar"
    #:date "2019-11-11"
    #:content
      '(article
        (p "ArnYtron3000, un générateur " (del "d'insultes")
           " de salutations se basant sur les légendaires salutations "
           "postées par " (a ((href "http://twitter.com/ArnY")) "ArnY") " sur le canal IRC #linux de "
           (a ((href "https://www.rezosup.org/")) "RezoSup") "."))))

; True quotations
(define pages:arnytron-real
  (pages:template
    #:title "Citation d'ArnY"
    #:author "ArnY"
    #:content
      (lambda (param)
        (let* ((date (weblet-parameter-ref param 'date #f))
               (quot (quotation-get-sample *ArnYtron3000* #:date date))
              )
          `(article
            ,(if quot
                `(p "Le " ,(quotation-date quot) ", " ,(quotation-author quot) " a salué #linux en disant..." (br)
                    (strong ,(quotation-quote quot)))
                (let ((other-quot (quotation-get-sample *ArnYtron3000*)))
                  `(p "Je ne sais pas si " ,(quotation-author other-quot) " a salué #linux le " ,date ", "
                      "mais le " ,(quotation-date other-quot) ", " ,(quotation-author other-quot) " a salué #linux en disant..." (br)
                      (strong ,(quotation-quote other-quot)))
                  ))
            )))))
