{ config, pkgs, ... }:

{
    home.file = {
        ".mailcap".text =
            "text/html; w3m -dump -o -document_charset=%{charset} %s; nametemplate=%s.html; copiousoutput";
    };
    home.packages = with pkgs; [ mailcap ];

    programs.notmuch = {
        enable = true;
        extraConfig = {
            database = { path = "/home/server/Storage/Thomas/Mail"; };
            user = {
                name = "Thomas Avé";
                primary_email = "email@thomasave.be";
                other_email = "Thomas.Ave@uantwerpen.be;Thomas.Ave@imec.be;";
            };
            maildir = { synchronize_flags = "true"; };
        };
    };
    programs.aerc = {
        enable = true;
        extraBinds = {
            global = {
                "<C-p>" = ":menu -adc 'fzy -l 30' :cf -a<Enter>";
                "<tab>" = ":next-tab<Enter>";
                "?" = ":help keys<Enter>";
                "<F5>" = ":check-mail<Enter>";
            };
            messages = {
                "q" = ":q<Enter>";
                "<Backspace>" = ":q<Enter>";
                "j" = ":next<Enter>";
                "k" = ":prev<Enter>";
                "<Up>" = ":prev<Enter>";
                "g" = ":select 0<Enter>";
                "G" = ":select -1<Enter>";
                "J" = ":next-folder<Enter>";
                "K" = ":prev-folder<Enter>";
                "v" = ":mark -t<Enter>";
                "V" = ":mark -v<Enter>";
                "<Space>" = ":read -t<Enter>";
                "T" = ":toggle-threads<Enter>";
                "<Enter>" = ":view<Enter>";
                "d" = ":prompt 'Really delete this message?' 'delete-message'<Enter>";
                "D" = ":delete<Enter>";
                "a" = ":archive flat<Enter>";
                "A" = ":unmark -a<Enter>:mark -T<Enter>:archive flat<Enter>";
                "f" = ":flag -t<Enter>";
                "m" = ":compose<Enter>";
                "rr" = ":reply -a<Enter>";
                "rq" = ":reply -aq<Enter>";
                "Rr" = ":reply<Enter>";
                "Rq" = ":reply -q<Enter>";
                "c" = ":cf<space>";
                "$" = ":term<space>";
                "!" = ":term<space>";
                "|" = ":pipe<space>";
                "/" = ":search -a<space>";
                "\\" = ":filter -a <space>";
                "n" = ":next-result<Enter>";
                "N" = ":prev-result<Enter>";
                "<Esc>" = ":clear<Enter>";
                "s" = ":split<Enter>";
                "S" = ":vsplit<Enter>";
                "pl" = ":patch list<Enter>";
                "pa" = ":patch apply <Tab>";
                "pd" = ":patch drop <Tab>";
                "pb" = ":patch rebase<Enter>";
                "pt" = ":patch term<Enter>";
                "ps" = ":patch switch <Tab>";
            };
            view = {
                "/" = ":toggle-key-passthrough<Enter>/";
                "q" = ":close<Enter>";
                "O" = ":open<Enter>";
                "o" = ":open<Enter>";
                "S" = ":save<space>";
                "|" = ":pipe<space>";
                "D" = ":delete<Enter>";
                "A" = ":archive flat<Enter>";
                "<C-l>" = ":open-link <space>";
                "f" = ":flag -t<Enter>";
                "rr" = ":reply -a<Enter>";
                "rq" = ":reply -aq<Enter>";
                "Rr" = ":reply<Enter>";
                "Rq" = ":reply -q<Enter>";
                "H" = ":toggle-headers<Enter>";
                "<C-k>" = ":prev-part<Enter>";
                "<C-Up>" = ":prev-part<Enter>";
                "<C-j>" = ":next-part<Enter>";
                "<C-Down>" = ":next-part<Enter>";
                "J" = ":next<Enter>";
                "<C-Right>" = ":next<Enter>";
                "K" = ":prev<Enter>";
                "<C-Left>" = ":prev<Enter>";
            };
            "view::passthrough" = {
                "$noinherit" = "true";
                "$ex" = "<C-x>";
                "<Esc>" = ":toggle-key-passthrough<Enter>";
            };
            compose = {
                "$noinherit" = "true";
                "$ex" = "<C-x>";
                "$complete" = "<C-o>";
                "<C-k>" = ":prev-field<Enter>";
                "<C-Up>" = ":prev-field<Enter>";
                "<C-j>" = ":next-field<Enter>";
                "<C-Down>" = ":next-field<Enter>";
                "<A-p>" = ":switch-account -p<Enter>";
                "<C-Left>" = ":switch-account -p<Enter>";
                "<A-n>" = ":switch-account -n<Enter>";
                "<C-Right>" = ":switch-account -n<Enter>";
                "<tab>" = ":next-field<Enter>";
                "<backtab>" = ":prev-field<Enter>";
                "<C-p>" = ":prev-tab<Enter>";
                "<C-PgUp>" = ":prev-tab<Enter>";
                "<C-n>" = ":next-tab<Enter>";
                "<C-PgDn>" = ":next-tab<Enter>";
            };
            "compose::editor" = {
                "$noinherit" = "true";
                "$ex" = "<C-x>";
                "<C-k>" = ":prev-field<Enter>";
                "<C-Up>" = ":prev-field<Enter>";
                "<C-j>" = ":next-field<Enter>";
                "<C-Down>" = ":next-field<Enter>";
                "<C-p>" = ":prev-tab<Enter>";
                "<C-PgUp>" = ":prev-tab<Enter>";
                "<C-n>" = ":next-tab<Enter>";
                "<C-PgDn>" = ":next-tab<Enter>";
            };
            "compose::review" = {
                "y" = ":send<Enter>";
                "n" = ":abort<Enter>";
                "v" = ":preview<Enter>";
                "p" = ":postpone<Enter>";
                "q" = ":choose -o d discard abort -o p postpone postpone<Enter>";
                "e" = ":edit<Enter>";
                "a" = ":attach<space>";
                "d" = ":detach<space>";
            };
            terminal = {
                "$noinherit" = "true";
                "$ex" = "<C-x>";
                "<C-p>" = ":prev-tab<Enter>";
                "<C-n>" = ":next-tab<Enter>";
                "<C-PgUp>" = ":prev-tab<Enter>";
                "<C-PgDn>" = ":next-tab<Enter>";
            };
        };
        templates = {
            quoted_reply = ''
          {{(index .OriginalFrom 0).Name}}, {{dateFormat (.OriginalDate | toLocal) "Jan 02, 2006 at 15:04"}}:
          {{ if eq .OriginalMIMEType "text/html" -}}
          {{- exec `${pkgs.w3m}/bin/w3m -dump -o display_link_number=1 -T text/html` .OriginalText | quote -}}
          {{- else -}}
          {{- .OriginalText | quote -}}
          {{- end}}
            '';
        };
        extraConfig = {
            general = {
                term = "xterm-256color";
                unsafe-accounts-conf = true;
            };
            ui = {
                border-char-vertical = "│";
                border-char-horizontal = "─";
                styleset-name = "catppuccin-mocha";
                timestamp-format = "2006-01-02 15:04";
                sidebar-width = 30;
                mouse-enabled = true;
                dirlist-right = "{{if .Unread}}{{humanReadable .Unread}}{{end}}";
                dirlist-tree = true;
            };
            viewer = {
                pager =
                    "${pkgs.neovim}/bin/nvim -u ${config.xdg.configHome}/nvim/aerc.lua";
            };
            compose = { address-book-cmd = ''notmuch address "%s"''; };
            multipart-converters = {
                "text/html" =
                    "${pkgs.w3m}/bin/w3m -dump -o display_link_number=1 -T text/html";
            };
            filters = {
                "text/plain" = "cat";
                "text/calendar" = "${pkgs.gnome-calendar}/bin/gnome-calendar";
                "message/delivery-status" = "cat";
                "message/rfc822" = "cat";
                "text/html" = "${pkgs.w3m}/bin/w3m -dump -o display_link_number=1 -T text/html";
                "application/x-sh" = "${pkgs.bat}/bin/bat -fP -l sh";
            };
        };
        extraAccounts = {
            Zoho = {
                outgoing = "smtps://email%40thomasave.be@smtp.zoho.com";
                outgoing-cred-cmd = "cat ~/.secrets/Aerc/Zoho.key";
                source =
                    "maildir:///home/server/Storage/Thomas/Mail/email@thomasave.be/";
                check-mail-cmd =
                    "ssh server@10.1 /home/server/Containers/mbsync/sync.sh";
                check-mail-timeout = "60s";
                default = "Inbox";
                from = ''"Thomas Avé" <email@thomasave.be>'';
                cache-headers = true;
            };
            UAntwerpen = {
                outgoing =
                    "ssh server@192.168.1.2 /home/server/Containers/mbsync/send.sh";
                source =
                    "maildir:///home/server/Storage/Thomas/Mail/Thomas.Ave@uantwerpen.be";
                check-mail-cmd =
                    "ssh server@10.1 /home/server/Containers/mbsync/sync.sh";
                check-mail-timeout = "60s";
                default = "Inbox";
                from = ''"Thomas Avé" <Thomas.Ave@uantwerpen.be>'';
                cache-headers = true;
            };
            IMEC = {
                outgoing = "smtp+insecure://ave57%40imec.be@smtp.thomasave.be:1025";
                outgoing-cred-cmd = "cat ~/.secrets/Aerc/IMEC.key";
                source =
                    "maildir:///home/server/Storage/Thomas/Mail/Thomas.Ave@imec.be/";
                check-mail-cmd =
                    "ssh server@10.1 /home/server/Containers/mbsync/sync.sh";
                check-mail-timeout = "60s";
                default = "Inbox";
                from = ''"Thomas Avé" <Thomas.Ave@imec.be>'';
                cache-headers = true;
            };
            NotMuch = {
                source = "notmuch:///home/server/Storage/Thomas/Mail/";
                check-mail-cmd =
                    "ssh server@10.1 /home/server/Containers/mbsync/sync.sh";
                check-mail-timeout = "60s";
                from = ''"Thomas Avé" <Thomas.Ave@uantwerpen.be>'';
                query-map = "${config.xdg.configHome}/aerc/map.conf";
                default = "INBOX";
                cache-headers = true;
            };
        };
    };

    home.file."${config.xdg.configHome}/aerc/map.conf".text =
        "Inbox=tag:inbox and not tag:archived and not tag:deleted";
    home.file."${config.xdg.configHome}/aerc/stylesets/catppuccin-mocha".text = ''
    *.default=true
    *.normal=true

    default.fg=#abb2bf
    default.bg=#11121D
    *.bg=#11121D

    error.fg=#f38ba8
    warning.fg=#fab387
    success.fg=#a6e3a1

    tab.fg=#6c7086
    tab.bg=#181825
    tab.selected.fg=#cdd6f4
    tab.selected.bg=#023269
    tab.selected.bold=true

    part*.selected.bg=#023269

    border.fg=#023269
    border.bold=true

    msglist_unread.bold=true
    msglist_unread.fg=#f9e2af
    msglist_flagged.fg=#e06c75

    msglist_flagged.bold=true
    msglist_result.fg=#89b4fa
    msglist_result.bold=true
    msglist_*.selected.bold=true
    msglist_*.selected.bg=#023269
    msglist_*.bg=#11121D

    dirlist_*.selected.bold=true
    dirlist_*.selected.bg=#023269
    dirlist_*.bg=#181825

    statusline_default.fg=#9399b2
    statusline_default.bg=#2C3043
    statusline_error.bold=true
    statusline_success.bold=true

    spinner.bg=#11121D

    [viewer]
    header.bg=#181825
    url.fg=#89b4fa
    url.underline=true
    header.bold=true
    signature.dim=true
    diff_meta.bold=true
    diff_chunk.fg=#89b4fa
    diff_chunk_func.fg=#89b4fa
    diff_chunk_func.bold=true
    diff_add.fg=#a6e3a1
    diff_del.fg=#f38ba8
    quote_*.fg=#6c7086
    quote_1.fg=#9399b2
    '';
}
