{ pkgs, hyper, rice, secrets, ... }: 
pkgs.lib.mkIf (hyper.host != "NxACE")
{
  home.packages = with pkgs; [
    thunderbird
  ];

  programs.thunderbird = let
    inherit (pkgs.lib.generators) toJSON;
    extensions = toJSON {} {
      "addon@darkreader.org" = "71d6c69d-55f9-4c56-888c-abdcf6efd73d";
    };
  in {
    enable = true;
    profiles = {
      "${hyper.user}" = {
        isDefault = true;
        settings = with rice.color; {
          "calendar.alarms.showmissed" = false;
          "calendar.alarms.playsound" = false;
          "calendar.alarms.show" = false;
          "extensions.webextensions.uuids" = extensions;
          "mail.startup.enabledMailCheckOnce" = true;
          "mailnews.mark_message_read.delay" = true;
          "mailnews.mark_message_read.delay.interval" = 3;
          "mailnews.tags.$label1.color" = red.base;
          "mailnews.tags.$label1.tag" = "Important";
          "mailnews.tags.$label2.color" = yellow.base;
          "mailnews.tags.$label2.tag" = "Work";
          "mailnews.tags.$label3.color" = green.base;
          "mailnews.tags.$label3.tag" = "Personal";
          "mailnews.tags.$label4.color" = cyan.base;
          "mailnews.tags.$label4.tag" = "To Do";
          "mailnews.tags.$label5.color" = blue.base;
          "mailnews.tags.$label5.tag" = "Later";
          "mailnews.headers.showMessageId" = true;
          "mailnews.headers.showOrganization" = true;
          "mailnews.headers.showReferences" = true;
          "mailnews.headers.showSender" = true;
          "mailnews.headers.showUserAgent" = true;
          "toolkit.legacyUserProfileCustomizations.stylesheets" = true;         
        };
        userChrome = with rice.color; /* css */ ''
          body,
          #navigation-toolbox,
          #calendarContent,
          #calSidebar,
          #tabpanelcontainer,
          #spacesToolbar,
          #tabs-toolbar,
          #calMinimonth,
          #primaryButtonSidePanel *,
          .minimonth-week,
          .multiday-header-corner, .day-column-heading, calendar-header-container, calendar-event-column, .multiday-hour-box,
          #view-box,
          #tabs-toolbar,
          #status-bar,
          .calview-toggle,
          #calview-toggle-item,
          #folderPaneHeaderBar,
          #folderPane,
          #threadPaneHeaderBar,
          #threadTree,
          #tabs-toolbar {
            color: ${foreground} !important;
            font-family: ${rice.font.base.name} !important;
            background-color: transparent !important;
            background-image: none !important;
            border: none !important;
            -moz-appearance: none !important;
          }
          .minimonth-nav-section {
            background-color: rgba(${rice.lib.hex-to-rgb-comma-string background},${builtins.toString rice.transparency}) !important;
            border: ${builtins.toString rice.border-width}px solid ${border};
          }
          .button,
          .button-primary,
          .tab-content[selected] {
            background-color: ${accent.base}; 
            color: ${background};
            background-image: none; 
          }
          html {
            background-color: rgba(${rice.lib.hex-to-rgb-comma-string background},${builtins.toString rice.transparency}) !important;
          }
        '';
      }; 
    }; 
  };


  accounts.email.accounts = let
    OAuth2Settings = id: {
      "mail.smtpserver.smtp_${id}.authMethod" = 10;
      "mail.server.server_${id}.authMethod" = 10;
    };
  in {
    gmail-online = {
      address = secrets.email.gmail-online.mail;
      realName = "Lennart J. Kurzweg";
      flavor = "gmail.com";
      primary = true;
      smtp.tls.useStartTls = true;
      signature = {
        text = ''
          About Me: https://nx2.site/about-me
          Contact: https://nx2.site/contact
          GPG: https://nx2.site/gpg
        '';
        showSignature = "append";
      };
      thunderbird = {
        enable = true;
        profiles = [ "nx2" ];
        # The id given as argument is an automatically generated account identifier.
        settings = OAuth2Settings; 
      };
    };

    gmail-business = {
      address = secrets.email.gmail-business.mail;
      realName = "Lennart J. Kurzweg";
      flavor = "gmail.com";
      signature = {
        text = ''
          About Me: https://nx2.site/about-me
          Contact: https://nx2.site/contact
          GPG: https://nx2.site/gpg
        '';
        showSignature = "append";
      };
      thunderbird = {
        enable = true;
        profiles = [ "nx2" ];
        # The id given as argument is an automatically generated account identifier.
        settings = OAuth2Settings; 
      };
    };

    hsmw = with secrets.email.hsmw; {
      address = "${un}@hs-mittweida.de";
      userName = "${un}@hs-mittweida.de";
      realName = "Lennart J. Kurzweg";
      imap = {
        port = 993;
        host = "xc.hs-mittweida.de";
      };
      smtp = {
        port = 587;
        host = "xc.hs-mittweida.de";
        tls.useStartTls = true;
      };
      signature = {
        text = ''
          MatNr: ${mnr}
          SemGr: ${semgr}
          About Me: https://nx2.site/about-me
          Contact: https://nx2.site/contact
          GPG: https://nx2.site/gpg
        '';
        showSignature = "append";
      };
      thunderbird = {
        enable = true;
        profiles = [ "nx2" ];
        settings = id: {
          "mail.server.server_${id}.fcc_folder" = "imap://${un}%40hs-mittweida.de@xc.hs-mittweida.de/Sent";
        };
      };
    };
    tuda = with secrets.email.tuda; {
      address = "${un}@stud.tu-darmstadt.de";
      userName = tuid;
      realName = "Lennart J. Kurzweg";
      imap = {
        port = 993;
        host = "imap.stud.tu-darmstadt.de";
      };
      smtp = {
        port = 465;
        host = "smtp.tu-darmstadt.de";
        tls.enable = true;
      };
      signature = {
        text = ''
          Field: MSc. AI/ML
          MNr: ${mnr}
          TU-ID: ${tuid}
          About Me: https://nx2.site/about-me
          Contact: https://nx2.site/contact
          GPG: https://nx2.site/gpg
        '';
        showSignature = "append";
      };
      thunderbird = {
        enable = true;
        profiles = [ "nx2" ];
        settings = id: {
          "mail.server.server_${id}.fcc_folder" = "imap://${un}%40hs-mittweida.de@xc.hs-mittweida.de/Sent";
        };
      };
    };
  };
}



