(ns auto-ap.views.main
  (:require
   [re-frame.core :as re-frame]
   [bidi.bidi :as bidi]
   [auto-ap.routes :as routes]
   [auto-ap.subs :as subs]
   [auto-ap.events :as events]
   [auto-ap.views.utils :refer [active-when active-when= login-url dispatch-event]]
   [auto-ap.views.components.layouts :refer [side-bar-layout loading-layout]]
   [auto-ap.views.pages.unpaid-invoices :refer [unpaid-invoices-page paid-invoices-page all-invoices-page voided-invoices-page]]
   [auto-ap.views.pages.import-invoices :refer [import-invoices-page]]
   [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.login :refer [login-page]]
   [auto-ap.views.pages.payments :refer [payments-page]]
   [auto-ap.views.pages.pos.sales-orders :refer [sales-orders-page]]
   [auto-ap.views.pages.pos.expected-deposits :refer [expected-deposits-page]]
   [auto-ap.views.pages.admin :refer [admin-page]]
   [auto-ap.views.pages.home :refer [home-page]]
   [auto-ap.views.pages.admin.clients :refer [admin-clients-page]]
   [auto-ap.views.pages.admin.accounts :refer [admin-accounts-page]]
   [auto-ap.views.pages.admin.rules :refer [admin-rules-page]]
   [auto-ap.views.pages.admin.vendors :refer [admin-vendors-page]]
   [auto-ap.views.pages.admin.excel-import :refer [admin-excel-import-page]]
   [auto-ap.views.pages.admin.users :refer [admin-users-page]]
   [auto-ap.views.pages.admin.import-batches :refer [import-batches-page]]
   [auto-ap.views.pages.admin.yodlee :refer [admin-yodlee-page]]
   [auto-ap.views.pages.admin.yodlee2 :as yodlee2]
   [auto-ap.entities.clients :as clients]))

(defmulti page (fn [active-page] active-page))
(defmethod page :unpaid-invoices [_]
  ^{:key :unpaid}
  [unpaid-invoices-page {:status :unpaid}])

(defmethod page :import-invoices [_]
  (import-invoices-page ))


(defmethod page :paid-invoices [_]
  ^{:key :paid}
  [paid-invoices-page {:status :paid}])

(defmethod page :voided-invoices [_]
  ^{:key :voided}
  [voided-invoices-page {:status :voided}])

(defmethod page :invoices [_]
  ^{:key :all}
  [all-invoices-page {}])

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

(defmethod page :sales-orders [_]
  [sales-orders-page])

(defmethod page :expected-deposits [_]
  [expected-deposits-page])

(defmethod page :transactions [_]
  (transactions-page {}))

(defmethod page :unapproved-transactions [_]
  [transactions-page {:approval-status :unapproved}])

(defmethod page :approved-transactions [_]
  [transactions-page {:approval-status :approved}])

(defmethod page :requires-feedback-transactions [_]
  [transactions-page {:approval-status :requires-feedback}])

(defmethod page :excluded-transactions [_]
  [transactions-page {:approval-status :excluded}])


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

(defmethod page :profit-and-loss [_]
  (profit-and-loss-page))

(defmethod page :balance-sheet [_]
  (balance-sheet-page))

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

(defmethod page :admin-clients [_]
  (admin-clients-page))


(defmethod page :admin-rules [_]
  (admin-rules-page))

(defmethod page :admin-users [_]
  (admin-users-page))

(defmethod page :admin-vendors [_]
  (admin-vendors-page))

(defmethod page :admin-import-batches [_]
  [import-batches-page])

(defmethod page :admin-yodlee [_]
  (admin-yodlee-page))

(defmethod page :admin-yodlee2 [_]
  (yodlee2/admin-yodle-provider-accounts-page))

(defmethod page :admin-accounts [_]
  (admin-accounts-page))

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

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

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

(defmethod page :external-import-ledger [_]
  [external-import-page])

(defmethod page :external-ledger [_]
  [external-ledger-page])

(defmethod page :admin-excel-import [_]
  [admin-excel-import-page])

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

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