             [frontend.components.forms :as forms]
             [frontend.components.pieces.card :as card]
             [frontend.components.pieces.empty-state :as empty-state]
+            [frontend.components.pieces.form :as form]
             [frontend.components.pieces.modal :as modal]
             [frontend.components.pieces.org-picker :as org-picker]
             [frontend.components.pieces.table :as table]

        (when (not= (:selected-org-ident (om/get-render-state owner))
                    selected-org-ident)
          (api/get-org-settings-normalized name vcs-type api-chan))
        (when (and selected-org
                   invite-teammates?)
          (api/get-org-members api-chan (:name selected-org)))))

    om/IRenderState
    (render-state [_ {:keys [selected-org-ident invite-teammates?]}]

       (map (fn [u] (select-keys u [:email :login :id])))
       vec))

(defn invite-teammates-modal [{:keys [selected-org-ident app]} owner]
  (reify
    om/IInitState
    (init-state [_]
      {:invite-teammates? true})
    om/IDidMount
    (did-mount [_]
      (api/get-org-members (om/get-shared owner [:comms :api]) (:name (get-in app selected-org-ident))))

    om/IRenderState
    (render-state [_ {:keys []}]
      (when (om/get-state owner :invite-teammates?)
        (let [invite-data (:invite-data app)
            users (remove :circle_member (:github-users invite-data))
            opts {:vcs_type (:vcs_type invite-data)
                  :org-name (:org invite-data)}
            count-users (count users)
            count-with-email (count (filter #(utils/valid-email? (:email %)) users))
            count-selected (count (filter #(:checked %) users))]
        (html
          (component
            (modal/modal-dialog {:title "Invite Teammates"
                                 :body
                                 [:div
                                  [:div
                                   "These are the people who are not using CircleCI yet:"
                                   [:br]
                                   [:span [:b count-with-email] " of " [:b count-users] " users have emails, " [:b count-selected] " are selected"]]
                                  [:div.constraining-modal
                                   (om/build table/table {:rows users
                                                          :key-fn :login
                                                          :columns [{:header "Username"
                                                                     :cell-fn (fn [user-map]
                                                                                [:span
                                                                                 [:img.invite-gravatar {:src (gh-utils/make-avatar-url user-map :size 50)}]
                                                                                 (str "  " (:login user-map))])}
                                                                    {:header "Email"
                                                                     :cell-fn (fn [user-map]
                                                                                (let [{:keys [avatar_url email login index checked]} user-map
                                                                                      id-name (str login "-email")
                                                                                      error? (and (or checked (not (empty? email)))
                                                                                                  (not (utils/valid-email? email)))]
                                                                                  (om/build form/text-field {:on-change (fn [event]
                                                                                                                          (utils/edit-input owner (conj (state/invite-github-user-path index) :email) event)
                                                                                                                          (when (and (not= checked true)
                                                                                                                                     (not (empty? email)))
                                                                                                                            (utils/toggle-input owner (conj (state/invite-github-user-path index) :checked) nil)))
                                                                                                             :value email
                                                                                                             :size :medium
                                                                                                             :validation-error (when (and (or checked (not (empty? email)))
                                                                                                                                           (not (utils/valid-email? email)))
                                                                                                                                  (str email "is not a valid email"))})))}
                                                                    {:type :shrink
                                                                     :cell-fn (fn [user-map]
                                                                                (let [{:keys [email login index checked]} user-map
                                                                                      id-name (str login "-checkbox")
                                                                                      email-valid? (utils/valid-email? email)]
                                                                                  [:input {:type "checkbox"
                                                                                           :id id-name
                                                                                           :disabled (and (not (utils/valid-email? email))
                                                                                                           (not (empty? email)))
                                                                                           :checked checked
                                                                                           :on-click #(utils/toggle-input owner (conj (state/invite-github-user-path index) :checked) %)}]))}]
                                                          :striped? true
                                                          :fixed-header? true})]]
                                 :actions [(button/button {:on-click #(om/set-state! owner :invite-teammates? false)} "Cancel")
                                           (forms/managed-button
                                             [:button.btn.btn-success {:data-success-text "Sent"
                                                                       :on-click #(.log js/console "Invitees: " (seq (invitees users)))
                                                                       ;; :on-click #(raise! owner [:invited-github-users (merge {:invitees (invitees users)
                                                                       ;;                                                         :vcs_type (:vcs_type opts)}
                                                                       ;;                                                        (if (:project-name opts)
                                                                       ;;                                                          {:project-name (:project-name opts)}
                                                                       ;;                                                          {:org-name (:org-name opts)}))])
                                                                       :disabled (or (empty? (invitees users))
                                                                                     (not (every? #(utils/valid-email? (:email %)) (invitees users))))}
                                              "Send Invites "
                                              [:i.fa.fa-envelope-o]])]
                                 :close-fn #(om/set-state! owner :invite-teammates? false)}))))))))

(defn- main-content [app owner]
  (reify
    om/IInitState
