(use-modules (alterator gettext)
             (alterator http template)
             (alterator http html)

             (alterator configd woo)
             (alterator configd form)
             (alterator configd html)
             (alterator configd constraints)
             (alterator configd frontend))

;;possible modes: new,delete,edit,view
;;object variable defines object name
(define (get-command url url-args)
  (cond
   ((assoc "card-index-new" url-args) 'new)
   ((assoc "card-index-delete" url-args) 'delete)
   ((assoc "card-index-apply" url-args) 'apply)
   ((assoc "card-index-apply-main" url-args) 'apply-main)
   ((assoc "card-index-cancel" url-args) 'cancel)
   (else 'view)))

(define (woo-read-first/protected url)
  (or (woo-catch
       (thunk (woo-read-first url))
       (lambda reason #f))
      '(list url)))

(define (get-current url)
  (woo-get-option (woo-read-first/protected url)
                  'name
                  #f))

(define (make-constraints action args)
  (or (cond-plistq 'async (cdr args))
      (apply fill-constraints action args)))

(define (first-object objects)
  (let ((item (cond-car objects)))
    (and item (html:select-option-value item))))

(define (card-index url url-args template-args)
  (let* ((template-url (design-path (cond-assoc 'url template-args "form-unknown.html")))
         (command (get-command url url-args))
         (current (or (cond-assoc "name" url-args) (get-current url)))
         (async (cond-assoc "async" url-args))
         (args (car (woo-args url url-args)))
         (_ (make-translator "alterator-fbi" (cond-assoc "language" url-args '("en_US")))))

    (case command
      ((view cancel)
       (let* ((objects (woo-list url))
              (current (or (and (eq? command 'view ) current) (first-object objects)))
              (sub-url (and current (string-append url "/" current)))
              (setup-constraints (make-constraints "write" args)))
         (template template-url
                   setup-constraints ;;for HEAD
                   ;;selector tuning
                   (tag: "form"
                         (@ 'class "selector-chooser")
                         (lambda (options content)
                           (scm-filter
                            content
                            (make-cb
                             setup-constraints ;;for LABEL's
                             (if current
                                 (list
                                  (replace-tag: "select"
                                                (@ 'name "name")
                                                (lambda(options content)
                                                  `(select ,@options
                                                           ,(html:select-options current objects))))
                                  (apply fill-form args))
                                 (list
                                  (replace-tag: "select"
                                                (@ 'name "name") "")
                                  (replace-tag: "input"
                                                (@ 'name "card-index-delete") "")))))))

                   ;;object tuning
                   (tag: "form"
                         (@ 'class "selector-data")
                         (lambda (options content)
                           (if current
                               (scm-filter
                                content
                                (make-cb
                                 setup-constraints ;;for LABEL's
                                 (tag: "div"
                                       (@ 'class "selector-name")
                                       (html:hidden 'name current)
                                       (html:hidden 'card-index-mode "write"))
                                 (apply fill-form sub-url (cdr args))))
                               (_ "No objects found...")))) )))
      ((new)
       (let ((setup-constraints (make-constraints "new" args)))
         (template template-url
                   setup-constraints ;;for HEAD
                   ;;selector tuning
                   (tag: "form"
                         (@ 'class "selector-chooser")
                         (html:submit 'card-index-cancel
                                      (_ "Return to view")))
                   ;;object tuning
                   (tag: "form"
                         (@ 'class "selector-data")
                         (lambda (options content)
                           (scm-filter
                            content
                            (make-cb
                             setup-constraints ;;for LABEL's
                             (tag: "div"
                                   (@ 'class "selector-name")
                                   (@ 'template-operation 'append-content)
                                   (html:hidden 'card-index-mode "new"))
                             (apply new-form args))))))))

      ((apply apply-main)
       (let* ((mode (cond-assoc "card-index-mode" url-args "write"))
              (delete-mode (string=? mode "delete"))
              (new-mode (string=? mode "new"))
              (object-name (or (and (eq? command 'apply) current) ""))
              (redirect (cond-assq 'redirect template-args #f))
              (new-url-args (acons "language"
                                   (cond-assoc "language" url-args '("en_US"))
                                   '())))
         (for-each
          (lambda(cmd)
            (apply (if delete-mode woo-try woo-try/constraints)
                   mode
                   (if (and new-mode (string=? (car cmd) ""))
                       url
                       (string-append url "/" object-name))
                   (cdr cmd))
            )
          (woo-args "" url-args))
         (if (string? redirect)
             (html:redirect redirect)
             (card-index url
                         (if (not delete-mode)
                             (acons "name" current new-url-args)
                             new-url-args)
                         template-args))))
      ((delete) (template template-url
                          (replace-tag: "form"
                                        (@ 'class "selector-chooser") "")
                          (tag: "form"
                                (@ 'class "selector-data")
                                (html:hidden 'name current)
                                (html:hidden 'card-index-mode "delete")
                                (html: "p" (_ "Do you really want to delete ") current "?")
                                (html:submit 'card-index-cancel (_ "No"))
				(html: "&" "nbsp")
                                (html:submit 'card-index-apply (_ "Yes")))))
      (else
       (template template-url
                 (tag: "body"
                       (_ "Unsupported mode") (->string command)))))))

(lambda (self objects options)
  (let ((url (string-append "/" (string-join objects "/")))
        (url-args (cond-plistq 'url-args options))
        (template-args (cond-plistq 'template-args options)))
    (list
     'scm
     (woo-catch
      (thunk (card-index url url-args template-args))
      (lambda(reason)
        (html:error (card-index url
                                (list (or (assoc "name" url-args) (cons "name" #f)))
                                template-args)
                    reason))))))
