(ns auto-ap.shared-views.company.sidebar
  (: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])))


(defn active-when [active-page f & rest]
  (when (apply f (into [active-page] rest)) " is-active"))

(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 company-side-bar-impl [active-route]
  [:div
   (menu-item {:label "Reports"
               :route :reports
               :test-route #{:reports}
               :active-route active-route
               :icon-class "icon icon-receipt"
               :icon-style {:font-size "25px"}})

   (menu-item {:label        "Plaid Link"
               :route        :plaid
               :test-route   #{:plaid}
               :active-route active-route
               :icon-class   "icon icon-saving-bank-1"
               :icon-style   {:font-size "25px"}})
   (menu-item {:label        "Yodlee Link"
               :route        :yodlee2
               :test-route   #{:yodlee2}
               :active-route active-route
               :icon-class   "icon icon-saving-bank-1"
               :icon-style   {:font-size "25px"}})

   (menu-item {:label        "Other"
               :route        :company-other
               :test-route   #{:company-other}
               :active-route active-route
               :icon-class   "icon icon-cog-play-1"
               :icon-style   {:font-size "25px"}})
   (menu-item {:label        "1099 Info"
               :route        :company-1099
               :test-route   #{:company-1099}
               :active-route active-route
               :icon-class   "icon icon-cog-play-1"
               :icon-style   {:font-size "25px"}})])

#?(:clj 
   (defn company-side-bar [active-page]
     (company-side-bar-impl active-page))
   :cljs
   (defn company-side-bar []
     (company-side-bar-impl @(re-frame/subscribe [:auto-ap.subs/active-page]))))
