(ns distractionless.ui.apps.settings
  (:require ["package:flutter/material.dart" :as m]
            [cljd.flutter :as f]
            [distractionless.ui.constants :as duconstants]
            [distractionless.config :as dconfig]))

(defn- header [app ctx]
  (m/Row
   .mainAxisAlignment m/MainAxisAlignment.spaceBetween
   .children [(m/Text (.-name app)
                      .style duconstants/title-style)
              (m/IconButton .icon (m/Icon m/Icons.clear_rounded)
                            .color duconstants/text-color
                            .onPressed #(m/Navigator.pop ctx)
                            .iconSize 30.0)]))

(defn- favourite-setting [app config-file reloader]
  (m/CheckboxListTile
   .title (m/Text "Favorit"
                  .style duconstants/text-style)
   .value (contains? (get (dconfig/read-from-file config-file) "favourites")
                     (.-packageName app))
   .onChanged (fn [new-val]
                (if new-val
                  (dconfig/update-config! config-file
                                          #(update % "favourites" conj (.-packageName app))
                                          reloader)
                  (dconfig/update-config! config-file
                                          (fn [config]
                                            (update config "favourites" disj (.-packageName app)))
                                          reloader)))
   .activeColor duconstants/checkbox-active-color))

(defn open [app config-file ctx reloader]
  (m/showDialog
   .context ctx
   .builder (f/build :watch [_ reloader]
                     (m/Dialog.fullscreen
                      .backgroundColor duconstants/background-color
                      .child (m/Column
                              .children (list (header app ctx)
                                              (favourite-setting app config-file reloader)))))))