{ pkgs, lib, ... }:

let
  homedir = builtins.getEnv "HOME";

  base16-scheme = "solarized";
  base16 = pkgs.stdenv.mkDerivation {
    name = "base16-builder";
    src = builtins.fetchTarball {
      url = "https://github.com/auduchinok/base16-builder/archive/51e3ad4d447fc3f1f539d0bfe33c851728fb6b5f.tar.gz";
      sha256 = "1qir689h38c6jr7fbbqbc3029544zgv41lrrqdcq26kcwxcwjrz1";
    };
    nativeBuildInputs = [pkgs.ruby];
    buildPhase = "${pkgs.ruby}/bin/ruby base16 -s schemes/${base16-scheme}.yml";
    installPhase = ''
      mkdir -p $out
      cp -r output/* $out
    '';
  };
  theme = lib.removeSuffix "\n" (builtins.readFile "${homedir}/.local/share/xtheme");
  xresources = "${base16}/xresources/base16-${base16-scheme}.${theme}.xresources";

  colors = { # derived from spacemacs
    "dark" = {
      highlight = "#5d4d7a";
      background = "#292b2e";
      foreground = "#b2b2b2";
    };
    "light" = {
      highlight = "#67b11d";
      background = "#f6f1e1";
      foreground = "#655370";
    };
  };
in
{
  imports = [ ./common.nix ./email.nix ];
  home.packages = [
    pkgs.gnupg
    pkgs.pinentry
    pkgs.pavucontrol
    pkgs.keybase-gui
  ];
  fonts.fontconfig.enable = true;

  xresources = {
    properties = {
      "XTerm*font" = "-*-FiraMono-medium-r-normal--18-*-*-*-*-*-iso10646-1";
      "XTerm*faceName" = "FireMono";
      "XTerm*faceSize" = "10";
      "XTerm*termName" = "xterm-256color";
      "XTerm*metaSendsEscape" = true;
      "XTerm*utf8" = true;
      #"Xautolock.time:" = 1;
      #"Xautolock.locker:" = "xlock";
      #"Xautolock.corners:" = "+0-0";
      #"Xautolock.cornerdelay:" = 3;
      #"Xautolock.notify:" = 30;
      #"Xautolock.notifier:" = "notify-send -u critical -t 10000 -- 'Locking screen in 30 seconds'";
    };
    extraConfig = builtins.readFile(xresources);
  };

  services = {
    emacs.enable = true;
    network-manager-applet.enable = true;

    kbfs.enable = true;
    keybase.enable = true;

    polybar = {
      enable = true;
      config = {
        "bar/top" = {
          font-0 = "mononoki:size-10";
          monitor = "\${env:MONITOR:HDMI-1}";
          monitor-fallback = "\${env:MONITOR:eDP-1}";
          width = "100%";
          height = "2%";
          radius = 0;
          background = colors."${theme}".background;
          foreground = colors."${theme}".foreground;
          tray-position = "right";
          tray-detached = false;
          tray-maxsize = 16;
          tray-background = colors."${theme}".background;
          tray-offset-x = 0;
          tray-offset-y = 0;
          tray-padding = 0;
          tray-scale = 1;
          module-margin = 1;
          modules-left = "ewmh";
          modules-center = "date";
          modules-right = [ "ethernet" "wifi" "battery" "volume" "cpu" ];
        };
        "module/date" = {
          type = "internal/date";
          internal = 5;
          date = "%Y.%m.%d";
          time = "%H.%M";
          label = "%date%..%time%";
        };
        "module/battery" = {
          type = "internal/battery";
          battery = "BAT0";
          adapter = "AC";
          full-at = 99;
        };
        "module/volume" = {
          type = "internal/alsa";
          interval = 5;
          label-volume = "vol: %percentage%% ";
        };
        "module/ewmh" = {
          type = "internal/xworkspaces";
          enable-click = true;
          enable-scroll = false;
        };
        "module/ethernet" = {
          type = "internal/network";
          interface = "enp1s0";
          label-connected = "eth up: %upspeed:9% down: %downspeed%";
          label-disconnected = "no eth";
        };
        "module/wifi" = {
          type = "internal/network";
          interface = "wlan1";
          label-connected = "wifi up: %upspeed% down: %downspeed%";
          label-disconnected = "no wifi";
        };
        "module/cpu" = {
          type = "internal/cpu";
          label = "cpu: %percentage%%";
        };
        "module/mail" = {
          # doesn't work, can't find "notmuch", idk why
          type = "custom/script";
          exec = "notmuch count tag:inbox and tag:unread";
          label = "mail: %output%";
        };
      };
      script = ''
        #!/usr/bin/env sh
        systemctl --user daemon-reload
        polybar top &
      '';
    };

    dunst = {
      enable = true;
      settings = {
          global = {
          geometry = "320x5-10+30"; # span entire top bar
          transparency = 0;
          frame_color = colors."${theme}".highlight;
          frame_width = 3; # same as xmonad border
          separator_color = "frame";
          font = "Fira Sans";
          background = colors."${theme}".background;
          foreground = colors."${theme}".foreground;
          padding = 10;
          horizontal_padding = 10;
          word_wrap = "yes";
          markup = "full";
          format = "<b>%s</b>\\n%b\\n";
        };

        urgency_normal = {
          background = colors."${theme}".background;
          foreground = colors."${theme}".foreground;
          timeout = 10;
        };
      };
    };

    redshift = {
      enable = true;
      latitude = "33.044444";
      longitude = "-117.271667";
      temperature = {
        day = 6000;
        night = 3500;
      };
    };

    gpg-agent = {
      enable = true;
      enableScDaemon = true;
      defaultCacheTtl = 72000;
      maxCacheTtl = 7200;
      enableSshSupport = false;
      enableExtraSocket = true;
      verbose = true;
      extraConfig = ''
        allow-emacs-pinentry
        # StreamLocalBindUnlink yes
        # RemoteForward /home/ben/.gnupg/S.gpg-agent /home/ben/.gnupg/S.gpg-agent.extra
      '';
    };
  };

  xsession = {
    enable = true;
    pointerCursor = {
      package = pkgs.vanilla-dmz;
      name = "Vanilla-DMZ";
    };
    windowManager = {
      xmonad = {
        enable = true;
        extraPackages = hpkgs: [
          hpkgs.xmonad-contrib
          hpkgs.xmonad-extras
          hpkgs.monad-logger
          #hpkgs.taffybar
        ];
        enableContribAndExtras = true;
        config = ./xmonad.hs;
      };
    };
  };

  programs = {
    ssh = {
      enable = true;
      forwardAgent = true;
    };

    firefox = {
      enable = true;
    };

    chromium = {
      enable = true;
    };

    rofi = {
      enable = true;
    };
  };
}