{ config, pkgs, unstablePkgs, ... }:

let
  rangerGit = pkgs.ranger.overrideAttrs (old: {
    version = "git";
    src = pkgs.fetchFromGitHub {
      owner = "ranger";
      repo = "ranger";
      rev = "c7777d558d5b69843b21f986e9af1af311c83887";
      hash = "sha256-DTVoEfc4dAaBTDLFujvWIYj5KHL89YknUiinIs9Rkeg=";
    };
    propagatedBuildInputs = with pkgs.python3Packages; [
      # required for test suite, it's being ran during package build for some reason
      flake8
      pylint
      pytest
      setuptools
    ] ++ old.propagatedBuildInputs;
  });
in
{
  imports = [
    ./unstable-packages.nix
  ];
  environment.systemPackages = with pkgs; [
    rangerGit
    kitty
    zoxide
  ];

  programs.bash.interactiveShellInit = ''
    if test -n "$KITTY_INSTALLATION_DIR"; then
      export KITTY_SHELL_INTEGRATION="enabled,no-sudo"
      source "$KITTY_INSTALLATION_DIR/shell-integration/bash/kitty.bash"
    fi
    eval "''$(zoxide init bash)"
    alias bye=exit
  '';

  environment.etc."xdg/kitty/kitty.conf".text = ''
    font_size 10.0
    scrollback_lines 10000
    window_border_width 0.5
    window_padding_width 3
    ${if config.services.xserver.desktopManager.gnome.enable then "hide_window_decorations yes" else ""}
    background_opacity 0.8
    dynamic_background_opacity yes
  '';

  environment.etc."ranger/rc.conf".text = ''
    eval import os; fm.set_option_from_string("preview_images", "true") if "KITTY_INSTALLATION_DIR" in os.environ else None;
    eval import os; fm.set_option_from_string("preview_images_method", "kitty") if "KITTY_INSTALLATION_DIR" in os.environ else None;
    set vcs_aware true
    set show_hidden true

    alias drag shell ${unstablePkgs.ripdrag}/bin/ripdrag -Axd %p &
    map <C-d> drag
  '';

  environment.etc."ranger/plugins/zoxide.py".source = pkgs.fetchFromGitHub {
    owner = "jchook";
    repo = "ranger-zoxide";
    rev = "281828de060299f73fe0b02fcabf4f2f2bd78ab3";
    hash = "sha256-JEuyYSVa1NS3aftezEJx/k19lwwzf7XhqBCL0jH6VT4=";
  } + /__init__.py;

  programs.direnv.enable = true;

  programs.starship = {
    enable = true;
    settings = {
      format = "$all$line_break\${custom.ranger}$jobs$battery$time$status$os$container$shell$character";
      directory = {
        truncation_length = 5;
        truncation_symbol = "…/";
      };
      username = {
        show_always = true;
      };
      status.disabled = false;
      custom.ranger = {
        when = "test $RANGER_LEVEL";
        command = "echo \"✦\"";
        style = "bold 208";
      };
      # custom.PS1 = {
      #   when = true;
      #   command = "echo -e \"\\x1b\\x5d133;A\\x1b\\x5c\"";
      #   format = "$output";
      #   use_stdin = false;
      # };
    };
  };
}