(ns auto-ap.views.pages.admin.vendors
  (:require
   [auto-ap.effects.forward :as forward]
   [auto-ap.subs :as subs]
   [auto-ap.shared-views.admin.side-bar :refer [admin-side-bar]]
   [auto-ap.views.components.layouts :refer [side-bar-layout]]
   [auto-ap.views.pages.admin.vendors.merge-dialog :as merge-dialog]
   [auto-ap.views.pages.admin.vendors.side-bar :as side-bar]
   [auto-ap.views.pages.admin.vendors.table :as table]
   [auto-ap.views.pages.data-page :as data-page]
   [auto-ap.views.utils :refer [dispatch-event with-user]]
   [clojure.set :as set]
   [re-frame.core :as re-frame]
   [vimsical.re-frame.fx.track :as track]
   [auto-ap.views.components.vendor-dialog :as vendor-dialog]))

(def default-read [:id :name :hidden :terms [:default-account [:name :id :location]]
                   [:account-overrides [[:client [:id :name]] :id [:account [:id :numeric-code :name]]]]
                   [:automatically-paid-when-due [:id :name]]
                   [:terms-overrides [[:client [:id :name]] :id :terms]]
                   [:schedule-payment-dom [[:client [:id :name]] :id :dom]]
                   [:usage [:client-id :count]]
                   [:primary-contact [:name :phone :email :id]]
                   [:secondary-contact [:id :name :phone :email]]
                   :print-as :invoice-reminder-schedule :code
                   :legal-entity-name
                   :legal-entity-first-name :legal-entity-middle-name :legal-entity-last-name
                   :legal-entity-tin :legal-entity-tin-type
                   :legal-entity-1099-type
                   [:address [:street1 :street2 :city :state :zip]]])

(re-frame/reg-event-fx
 ::params-change
 [with-user]
 (fn [{:keys [user]} [_ params]]
   {:graphql {:token user
              :owns-state {:single [::data-page/page ::page]}
              :query-obj {:venia/queries [{:query/data [:vendor
                                                        {:sort      (:sort params)
                                                         :start     (:start params 0)
                                                         :per-page  (:per-page params)
                                                         :name-like (:name-like params)}
                                                        [[:vendors default-read]
                                                         :total
                                                         :start
                                                         :end]]
                                           :query/alias :result}]}
              :on-success (fn [result]
                            [::data-page/received ::page
                             (set/rename-keys (:result result)
                                              {:vendors :data})])}}))

(re-frame/reg-event-fx
  ::mounted
  (fn  [_ _]
    {::forward/register [{:id       ::merge-complete
                          :events   #{::merge-dialog/complete}
                          :event-fn (fn [_]
                                      [::params-change {}])}
                         {:id       ::save-complete
                          :events   #{::vendor-dialog/save-complete}
                          :event-fn (fn [_]
                                      [::params-change {}])}]
     ::track/register   {:id           ::params
                         :subscription [::data-page/params ::page]
                         :event-fn     (fn [params]
                                         [::params-change params])}}))

(re-frame/reg-event-fx
  ::unmounted
  (fn [_ _]
    {:dispatch [::data-page/dispose ::page]
     ::forward/dispose [{:id ::merge-complete} {:id ::save-complete}]
     ::track/dispose   {:id ::params}}))

(defn admin-vendors-content []
  [(with-meta
     (fn []
       [:div.inbox-messages
        (when-let [banner (:banner @(re-frame/subscribe [::subs/admin]))]
          [:div.notification banner])
        [:div
         [:h1.title "Vendors"]
         [:div.is-pulled-right [:a.button.is-primary.is-outlined {:on-click (dispatch-event [::merge-dialog/show])} "Merge vendors"]]
         [table/vendors-table {:id :vendors
                               :data-page ::page}]]])
     {:component-did-mount #(re-frame/dispatch [::mounted])
      :component-will-unmount #(re-frame/dispatch-sync [::unmounted])})])

(defn admin-vendors-page []
  [side-bar-layout {:side-bar [admin-side-bar {}
                               [side-bar/vendor-side-bar {:data-page ::page}]]
                    :main [admin-vendors-content]}])
