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

(defn todo-item-widget [todo]
  (let [{:keys [id title completed category priority]} todo]
    (f/widget
      :context ctx
      :get {{color-scheme .-colorScheme} m/Theme}
      (m/Card
        .margin (m/EdgeInsets.symmetric .horizontal 16 .vertical 4)
        .child
        (m/ListTile
          .leading
          (m/Checkbox
            .value completed
            .onChanged (fn [_]
                         (state/toggle-todo! id)
                         (db/toggle-todo-db! id (not completed))))
          .title
          (m/Text title
            .style (m/TextStyle
                     .decoration (if completed
                                   m/TextDecoration.lineThrough
                                   m/TextDecoration.none)
                     .color (when completed
                              (.-onSurfaceVariant ^m/ColorScheme color-scheme))))
          .subtitle
          (m/Row
            .children
            [(m/Chip
               .label (m/Text category
                        .style (m/TextStyle .fontSize 12))
               .visualDensity m/VisualDensity.compact
               .padding m/EdgeInsets.zero)
             (m/SizedBox .width 8)
             (priority-badge/priority-badge priority)])
          .onTap (fn [] (todo-form/show-todo-dialog ctx todo)))))))
