(ns auto-ap.views.components.modal
  (:require
   [auto-ap.status :as status]
   [auto-ap.views.utils :refer [appearing dispatch-event]]
   [re-frame.core :as re-frame]))

(re-frame/reg-sub
 ::modal-state
 (fn [db]
   (::state db)))

(re-frame/reg-event-db
 ::modal-requested
 (fn [db [_ state]]
   (assoc db ::state (assoc state :visible? true))))

(re-frame/reg-event-fx
 ::modal-closed
 (fn [{:keys [db]} [_ _]]
   (let [[_ status-id] (some-> db ::state :confirm :status-from )]
     (cond-> {:db (dissoc db ::state)}
       status-id (assoc :dispatch [::status/completed status-id])))))


(defn global-modal []
  (let [state (re-frame/subscribe [::modal-state])]
    (fn []
      (when (:visible? @state)
        (let [{:keys [title body status-from foot class cancel? confirm]} @state]
          [:div.modal.is-active (cond-> {}
                                  class (assoc :class class))
           [:div.modal-background {:on-click (dispatch-event [::modal-closed])}]
           
           [:div.modal-card (cond-> {}
                              class (assoc :class class))
            [:header.modal-card-head
             [:p.modal-card-title
              title]
             [:button.delete {:on-click (dispatch-event [::modal-closed])}]]
            [:section.modal-card-body
             body]
            (let [status (or (some-> confirm :status-from re-frame/subscribe deref )
                             (some-> status-from re-frame/subscribe deref ))
                  can-submit (if-let [can-submit (-> confirm :can-submit)]
                               @(re-frame/subscribe can-submit)
                               true)]
              (if foot
                [:footer.modal-card-foot
                 [appearing {:visible? (= :error (:state status))
                             :timeout 200
                             :enter-class "appear"
                             :exit-class "disappear"}
                  [:div.has-text-danger {:style {:flex-basis "100%"}} (:message (first (:error status)))] ]
                 foot]
                [:footer.modal-card-foot
                 [:div
                  [appearing {:visible? (= :error (:state status))
                              :timeout 200
                              :enter-class "appear"
                              :exit-class "disappear"}
                   [:div.has-text-danger {:style {:flex-basis "100%"}} (:message (first (:error status)))]]
                  
                  [:div.buttons
                   (when confirm
                     [:button.button {:class (conj  (status/class-for status) (:class confirm) )
                                      :disabled (or (status/disabled-for status)
                                                    (not can-submit))
                                      :on-click (:on-click confirm)}
                      (:value confirm)])
                   (when cancel?
                     [:button.button {:on-click (dispatch-event [::modal-closed] )} "Cancel"])]]]))]])))))

