(ns auto-ap.views.pages.admin.rules
  (:require [auto-ap.forms :as forms]
            [auto-ap.subs :as subs]
            [auto-ap.views.components.admin.side-bar :refer [admin-side-bar]]
            [auto-ap.views.pages.admin.rules.side-bar :as side-bar]
            [auto-ap.views.components.layouts :refer [appearing-side-bar side-bar-layout]]
            [auto-ap.views.pages.admin.rules.table :as table]
            [auto-ap.views.pages.admin.rules.results-modal :as results-modal]
            [auto-ap.views.pages.admin.rules.form :as form]
            [auto-ap.views.pages.admin.rules.common :refer [default-read]]
            [auto-ap.views.utils :refer [dispatch-event with-user]]
            [vimsical.re-frame.cofx.inject :as inject]
            [vimsical.re-frame.fx.track :as track]
            [auto-ap.events :as events]
            [auto-ap.utils :refer [replace-by merge-by]]
            [re-frame.core :as re-frame]
            [auto-ap.status :as status]
            [auto-ap.effects.forward :as forward]
            [auto-ap.views.pages.data-page :as data-page]
            [clojure.set :as set]
            [auto-ap.views.components.buttons :as buttons]))



;; EVENTS



(re-frame/reg-event-fx
 ::params-change
 [with-user ]
 (fn [{:keys [db user] :as cofx} [_ params]]
   {:graphql {:token user
              :owns-state {:single [::data-page/page ::page]}
              :query-obj {:venia/queries [{:query/data [:transaction_rule_page
                                                        {:sort (:sort params)
                                                         :start (:start params 0)
                                                         :per-page (:per-page params)
                                                         :vendor-id (:id (:vendor params))
                                                         :client-id (:id @(re-frame/subscribe [::subs/client]))
                                                         :note (:note params)
                                                         :description (:description params)}
                                                        [[:transaction-rules default-read]
                                                         :total
                                                         :start
                                                         :end]]
                                           :query/alias :result}]} 
              :on-success (fn [result]
                            [::data-page/received ::page
                             (set/rename-keys (:result result)
                                              {:transaction-rules :data})])}}))

(re-frame/reg-event-fx
 ::new-rule-clicked
 (fn [{:keys [db]} _]
   {:dispatch [::form/adding {:client @(re-frame/subscribe [::subs/client])}]}))


(re-frame/reg-event-fx
 ::mounted
 (fn [{:keys [db]}]
   {:dispatch-n [[::events/yodlee-merchants-needed]]
    ::forward/register [{:id ::page
                         :events #{::form/updated}
                         :event-fn (fn [[_ result]]
                                     [::data-page/updated-entity ::page result])}
                        {:id ::deleted-transaction-rule
                         :events #{::table/deleted-transaction-rule }
                         :event-fn (fn [[_ result]]
                                     [::data-page/deleted-entity ::page result])}]
    ::track/register {:id ::params
                      :subscription [::data-page/params ::page]
                      :event-fn (fn [params]
                                  [::params-change params])}}))

(re-frame/reg-event-fx
 ::unmounted
 (fn [{:keys [db]}]
   {:dispatch [::data-page/dispose ::page]
    ::forward/dispose [{:id ::page}
                       {:id ::deleted-transaction-rule}]
    ::track/dispose {:id ::params}}))

;; VIEWS
(def rules-content
  (with-meta
    (fn []
      (let [current-client @(re-frame/subscribe [::subs/client])
            user @(re-frame/subscribe [::subs/user])]
        [:div 
         [:h1.title "Transaction Rules"]
         (when (= "admin" (:user/role user))
           [:div.is-pulled-right
            [buttons/new-button {:event [::new-rule-clicked]
                                 :class "is-primary"
                                 :name "Transaction Rule"}]])
         [table/table {:id :transaction-rules
                       :data-page ::page}]]))
    {:component-did-mount (dispatch-event [::mounted ])
     :component-will-unmount #(re-frame/dispatch-sync [::unmounted])}))

(defn admin-rules-page []
  (let [{:keys [active?]} @(re-frame/subscribe [::forms/form ::form/form])]
    [side-bar-layout {:side-bar [admin-side-bar {}
                                 [side-bar/rule-side-bar {:data-page ::page}]]
                      :main [rules-content]
                      :right-side-bar [appearing-side-bar {:visible? active?}
                                       [form/form {:data-page ::page}]]}]))
