(ns auto-ap.views.pages.payments
  (:require [re-frame.core :as re-frame]
            [auto-ap.entities.clients :as client]
            [auto-ap.entities.vendors :as vendor]
            [reagent.core :as reagent]
            [goog.string :as gstring]
            [clojure.spec.alpha :as s]
            [vimsical.re-frame.cofx.inject :as inject]
            [auto-ap.views.components.sorter :refer [sorted-column]]
            [auto-ap.views.components.date-range-filter :refer [date-range-filter]]
            [auto-ap.views.components.layouts :refer [side-bar-layout]]
            [auto-ap.views.components.paginator :refer [paginator]]
            [auto-ap.views.components.sort-by-list :refer [sort-by-list]]
            [auto-ap.events :as events]
            [auto-ap.views.utils :refer [dispatch-event date->str bind-field nf with-user]]
            [auto-ap.utils :refer [by]]
            [auto-ap.views.pages.payments.side-bar :as side-bar]
            [auto-ap.views.pages.payments.table :as table]
            [auto-ap.views.pages.check :as check]
            [auto-ap.subs :as subs]
            [auto-ap.status :as status]
            [vimsical.re-frame.fx.track :as track]
            [auto-ap.effects.forward :as forward]
            [auto-ap.views.pages.data-page :as data-page]
            [clojure.set :as set]))


(re-frame/reg-event-fx
 ::params-change
 [with-user]
 (fn [{:keys [user db ]}[_ params]]
   {:graphql {:token user
              :owns-state {:single [::data-page/page ::page]}
              :query-obj {:venia/queries [[:payment_page
                                           {:start (:start params 0)
                                            :per-page (:per-page params)
                                            :sort (:sort params)
                                            :client-id (:id @(re-frame/subscribe [::subs/client]))
                                            :vendor-id (:id (:vendor params))
                                            :payment-type (:payment-type params)
                                            :exact-match-id (some-> (:exact-match-id params) str)
                                            :date-range (:date-range params)
                                            :amount-gte (:amount-gte (:amount-range params))
                                            :amount-lte (:amount-lte (:amount-range params))
                                            :check-number-like (str (:check-number-like params))
                                            :invoice-number (:invoice-number params)}
                                           [[:payments [:id :status :amount :type :check_number :s3_url
                                                        [:bank-account [:name]]
                                                        :date [:vendor [:name :id]] [:client [:name :id]]
                                                        [:invoices [:invoice-id [:invoice [:invoice-number :id]]
                                                                    :amount]]
                                                        [:transaction [:id :date]]]]
                                            :total
                                            :start
                                            :end]]]} 
              :on-success (fn [result]
                            (let [result (set/rename-keys (first (:payment-page result))
                                                          {:payments :data})]
                              [::data-page/received ::page result]))}}))


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

(re-frame/reg-event-fx
 ::mounted
 (fn [{:keys [db]} _]
   {::track/register {:id ::params
                      :subscription [::data-page/params ::page]
                      :event-fn (fn [params]
                                  [::params-change params])}
    ::forward/register {:id ::page
                        :events #{::table/payment-voided}
                        :event-fn (fn [[_ {:keys [void-payment]}]]
                                    [::data-page/updated-entity ::page (assoc void-payment :class "live-removed")])}}))

(defn content []
  [:div
   [:h1.title "Payments"]
   [table/table {:id :payments
                 :data-page ::page}]])

(defn payments-page []
  (reagent/create-class
   {:display-name "payments-page"
    :component-will-unmount #(re-frame/dispatch-sync [::unmounted])
    :component-did-mount #(re-frame/dispatch [::mounted])
    :reagent-render
    (fn []
      [side-bar-layout {:side-bar [side-bar/side-bar {:data-page ::page}]
                        :main [content]}])}))
