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

let
  inherit (pkgs) stdenv;
  lsOptions = if stdenv.isDarwin then "-p" else "-v --group-directories-first";
  lsIsoDate = if stdenv.isDarwin then "" else "--time-style=long-iso";
in
{
  home.file = {
    ".rm_recycle_home".text = ""; # use trash automatically in home directory
  };
  home.packages = with pkgs; [
    fzf
    ghq
    git
    git-lfs
    zsh-completions
  ];
  xdg.configFile.zsh = {
    recursive = true;
    source = ../zsh/.config/zsh;
  };

  programs.zsh = {
    enable = true;

    enableAutosuggestions = true;
    enableCompletion = true;
    defaultKeymap = "emacs";

    dotDir = ".config/zsh";

    history = {
      expireDuplicatesFirst = true;
      extended = true;
      path = ".cache/zsh/history";
      save = 20000;
      size = 10000;
    };

    localVariables = {
      ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE = "fg=8";
    };

    plugins = [
      {
        name = "kubectl-aliases";
        src = pkgs.fetchFromGitHub {
          owner = "alanpearce";
          repo = "kubectl-aliases";
          rev = "cec7c637e0cd17e17b8b036ad5108cd650fac4c8";
          # date = 2019-11-15T14:35:38+01:00;
          sha256 = "1l4cwdypxwz0qlwab14in6d6zi81sqmbhj7ninsngpvhx4ldy313";
        };
      }
      {
        name = "cd-gitroot";
        src = pkgs.fetchFromGitHub {
          owner = "mollifier";
          repo = "cd-gitroot";
          rev = "a1d59511eba2b99935635b2efef00f8d2363aecd";
          # date = 2016-03-23T18:29:15+09:00;
          sha256 = "1l0fb24vcgd7vgya1r47h2bfczkxrb3mxn791l305f5d350wvqfz";
        };
      }
      {
        name = "careful_rm";
        src = pkgs.fetchFromGitHub {
          owner = "MikeDacre";
          repo = "careful_rm";
          rev = "f62506b2b6a9ec312dfd7fc8148eb3920a1e790f";
          # date = 2019-04-14T10:35:56-07:00;
          sha256 = "1pjlrdwv2509cgcdsfxff8cycfhj6z0y4jbjr98209bwi3ds7665";
        };
      }
      {
        name = "calc";
        src = pkgs.fetchFromGitHub {
          owner = "arzzen";
          repo = "calc.plugin.zsh";
          rev = "3e0b67fa0fecb8f06d5cf3f80075b53c832c1813";
          # date = 2019-09-17T09:15:39+02:00;
          sha256 = "1i0c5szvfc1f3rnf8dp57v7c15a4lf6lkn1l0qdbbq06wkcg2zpq";
        };
      }
      {
        name = "better-npm-completion";
        src = pkgs.fetchFromGitHub {
          owner = "lukechilds";
          repo = "zsh-better-npm-completion";
          rev = "b61f6bb4e640728c42ae84ca55a575ee88c60fe8";
          # date = 2017-07-02T09:59:33+01:00;
          sha256 = "00c1gdsam0z6v09fvz7hyl0zgmgnwbf59i1yrbkrz08frjlr16ax";
        };
      }
      {
        name = "zsh-nix-shell";
        src = pkgs.fetchFromGitHub {
          owner = "chisui";
          repo = "zsh-nix-shell";
          rev = "b2609ca787803f523a18bb9f53277d0121e30389";
          # date = 2019-04-22T22:00:26+02:00;
          sha256 = "01w59zzdj12p4ag9yla9ycxx58pg3rah2hnnf3sw4yk95w3hlzi6";
        };
      }
      {
        name = "you-should-use";
        src = pkgs.fetchFromGitHub {
          owner = "MichaelAquilina";
          repo = "zsh-you-should-use";
          rev = "06de2be797cc1c987d196c93d91ce165fa9bf5f1";
          # date = 2019-08-24T17:12:44+01:00;
          sha256 = "1qrm7f4my1z7d3h3zgizw1wajk20mkriq0zhxqh36p8q24py319g";
        };
      }
      {
        name = "agkozak-zsh-prompt";
        src = pkgs.fetchFromGitHub {
          owner = "agkozak";
          repo = "agkozak-zsh-prompt";
          rev = "v3.3.0";
          # date = 2019-07-23T18:45:52-07:00;
          sha256 = "0f8v1ih4205lgghcssbvb3clpymjj2f134cab5vrggdaapcs10xq";
        };
      }
      {
        name = "anyframe";
        src = pkgs.fetchFromGitHub {
          owner = "mollifier";
          repo = "anyframe";
          rev = "598675303044df8e9d04722f3adff4f63a238922";
          # date = 2017-07-19T21:59:49+09:00;
          sha256 = "08bjm1dd2mpv8rk8x6yvm6gj490rgimmiq7ln4jr5hik2k3mm82r";
        };
      }
    ] ++ lib.optionals stdenv.isDarwin [
      {
        name = "tumult";
        src = pkgs.fetchFromGitHub {
          owner = "unixorn";
          repo = "tumult.plugin.zsh";
          rev = "e74db60be21dfebaa89b84fb9a45939814573c8c";
          # date = 2019-03-07T08:53:24-07:00;
          sha256 = "1328g3h51478ai25niv9qgf8bjgg3cw98zdqfznrbgw0k3qj66zk";
        };
      }
    ];

    shellAliases = {
      l = "ls ${lsOptions} -Bp";
      l1="ls -1";
      ls="ls ${lsOptions} -hF";
      la="ls ${lsOptions} -hA";
      ll="ls ${lsOptions} ${lsIsoDate} -hl";
      lal="ll -A";
      lla="lal";
      llr="ll -t";

      cg = "cd-gitroot";
      cdg = "cd-gitroot";

      https = "http --default-scheme https";
      kns = "kubens";
      kx = "kubectx";

      history = "fc -l $(( $LINES - 2 ))";
      hist-freq-lines = "fc -l -10000 | cut -d' ' -f4- | sort | uniq -c | sort -g | tail -n100 | less";
      hist-freq-commands = "fc -l -10000 | cut -d' ' -f4 | sort | uniq -c | sort -g | tail -n10 | less";
      wprop = "xprop | egrep '^WM_(CLASS|NAME|WINDOW_ROLE|TYPE)'";

      # Enable the following commands to support aliases.
      sudo = "sudo ";
      watch = "watch ";

      d = "docker";
      db = "docker build";
      dr = "docker run";
      di = "docker image";
      dj = "docker pull";
      dk = "docker push";

      dcb = "dc build";
      dcd = "dc down";
      dcj = "dc pull";
      dck = "dc push";
      dcl = "dc logs";
      dclf = "dc logs -f";
      dcu = "dc up";
      dcud = "dc up -d";
      dcr = "dc restart";

      g = "rg";

      ga = "git add";
      gs = "git st";
      gd = "git diff";
      gf = "git fetch";
      gk = "git push";
      gkf = "git push --force-with-lease";
      gj = "git pull";
      gl = "git lg";
      gm = "git merge";
      ge = "git remote";
      gr = "git rebase";
      gz = "git stash";
      gzl = "git stash list";
      gzp = "git stash pop";
      gdt = "git difftool";
      grl = "git reflog";
      gri = "git rebase --interactive";
      gfa = "git fetch --all";
      grs = "git reset";
      grsh = "git reset --hard";
      gsh = "git show";
      gsm = "git submodule";
      gci = "git commit";
      gx = "git restore";
      gb = "git switch";
      gbr = "git br";
      gbrc = "git checkout -b";
      gbrd = "git branch --delete";
      gbrm = "git branch --move";
      gmup = "git mup";
      grup = "git rup";

      hos = "home-manager switch";
      hon = "home-manager news";
      hoh = "home-manager-help";

      nic = "nix-channel";
      nica = "nix-channel --add";
      nicl = "nix-channel --list";
      nicu = "nix-channel --update";
      nicr = "nix-channel --remove";
      nicb = "nix-channel --rollback";
      snic = "sudo nix-channel";
      snica = "sudo nix-channel --add";
      snicl = "sudo nix-channel --list";
      snicu = "sudo nix-channel --update";
      snicr = "sudo nix-channel --remove";
      snicb = "sudo nix-channel --rollback";
      n = "nix-env";
      ni = "nix-env -iA";
      nq = "nix-env -q";
      ne = "nix-env -e";
      nup = "nix-env -u";
      nlg = "nix-env --list-generations";
      snlg = "sudo nix-env --list-generations --profile /nix/var/nix/profiles/system";
      ngc = "nix-collect-garbage --delete-older-than 14d";
      sngc = "sudo nix-collect-garbage --delete-older-than 14d";
    };

    envExtra = ''
      if [[ ''${path[(I)$HOME/.local/bin ]} ]]
      then
        path+=($HOME/.local/bin)
      fi

      if [[ ''${path[(I)$HOME/go/bin ]} ]]
      then
        path+=($HOME/go/bin)
      fi
    '';

    initExtra = ''
      typeset -T GHQ_ROOT ghq_root
      export GHQ_ROOT="$HOME/projects:$HOME/go/src:$HOME/quicklisp/local-projects"

      function hist-freq-subcommands () {
        fc -l -m "$1*" -10000 | cut -d' ' -f4- | sort | uniq -c | sort -g | tail -n100 | less
      }

      source ${pkgs.fzf}/share/fzf/key-bindings.zsh

      source ${pkgs.google-cloud-sdk}/google-cloud-sdk/completion.zsh.inc

    '' + builtins.readFile ../zsh/.config/zsh/.zshrc;
  };
}