(ns auto-ap.views.pages.pos.expected-deposits
  (:require [auto-ap.forms :as forms]
            [auto-ap.subs :as subs]
            [auto-ap.views.components.layouts :refer [side-bar-layout appearing-side-bar]]
            [auto-ap.views.pages.data-page :as data-page]
            [auto-ap.views.pages.pos.form :as form]
            [auto-ap.views.pages.pos.side-bar :as side-bar]
            [auto-ap.views.pages.pos.expected-deposits.table :as table]
            [auto-ap.views.utils :refer [with-user]]
            [clojure.set :as set]
            [re-frame.core :as re-frame]
            [reagent.core :as reagent]
            [vimsical.re-frame.fx.track :as track]))

(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 [[:expected_deposit_page
                                           (if (:exact-match-id params)
                                             {:exact-match-id (some-> (:exact-match-id params) str)
                                              :client-id      (:id @(re-frame/subscribe [::subs/client]))}
                                             {:start          (:start params 0)
                                              :sort           (:sort params)
                                              :per-page       (:per-page params)
                                              :exact-match-id (some-> (:exact-match-id params) str)
                                              :total-gte      (:amount-gte (:total-range params))
                                              :total-lte      (:amount-lte (:total-range params))
                                              :date-range     (:date-range params)
                                              :client-id      (:id @(re-frame/subscribe [::subs/client]))})
                                           [[:expected-deposits [:id :total :fee :location :date :status
                                                                 [:totals [:date :count :amount]]
                                                                 [:transaction [:id :date]]
                                                                 [:client [:name :id]]]]
                                            :total
                                            :start
                                            :end]]]} 
              :on-success (fn [result]
                            (let [result (set/rename-keys (:expected-deposit-page result)
                                                          {:expected-deposits :data})]
                              [::data-page/received ::page result]))}}))


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

(re-frame/reg-event-fx
 ::mounted
 (fn [_ _]
   {::track/register {:id ::params
                      :subscription [::data-page/params ::page]
                      :event-fn (fn [params]
                                  [::params-change params])}}))

(defn content []
  [:div
   [:h1.title "Expected Deposits"]
   [table/table {:id :expected-deposits
                 :data-page ::page}]])

(defn expected-deposits-page []
  (reagent/create-class
   {:display-name "expected-deposits-page"
    :component-will-unmount #(re-frame/dispatch-sync [::unmounted])
    :component-did-mount #(re-frame/dispatch [::mounted])
    :reagent-render
    (fn []
      (let [{form-active? :active?} @(re-frame/subscribe [::forms/form ::form/form])]
        [side-bar-layout {:side-bar [side-bar/side-bar {:data-page ::page}]
                          :main [content]
                          :right-side-bar [appearing-side-bar {:visible? form-active?}
                                           [form/form {}]]}]))}))

