(ns auto-ap.views.pages.admin.vendors.merge-dialog
  (:require [auto-ap.forms :as forms]
            [auto-ap.status :as status]
            [auto-ap.subs :as subs]
            [auto-ap.views.components.modal :as modal]
            [auto-ap.views.components.typeahead :refer [typeahead-v3]]
            [auto-ap.views.utils :refer [dispatch-event]]
            [re-frame.core :as re-frame]))

(re-frame/reg-sub
 ::can-submit
 :<- [::forms/form ::form]
 (fn [{:keys [data]}]
   (println data)
   (and (:from data)
        (:to data))))

(def merge-form (forms/vertical-form {:submit-event [::save]
                                      :change-event [::forms/change ::form]
                                      :can-submit [::can-submit]
                                      :id ::form}))

(defn form []
  (let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
        {:keys [form-inline horizontal-field field raw-field error-notification submit-button]} merge-form]
    

    (form-inline {}
                 [:<>
                  (field "Form Vendor (will be deleted)"
                         [typeahead-v3 {:entities-by-id @(re-frame/subscribe [::subs/vendors-by-id])
                                        :entity-index @(re-frame/subscribe [::subs/all-vendors-index])
                                        :type "typeahead-v3"
                                        :auto-focus true
                                        :entity->text (fn [x]
                                                       (str (:name x)  "  (" (reduce + 0 (map :count (:usage x))) " usages)") )
                                        :field [:from]}])


                  (field "To Vendor"
                         [typeahead-v3 {:entities-by-id @(re-frame/subscribe [::subs/vendors-by-id])
                                        :entity-index @(re-frame/subscribe [::subs/searchable-vendors-index])
                                        :type "typeahead-v3"
                                        :entity->text (fn [x]
                                                       (str (:name x)  "  (" (reduce + 0 (map :count (:usage x))) " usages)") )
                                        :field [:to]}])])))

(re-frame/reg-event-fx
 ::show
 (fn  [{:keys [db]} _]
   {:dispatch [::modal/modal-requested {:title "Merge Vendors"
                                        :body [form]
                                        :confirm {:value "Merge"
                                                  :status-from [::status/single ::form]
                                                  :class "is-primary"
                                                  :on-click (dispatch-event [::save])
                                                  :can-submit [::can-submit]
                                                  :close-event [::status/completed ::form]}}]
    :db (forms/start-form db ::form {})}
   ))

(re-frame/reg-event-fx
 ::complete
 (fn  [{:keys [db]} _]
   {:db (forms/stop-form db ::form)
    :dispatch [::modal/modal-closed ]}))

(re-frame/reg-event-fx
 ::save
 [(forms/in-form ::form)]
 (fn  [{{{:keys [from to]} :data :as merge-vendors-form} :db :as g} _]
   (let [user @(re-frame/subscribe [::subs/token])]
     {:graphql
      {:token user
       :owns-state {:single ::form}
       :query-obj {:venia/operation {:operation/type :mutation
                                     :operation/name "MergeVendors"}
                   :venia/queries [{:query/data [:merge-vendors
                                                 {:from (:id from) :to (:id to)} []]}]}
       :on-success [::complete]}})))

