(ns auto-ap.shared-views.admin.side-bar
  (:require [bidi.bidi :as bidi]
            [auto-ap.client-routes :as client-routes]
            [auto-ap.ssr-routes :as ssr-routes]
            #?(:cljs [re-frame.core :as re-frame])
            #?(:cljs [reagent.core :as r])
            [auto-ap.routes.admin.import-batch :as ib-routes]
            [auto-ap.routes.admin.transaction-rules :as tr-routes]
            [auto-ap.routes.admin.excel-invoices :as ei-routes]))

(defn deep-merge [v & vs]
  (letfn [(rec-merge [v1 v2]
            (if (and (map? v1) (map? v2))
              (merge-with deep-merge v1 v2)
              v2))]
    (when (some identity vs)
      (reduce #(rec-merge %1 %2) v vs))))
(def all-client-visible-routes 
  ["/" (deep-merge ssr-routes/routes (second client-routes/routes))])


(defn menu-item [{:keys [label route test-route active-route icon-class icon-style]}]
  [:p.menu-item
   [:a.item {:href  (bidi/path-for all-client-visible-routes route)
        :class (when (test-route active-route) "is-active")}
    (if icon-style 
      [:span {:class icon-class :style icon-style}]
      [:span {:class "icon"}
       [:i {:class icon-class}]])
    [:span {:class "name"} label]]])

(defn admin-side-bar-impl [active-route children]
  [:div
   [:p.menu-label "General"]
   (menu-item {:label "Dashboard"
               :icon-class "fa fa-tachometer"
               :test-route #{:admin}
               :active-route active-route
               :route :auto-ap.routes.admin/page})

   [:p.menu-label "Setup"]
   (menu-item {:label        "Clients"
               :icon-class   "fa fa-star-o"
               :test-route   #{:admin-clients
                               :admin-specific-client
                               :admin-specific-bank-account}
               :active-route active-route
               :route        :admin-clients})
   (menu-item {:label        "Vendors"
               :icon-class   "fa fa-star-o"
               :test-route   #{:admin-vendors}
               :active-route active-route
               :route        :admin-vendors})
   (menu-item {:label        "Users"
               :icon-class   "icon icon-single-neutral-book"
               :test-route   #{:users}
               :active-route active-route
               :route        :users
               :icon-style {:font-size "25px"}})
   (menu-item {:label        "Accounts"
               :icon-class   "icon icon-list-bullets"
               :test-route   #{:admin-accounts}
               :active-route active-route
               :route        :admin-accounts
               :icon-style   {:font-size "25px"}})
   (menu-item {:label        "Rules"
               :icon-class   "icon icon-cog-play-1"
               :test-route   #{::tr-routes/page}
               :active-route active-route
               :route        ::tr-routes/page
               :icon-style   {:font-size "25px"}})
   (menu-item {:label        "History"
               :icon-class   "icon icon-cog-play-1"
               :test-route   #{:admin-history :admin-history-search :admin-history-inspect}
               :active-route active-route
               :route        :admin-history
               :icon-style   {:font-size "25px"}})
   (menu-item {:label        "Background Jobs"
               :icon-class   "icon icon-cog-play-1"
               :test-route   #{:admin-jobs}
               :active-route active-route
               :route        :admin-jobs
               :icon-style   {:font-size "25px"}})
   [:p.menu-label "Import"]
   (menu-item {:label        "Excel Invoices"
               :icon-class   "fa fa-download"
               :test-route   #{:admin-excel-import}
               :active-route active-route
               :route        ::ei-routes/page})
   (menu-item {:label        "Import Batches"
               :icon-class   "fa fa-download"
               :test-route   #{:admin-import-batches}
               :active-route active-route
               :route        ::ib-routes/page})
   (menu-item {:label        "EZCater XLS Import"
               :icon-class   "icon icon-cog-play-1"
               :test-route   #{:admin-ezcater-xls}
               :active-route active-route
               :route        :admin-ezcater-xls
               :icon-style   {:font-size "25px"}})
   (menu-item {:label        "Invoice glimpse"
               :icon-class   "icon icon-cog-play-1"
               :test-route   #{:invoice-glimpse}
               :active-route active-route
               :route        :invoice-glimpse
               :icon-style   {:font-size "25px"}})

   (into [:div] children)])

#?(:clj 
   (defn admin-side-bar [active-page]
     (admin-side-bar-impl active-page nil))
   :cljs
   (defn admin-side-bar []
     (admin-side-bar-impl @(re-frame/subscribe [:auto-ap.subs/active-page])
                          (r/children (r/current-component)))))
