(ns auto-ap.views.components
  (:require [reagent.core :as r]
            [clojure.string :as str]
            [auto-ap.views.components.multi :as multi]
            [auto-ap.views.components.money-field :as money]
            [auto-ap.views.components.number :as number]
            [auto-ap.views.components.typeahead.vendor :as typeahead]
            [auto-ap.views.components.button-radio :as br]))


(defn checkbox [{:keys [on-change
                        value
                        label]
                 :as props}]
  (into [:label.checkbox
         [:input (-> props
                     (assoc 
                       :type      "checkbox"
                       :on-change (fn []
                                    (on-change (not value)))
                       :checked   value)
                     (dissoc :value))]
         " " label
         ]
        (r/children (r/current-component))))

(defn select-field [{:keys [options allow-nil? class on-change keywordize?] :as props}]
  [:div.select {:class class}
   [:select (-> props
                (dissoc :allow-nil? :class :options)
                (update :value (fn [v]
                                 (cond (str/blank? v)
                                         ""

                                         keywordize?
                                         (name v)

                                         :else
                                         v)))
                (assoc :on-change
                       (fn [e]
                         (println "VALUE IS" (keyword (.. e -target -value)))
                         (if keywordize?
                           (on-change (keyword (.. e -target -value)))
                           (on-change e))))
                (dissoc :keywordize?))
    [:<> 
     (when allow-nil?
       [:option {:value nil}])
     (for [[k v] options]
       ^{:key k}  [:option {:value k} v])]]])

(defn switch-input [{:keys [id label on-change value class]}]
  [:<> 
   [:input.switch {:type "checkbox"
                   :id id
                   :on-change (fn []
                                (on-change (not value)))
                   :checked (boolean value)
                   :class class}]
   [:label {:for id} label]])

(def multi-field-v2 multi/multi-field-v2)

(def number-input number/number-input)

(def money-input money/field)

(def search-backed-typeahead typeahead/search-backed-typeahead)
(def entity-typeahead typeahead/typeahead-v3)

(def button-radio-input br/button-radio)
