{ config, lib, pkgs, inputs, ... }:

{
  # Let Home Manager install and manage itself.
  programs.home-manager.enable = true;

  imports = [
    ./packages.nix
    ./unstable.nix
    ./home-minimal.nix
  ];

  home.sessionVariables = {
    MOZ_ENABLE_WAYLAND = 1;
    _JAVA_AWT_WM_NONREPARENTING = 1;
  };

  programs.msmtp.enable = true;
  programs.mbsync.enable = true;
  programs.mu.enable = true;

  accounts.email = {
    accounts.hacc = {
      address = "stuebinm@hacc.space";
      imap.host = "mail.hacc.space";
      mbsync = {
        enable = true;
        create = "maildir";
      };
      msmtp.enable = true;
      mu.enable = true;
      primary = true;
      realName = "stuebinm";
      smtp.host = "mail.hacc.space";
      userName = "stuebinm@hacc.space";
      passwordCommand = "cat ~/mailpasswd";
    };
  };

  programs.bash = {
    shellAliases = {
      "lrz-vpn" = "sudo openconnect https://asa-cluster.lrz.de  -g AnyConnect";
      news = "newsboat";
      pj = "pijul";
    };
  };

  programs.nushell = {
    enable = true;
    settings = {
      startup = [
        "mkdir ~/.cache/starship"
        "starship init nu | save ~/.cache/starship/init.nu"
        "source ~/.cache/starship/init.nu"
      ];
      prompt = "starship_prompt";
    };
  };

  programs.fish = {
    shellAliases = {
      "lrz-vpn" = "sudo openconnect https://asa-cluster.lrz.de";
      news = "newsboat";
      pj = "pijul";
      agenda = "CLICOLOR_FORCE=1 almanac month ~/.cache/feedsync/* | less";
      xmlpretty = "xmllint --format - | bat -l xml";
    };
  };

  programs.kitty = {
    enable = true;
    extraConfig = ''
      background_opacity 0.8
      confirm_os_window_close 0
      allow_remote_control yes
    '';
  };

  programs.neovim = {
    enable = true;
    viAlias = true;
    plugins = with pkgs; [
      (vimPlugins.nvim-treesitter.withPlugins 
        (plugins: [ tree-sitter-grammars.tree-sitter-nix ]))
    ];
    generatedConfigViml = ''
      lua <<EOF
        require'nvim-treesitter.configs'.setup {
        -- One of "all", "maintained" (parsers with maintainers), or a list of languages
        ensure_installed = "all",

        -- Install languages synchronously (only applied to `ensure_installed`)
        sync_install = false,

        -- List of parsers to ignore installing
        ignore_install = { "javascript" },

        highlight = {
          -- `false` will disable the whole extension
          enable = true,

          -- list of language that will be disabled
          -- disable = { "c", "rust" },

          -- Setting this to true will run `:h syntax` and tree-sitter at the same time.
          -- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
          -- Using this option may slow down your editor, and you may see some duplicate highlights.
          -- Instead of true it can also be a list of languages
          additional_vim_regex_highlighting = false,
        },
      }
      EOF
    '';
  };

  systemd.user.services.syncical = let
    feeds = (import inputs.feeds).ical; in {
      Unit.Description = "syncs ical feeds for almanac agenda";
      Service = {
        ExecStart = "${pkgs.gauche}/bin/gosh ${./scripts/sync.scm} ${lib.fold (a: b: a + " " + b) "" feeds}";
        X-ReloadIfChanged = true;
        Environment = ''
          PATH=${pkgs.curl}/bin:${pkgs.coreutils}/bin
        '';
      };
    };

  systemd.user.timers.syncical = {
    Unit.Description = "automatically fetch new calendars";
    Timer = {
      OnCalendar = "hourly";
      Unit = "syncical.service";
    };
    Install.WantedBy = [ "timers.target" ];
  };

  programs.newsboat = {
    enable = true;
    urls = (import ./newsboat-public.nix) ++ (import inputs.feeds).rss;
    browser = pkgs.lynx.outPath + "/bin/lynx";
    reloadThreads = 200;
    queries = {
      unread = ''unread="yes"'';
      non-papers = ''( unread="yes" ) and ( tags !# "paper" )'';
      paper = ''tags # "paper"'';
    };
    extraConfig = ''
      color background white default
      color listnormal white default
      color listnormal_unread magenta default bold
      color listfocus black white bold
      color listfocus_unread magenta white bold
      color info green color0
      color article white default

      ssl-verifypeer no

      macro v set browser "kitty @launch --type tab mpv --ytdl-format='bv[height<=1100]+ba' %u > /dev/null" ; open-in-browser ; set browser "${config.programs.newsboat.browser} %u"
      macro f set browser "${pkgs.xdg_utils}/bin/xdg-open %u >/dev/null 2>&1 &" ; open-in-browser ; set browser "${config.programs.newsboat.browser} %u"

      bind-key j down
      bind-key k up
    '';
  };


  programs.obs-studio = {
    enable = true;
    plugins = let
      move-transition = import obs/move-transition.nix;
      obs-websocket = import obs/obs-websocket.nix;
    in
      with pkgs; [
    ];
  };
  
  programs.gpg = {
    enable = true;
    settings = {
      auto-key-locate = "keyserver";
      with-fingerprint = "";
    };
  };
  services.gpg-agent = {
    enable = true;
    pinentryFlavor = "gtk2";
    maxCacheTtl = 1800;
  };
  
  programs.ssh = {
    forwardAgent = true;
    enable = true;
    matchBlocks = let
      keydir = config.home.homeDirectory + "/.ssh";
      hetznerlogin = match: {user = "root"; identityFile = keydir + "/hetzner"; forwardAgent = true;} // match;
      vpslogin = hostname: hetznerlogin {inherit hostname;};
      lmulogin = hostname: {identityFile = keydir + "/key_lmu"; user = "lmu-user"; } // { inherit hostname;};
    in {
      "parsons" = hetznerlogin {
        hostname = "parsons.hacc.space";
        port = 62954;
        user = "stuebinm";
        identityFile = keydir + "/id_rsa";
      };
      "flora" = vpslogin "flora.stuebinm.eu";
      "chaski" = vpslogin "chaski.stuebinm.eu";
      "cgit" = vpslogin "flora.stuebinm.eu" // {user = "git";};
      "mate" = {
        hostname = "192.168.69.174";
        user = "root";
        identityFile = keydir + "/hetzner";
      };
      "lmucast-test" = lmulogin "lmzveun-h5base.srv.mwn.de";
      "lmucast-prod" = lmulogin "lmzveun-pro-h5base.srv.mwn.de";
      "lmu-salsa" = {
        hostname = "lmzveun-salsa.srv.mwn.de";
        user = "ra72koc";
        identityFile = keydir + "/key_lmu";
      };
      "c3voc" = {
        hostname = "git.c3voc.de";
        user = "git";
        identityFile = keydir + "/id_ed25519";
      };
    };
  };
  
  programs.mpv = {
    enable = true;
    bindings = {
      "up" = "add speed 0.1";
      "down" = "add speed -0.1";
      "n" = "add chapter 1";
      "p" = "add chapter -1";
    };
  };


  programs.mako = {
    enable = true;
    backgroundColor = "#ff00ff80";
    borderColor = "#ff00ff";
    defaultTimeout = 5000; # milliseconds
    padding = "10";
    output = "DVI-I-1";
  };

}