(ns auto-ap.ssr.components.navbar
  (:require [auto-ap.graphql.utils :refer [is-admin? limited-clients]]
            [auto-ap.ssr-routes :as ssr-routes]
            [auto-ap.ssr.company-dropdown :as cd]
            [auto-ap.ssr.components.buttons :refer [icon-button-]]
            [auto-ap.ssr.components.user-dropdown :as user-dropdown]
            [auto-ap.ssr.svg :as svg]
            [bidi.bidi :as bidi]))

(defn navbar- [{:keys [client-selection client identity clients dd-env]}]
  [:nav {:class "fixed z-30 w-full bg-white border-b border-gray-200 dark:bg-gray-800 dark:border-gray-700"}
   [:div {:class "px-3 py-3 lg:px-5 lg:pl-3"}
    [:div {:class "flex items-center justify-between"}
     [:div {:class "flex items-center justify-start"}
      [:button {:aria-controls "left-nav", :id "left-nav-toggle" :type "button", :class "inline-flex items-center p-2 mt-2 ml-2 mr-2 text-sm text-gray-500 rounded-lg hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-gray-200 dark:text-gray-400 dark:hover:bg-gray-700 dark:focus:ring-gray-600"
                "@click" "leftNavShow = !leftNavShow"}
       [:span {:class "sr-only"} "Open sidebar"]
       [:svg {:class "w-6 h-6", :aria-hidden "true", :fill "currentColor", :viewbox "0 0 20 20", :xmlns "http://www.w3.org/2000/svg"}
        [:path {:clip-rule "evenodd", :fill-rule "evenodd", :d "M2 4.75A.75.75 0 012.75 4h14.5a.75.75 0 010 1.5H2.75A.75.75 0 012 4.75zm0 10.5a.75.75 0 01.75-.75h7.5a.75.75 0 010 1.5h-7.5a.75.75 0 01-.75-.75zM2 10a.75.75 0 01.75-.75h14.5a.75.75 0 010 1.5H2.75A.75.75 0 012 10z"}]]]
      [:a {:href "/" :class "flex ml-2 hidden md:mr-24 sm:inline"}
       [:img {:src "/img/logo-big2.png", :class "h-10", :alt "Integreat logo"}]]
      (when-not (= "prod" dd-env) [:div.rounded-full.bg-yellow-200.text-lg.text-yellow-800.px-4.hidden.md:block.mr-8 "environment: " dd-env])]

     [:div {:class "flex items-center gap-4"}

      (when (is-admin? identity)
        [:button.mt-1.lg:w-96.relative.hidden.lg:block {:class     "bg-gray-50 hover:bg-gray-200 dark:hover:bg-gray-700 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 w-full pl-10 py-4 pr-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500 gap-4 "
                                                        :hx-get    (bidi/path-for ssr-routes/only-routes :search)}
         [:div {:class "absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none text-gray-500"}
          [:div.w-4.h-4 svg/search]
          [:span.ml-2 "Search"]]])
      [:div {:class "hidden mr-3 -mb-1 sm:block"}
       [:span]]
      (icon-button-
       {:id        "toggleSidebarMobileSearch", :type "button", :class "p-2 text-gray-500 rounded-lg lg:hidden hover:text-gray-900 hover:bg-gray-100 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white"
        :hx-get    (bidi/path-for ssr-routes/only-routes
                                  :search)
        :hx-target "#modal-holder"
        :hx-swap   "outerHTML"}
       svg/search)
      (let [limited-clients (limited-clients identity)]
        (when (or (nil? limited-clients)
                  (> (count limited-clients) 1))
          (cd/dropdown {:client-selection client-selection :client client :identity identity
                        :clients clients})))

      (user-dropdown/dropdown {:identity identity})]]]])
