(ns auto-ap.events.expense-accounts
  (:require [re-frame.core :as re-frame]
            [reagent.core :as r]
            [clojure.string :as str]
            [clojure.spec.alpha :as s]
            [cljs-time.core :as c]
            [goog.string :as gstring]
            [auto-ap.entities.clients :as clients]
            [auto-ap.entities.invoice :as invoice]
            [auto-ap.entities.vendors :as vendor]
            [auto-ap.expense-accounts :as expense-accounts]
            [auto-ap.entities.invoices-expense-accounts :as invoices-expense-accounts]
            [auto-ap.views.utils :refer [dispatch-event bind-field horizontal-field date->str str->date pretty standard]]
            [auto-ap.utils :refer [by replace-if]]
            [auto-ap.events :as events]))

(re-frame/reg-event-fx
 ::change-expense-accounts
 (fn [{:keys [db]} [_ id]]
   {:dispatch [::events/modal-status ::change-expense-accounts {:visible? true}]
    :db (assoc-in db [::change-expense-accounts :invoice] (get (by :id (get-in db [::invoice-page :invoices])) id))}))

(re-frame/reg-event-fx
 ::change-expense-accounts-saving
 (fn [{:keys [db]} [_ id]]
   (let [{:keys [id expense-accounts]} (get-in db [::change-expense-accounts :invoice])]
     {:graphql
      {:token (-> db :user)
       :query-obj {:venia/operation {:operation/type :mutation
                                     :operation/name "EditExpenseAccounts"}
                   
                   :venia/queries [{:query/data [:edit-expense-accounts
                                                 {:invoice-id  id
                                                  :expense-accounts (map (fn [ea] {:id (:id ea)
                                                                                   :amount (:amount ea)
                                                                                   :location (:location ea)
                                                                                   :expense-account-id (:expense-account-id ea)})
                                                                         expense-accounts)}
                                                 [:id :total :outstanding-balance :invoice-number :date
                                                  [:vendor [:name :id]]
                                                  [:expense_accounts [:amount :id :location :expense_account_id
                                                                      [:expense_account [:id :name [:parent [:id :name]]]]]]
                                                  [:client [:name :id :locations]]
                                                  [:checks [:amount [:check [:amount :s3_url :check_number ]]]]
                                                  ]]}]}  
       :on-success [::expense-accounts-updated]}})))


(re-frame/reg-event-fx
 ::expense-accounts-updated
 (fn [{:keys [db]} [_ data]]
   (let [updated (:edit-expense-accounts data)]
     {:dispatch [::events/modal-completed ::change-expense-accounts]
      :db (-> db
              (update-in [::invoice-page :invoices]
                         
                         (fn [is]
                           
                           (replace-if #(= (:id %1) (:id %2)) updated is)))
              (dissoc ::change-expense-accounts))})))

(re-frame/reg-event-db
 ::add-expense-account-split
 (fn [db _]
   (let [{{{:keys [locations]} :client} :invoice} @(re-frame/subscribe [::change-expense-accounts])]
     (update-in db [::change-expense-accounts :invoice :expense-accounts]
                conj {:amount 0 :expense-account-id nil :location (first locations)}))))

(re-frame/reg-event-db
 ::remove-expense-account-split
 (fn [db [_ index]]
   (update-in db [::change-expense-accounts :invoice :expense-accounts]
              (fn [expense-accounts]
                (vec (concat (take index expense-accounts)
                             (drop (inc index) expense-accounts)))))))
