(ns auto-ap.views.main
  (:require
   [re-frame.core :as re-frame]
   [auto-ap.subs :as subs]
   [auto-ap.permissions :as p]
   [auto-ap.views.components.layouts :refer [loading-layout]]
   [auto-ap.views.pages.needs-activation :refer [needs-activation-page]]
   [auto-ap.views.pages.transactions :refer [transactions-page]]
   [auto-ap.views.pages.ledger :refer [ledger-page]]
   [auto-ap.views.pages.error :refer [error-page]]
   [auto-ap.views.pages.ledger.balance-sheet :refer [balance-sheet-page]]
   [auto-ap.views.pages.ledger.external-import :refer [external-import-page]]
   [auto-ap.views.pages.ledger.external-ledger :refer [external-ledger-page]]
   [auto-ap.views.pages.ledger.profit-and-loss :refer [profit-and-loss-page]]
   [auto-ap.views.pages.ledger.cash-flows :refer [cash-flows-page]]
   [auto-ap.views.pages.ledger.profit-and-loss-detail :refer [profit-and-loss-detail-page]]
   [auto-ap.views.pages.login :refer [login-page]]
   [auto-ap.views.pages.home :refer [home-page home-page-with-vendor]]))

(defmulti page (fn [active-page] active-page))


(defmethod page :transactions [_]
  (when (p/can? @(re-frame/subscribe [::subs/user]) {:subject :transaction-page})
    (transactions-page {})))

(defmethod page :unapproved-transactions [_]
  (when (p/can? @(re-frame/subscribe [::subs/user]) {:subject :transaction-page}) 
    [transactions-page {:approval-status :unapproved}]))

(defmethod page :approved-transactions [_]
  (when (p/can? @(re-frame/subscribe [::subs/user]) {:subject :transaction-page})
    [transactions-page {:approval-status :approved}]))

(defmethod page :requires-feedback-transactions [_]
  (when (p/can? @(re-frame/subscribe [::subs/user]) {:subject :transaction-page})
    [transactions-page {:approval-status :requires-feedback}]))

(defmethod page :excluded-transactions [_]
  (when (p/can? @(re-frame/subscribe [::subs/user]) {:subject :transaction-page})
    [transactions-page {:approval-status :excluded}]))


(defmethod page :ledger [_]
  (when (p/can? @(re-frame/subscribe [::subs/user]) {:subject :ledger-page})
    (ledger-page)))

(defmethod page :profit-and-loss [_]
  (when (p/can? @(re-frame/subscribe [::subs/user]) {:subject :ledger-page})
    (profit-and-loss-page)))

(defmethod page :cash-flows [_]
  (when (p/can? @(re-frame/subscribe [::subs/user]) {:subject :ledger-page})
    (cash-flows-page)))

(defmethod page :profit-and-loss-detail [_]
  (when (p/can? @(re-frame/subscribe [::subs/user]) {:subject :ledger-page})
    (profit-and-loss-detail-page)))

(defmethod page :balance-sheet [_]
  (when (p/can? @(re-frame/subscribe [::subs/user]) {:subject :ledger-page})
    (balance-sheet-page)))

(defmethod page :index [_]
  (home-page))

(defmethod page :new-vendor [_]
  (home-page-with-vendor))


(defmethod page :login [_]
  [login-page])

(defmethod page :needs-activation [_]
  [needs-activation-page])

(defmethod page :external-import-ledger [_]
  (when (p/can? @(re-frame/subscribe [::subs/user]) {:subject :ledger-page})
    [external-import-page]))

(defmethod page :external-ledger [_]
  (when (p/can? @(re-frame/subscribe [::subs/user]) {:subject :ledger-page})
    [external-ledger-page]))

(defmethod page :initial-error [_]
  [error-page])

(defn active-page []
  (let [ap (re-frame/subscribe [::subs/active-page])
        current-client (pr-str @(re-frame/subscribe [::subs/selected-clients-raw]))
        is-loading? @(re-frame/subscribe [::subs/is-initial-loading?])]
    (when @ap
      (if is-loading? 
        [loading-layout]
        
        [:div
         ^{:key (str @ap "-" current-client)} [page @ap]]))))
