#lang racket

(require net/url
         json
         "mastodon.rkt"
         "configuration.rkt"
         srfi/8)

(define character-generator (string-append feuforeve:website "/Floraverse/CharacterGenerator/Tweet"))
(define (pick-character)
  (read-json
    (get-pure-port
      (string->url character-generator))))

; Function to generate a character
; The concatenation of strings should be less than 500 characters (mastodon limit)
; The function generates characters as long as the limits are not respected
; The function returns two values: a list of strings for twitter and a single string for mastodon
(define (generate-messages)
  (define chr (pick-character))
  (define tweets (list
                   (hash-ref chr 'identity)
                   (hash-ref chr 'birth)
                   (hash-ref chr 'motto)
                   (hash-ref chr 'traits)))
  (define toot (string-join tweets "\n"))
  (if (>= (string-length toot) 500)
      (generate-messages)
      toot))

; Post a character
; Send to mastodon
(send
  (new mastodon-client%
      [instance floraverse:mastodon-instance]
      [authorization-bearer floraverse:mastodon-authorization-bearer])
  new-status
  (generate-messages)
  #:visibility "public")
