(ns todo.widgets.todo-form
  (:require
   ["package:flutter/material.dart" :as m]
   [cljd.flutter :as f]
   [todo.state :as state]
   [todo.db :as db]))

(defn show-todo-dialog [ctx todo]
  (let [editing? (some? todo)
        title-ctl (m/TextEditingController .text (or (:title todo) ""))
        selected-category (atom (or (:category todo) "Personal"))
        selected-priority (atom (or (:priority todo) :medium))]
    (m/showDialog
      .context ctx
      .builder
      (f/build
        :context dialog-ctx
        :watch [cat selected-category
                pri selected-priority]
        (m/AlertDialog
          .title (m/Text (if editing? "Edit Todo" "Add New Todo"))
          .content
          (m/SingleChildScrollView
            .child
            (m/Column
              .mainAxisSize m/MainAxisSize.min
              .crossAxisAlignment m/CrossAxisAlignment.start
              .children
              [(m/TextField
                 .controller title-ctl
                 .decoration (m/InputDecoration
                               .labelText "What needs to be done?"
                               .border (m/OutlineInputBorder))
                 .autofocus true)
               (m/SizedBox .height 16)
               (m/Text "Category"
                 .style (m/TextStyle .fontWeight m/FontWeight.w600))
               (m/SizedBox .height 8)
               (m/Wrap
                 .spacing 8
                 .children
                 (into []
                   (map (fn [c]
                          (m/ChoiceChip
                            .label (m/Text c)
                            .selected (= c cat)
                            .onSelected (fn [_] (reset! selected-category c))))
                        state/categories)))
               (m/SizedBox .height 16)
               (m/Text "Priority"
                 .style (m/TextStyle .fontWeight m/FontWeight.w600))
               (m/SizedBox .height 8)
               (m/SegmentedButton
                 .segments
                 (into []
                   (map (fn [p]
                          (m/ButtonSegment
                            .value p
                            .label (m/Text (name p))))
                        state/priorities))
                 .selected #{pri}
                 .onSelectionChanged
                 (fn [selection]
                   (reset! selected-priority (first selection))))]))
          .actions
          [(m/TextButton
             .onPressed (fn [] (.pop (m/Navigator.of dialog-ctx)))
             .child (m/Text "Cancel"))
           (m/FilledButton
             .onPressed
             (fn []
               (let [title (.-text title-ctl)]
                 (when (seq (.trim title))
                   (if editing?
                     (do
                       (state/update-todo! (:id todo)
                         {:title title
                          :category @selected-category
                          :priority @selected-priority})
                       (db/update-todo-db! (:id todo) title
                         @selected-category @selected-priority))
                     (let [new-todo (await (db/insert-todo! title
                                            @selected-category
                                            @selected-priority))]
                       (state/add-todo! new-todo)))
                   (.pop (m/Navigator.of dialog-ctx)))))
             .child (m/Text (if editing? "Save" "Add")))])))))
