{ config, pkgs, lib, ... }:
let
  # TODO: trim whitespaces from string
  email-uni-marburg = (builtins.readFile ../secrets/email-uni-marburg);
in
{
  # https://github.com/NotAShelf/nyx/blob/main/homes/notashelf/graphical/apps/thunderbird/default.nix
  # https://github.com/dr460nf1r3/dr460nixed/blob/main/home-manager/email.nix
  # https://github.com/yurrriq/dotfiles/blob/main/machines/nixps/home.nix

  home-manager.users."yoda" = { osConfig, config, pkgs, ... }: {
    accounts.email.accounts = {
      "personal" = {
        address = "daniel@systemli.org";
        gpg = {
          # The key to use as listed in gpg --list-keys.
          key = "94F3D3DDAC22802258FC044B6C47C753F0823002";
          signByDefault = true;
        };
        primary = true;
        realName = "Daniel Langbein";

        userName = "daniel@systemli.org";
        imap = {
          host = "mail.systemli.org";
          port = 993;
        };
        smtp = {
          host = "mail.systemli.org";
          port = 465;
        };

        # Enable the Thunderbird mail client for this email account.
        thunderbird.enable = true;
      };

      "uni-marburg" = {
        address = email-uni-marburg;
        primary = false;
        realName = "Daniel Langbein";

        # It seems as if both is possible:
        #   userName: part of email before "@"
        #   userName: full email
        #userName = lib.head (lib.splitString "@" email-uni-marburg);
        userName = email-uni-marburg;
        imap = {
          host = "imap.students.uni-marburg.de";
          port = 993;
        };
        smtp = {
          host = "smtp.students.uni-marburg.de";
          port = 465;
        };

        # Enable the Thunderbird mail client for this email account.
        thunderbird.enable = true;
      };
    };

    programs.thunderbird = {
      enable = true;
      profiles.default = {
        isDefault = true;
        #userChrome = "";
        #userContent = "";
        withExternalGnupg = true;
        settings = {
          # Don't execute JavaScript from PDF files.
          "pdfjs.enableScripting" = false;
          "privacy.donottrackheader.enabled" = true;

          # 1: Allow cookies from originating server only
          # https://kb.mozillazine.org/Network.cookie.cookieBehavior
          "network.cookie.cookieBehavior" = 1;
          # 2: Accept for session only
          # https://kb.mozillazine.org/Network.cookie.lifetimePolicy
          "network.cookie.lifetimePolicy" = 2;

          "mail.spellcheck.inline" = false;
          # Use Paragraph format instead of Body Text by default = false
          "mail.compose.default_to_paragraph" = false;
          # When new messages arrive: Show an alert = false
          "mail.biff.show_alert" = false;
          # When new messages arrive: Play a sound = false
          "mail.biff.play_sound" = false;
          # Automatically mark messages as read = false
          "mailnews.mark_message_read.auto" = false;

          # https://wiki.archlinux.org/index.php/Thunderbird#Change_the_default_sorting_order
          # Descending
          "mailnews.default_sort_order" = 2;
          # Threaded view
          "mailnews.default_view_flags" = 1;
        };
      };
    };
  };
}