{ pkgs, lib, ... }:

let
  inherit (import ./const.nix) fontSize homedir gpgid;
  base16-scheme = "atelier-sulphurpool";
  base16-scheme-filename = lib.strings.concatStrings (lib.strings.splitString "-" base16-scheme);
  theme = lib.removeSuffix "\n" (builtins.readFile "${homedir}/.local/share/xtheme");
  xresourcesFile = "${pkgs.base16}/xresources/base16-${base16-scheme-filename}.${theme}.256.xresources";

  # used in polybar and dunst
  colors = { # derived from spacemacs
    "dark" = {
      highlight = "#5d4d7a";
      background = "#292b2e";
      foreground = "#b2b2b2";
    };
    "light" = {
      highlight = "#67b11d";
      background = "#f6f1e1";
      foreground = "#655370";
    };
  };
  xmonadPackages = self: [
    self.xmonad-contrib
    #self.taffybar # fails to build
    self.xmonad-extras
    self.monad-logger
  ];
in
{
  imports = [
    ./common.nix
  ];
  home = {
    pointerCursor = {
      package = pkgs.phinger-cursors;
      name = "phinger-cursors";
    };
    keyboard.options = [ "caps:ctrl_modifier" ];
    sessionVariables = {
      LOCALE_ARCHIVE = "${pkgs.glibcLocales}/lib/locale/locale-archive";
    };
    file = {
      mutt = {
        text = (builtins.readFile ./muttrc) +
          (builtins.readFile ./mutt/solarized.muttrc);
        target = ".muttrc";
      };
      mailcap = {
        source = ./mailcap;
        target = ".mailcap";
      };
      xbindkeys = {
        source = ./xbindkeysrc;
        target = ".xbindkeysrc";
      };
    };

    packages = with pkgs; [
      acpi
      apvlv
      beets
      bind
      blueman
      cmdtree
      dict
      dmenu
      feh
      firefox
      flameshot
      gajim
      glibcLocales # rofi locale fix -- https://github.com/rycee/home-manager/issues/354#issuecomment-415914278
      gnumake
      gnupg
      # keybase-gui
      libnotify
      lshw
      mplayer
      muchsync
      mumble
      nyxt
      pandoc
      pavucontrol
      pinentry
      tdesktop
      thunderbird
      # texlive.combined.scheme-full
      #tor-browser-bundle-bin
      usbutils
      vlc
      xbindkeys
      xclip
      xdotool
      xlayoutdisplay
      xorg.xmodmap
      xsel
      xterm
      yank
      youtube-dl
      zathura
    ];
  };
  fonts.fontconfig.enable = true;

  gtk.enable = false; # this fails because of some missing service file
  gtk.font.name = "Fira Sans";
  gtk.font.size = fontSize;
  gtk.font.package = pkgs.fira;
  gtk.theme.name = "Breeze";
  gtk.iconTheme.name = "hicolor";
  gtk.cursorTheme.name = "breeze_cursors";

  xresources = {
    properties = {
      "XTerm*font" = "-*-FiraMono-medium-r-normal--${toString fontSize}-*-*-*-*-*-iso10646-1";
      "XTerm*faceName" = "FireMono";
      "XTerm*faceSize" = toString fontSize;
      "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 xresourcesFile;
  };

  services = {
    lorri.enable = false;
    emacs.enable = true;

    kbfs.enable = false;
    keybase.enable = false;

    mbsync = {
      frequency = "*:0/5";
      postExec = "${pkgs.notmuch}/bin/notmuch new";
    };

    polybar = {
      # https://github.com/0x746866/dots/blob/master/polybar/config
      enable = true;
      config = {
        "bar/top" = {
          background = colors."${theme}".background;
          font-0 = "FiraSans:size=${toString fontSize}";
          font-1 = "Font Awesome 5:pixelsize=11;1";
          font-2 = "MaterialIcons:size=10:antialias=false;2";
          foreground = colors."${theme}".foreground;
          height = "30";
          module-margin = 1;
          modules-center = "date";
          modules-left = ["ewmh" "volume-bar"];
          modules-right = [ "battery" "cpu" "mem" "temp" ];
          monitor = "\${env:MONITOR:HDMI-2}";
          monitor-fallback = "\${env:MONITOR:eDP-1}";
          radius = 0;
          separator = "|";
          tray-background = colors."${theme}".background;
          tray-detached = false;
          tray-maxsize = 16;
          tray-offset-x = 0;
          tray-offset-y = 0;
          tray-padding = 0;
          tray-position = "right";
          tray-scale = 1;
          width = "100%";
        };
        "module/ewmh" = {
          type = "internal/xworkspaces";
          label-active-foreground = "#fff";
        };
        "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-bar" = {
          type = "internal/alsa";
          master-soundcard = "hw:1";
          bar-volume-font = 2;
          bar-volume-width = 9;
          format-volume = "<label-volume><bar-volume>";
          label-volume = " .) ";
          label-muted = " .) mute";
          label-volume-foreground = colors.${theme}.foreground;
          format-muted-foreground = colors.${theme}.foreground;
          bar-volume-foreground-0 = colors.${theme}.foreground;
          bar-volume-foreground-1 = colors.${theme}.foreground;
          bar-volume-foreground-2 = colors.${theme}.foreground;
          bar-volume-foreground-3 = colors.${theme}.foreground;
          bar-volume-foreground-4 = colors.${theme}.foreground;
          bar-volume-foreground-5 = colors.${theme}.foreground;
          bar-volume-foreground-6 = colors.${theme}.foreground;
          bar-volume-gradient = true;
          bar-volume-indicator = "•";
          bar-volume-fill = "•";
          bar-volume-empty = "·";
          bar-volume-empty-foreground = colors.${theme}.foreground;
        };
        "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";
          interval = 3;
          format-padding = 1;
          format = "<label> <ramp-coreload>";
          label = " %percentage:2%%";
          ramp-coreload-0 = "▂";
          ramp-coreload-1 = "▃";
          ramp-coreload-2 = "▄";
          ramp-coreload-3 = "▅";
          ramp-coreload-4 = "▆";
          ramp-coreload-5 = "▇";
          ramp-coreload-0-foreground = colors.${theme}.foreground;
          ramp-coreload-1-foreground = colors.${theme}.foreground;
          ramp-coreload-2-foreground = colors.${theme}.foreground;
          ramp-coreload-3-foreground = colors.${theme}.foreground;
          ramp-coreload-4-foreground = colors.${theme}.foreground;
          ramp-coreload-5-foreground = colors.${theme}.highlight;
        };
        "module/temp" = {
          type = "internal/temperature";
          interval = 3;
          thermal-zone = 0;
          warn-temperature = 70;
          format = "<ramp> <label>";
          format-padding = 1;
          label = "%temperature-c%";
          ramp-0 = "_";
          ramp-1 = ".";
          ramp-2 = ":";
          ramp-3 = "|";
          ramp-4 = "!!";
          ramp-0-foreground = colors.${theme}.foreground;
          ramp-1-foreground = colors.${theme}.foreground;
          ramp-2-foreground = colors.${theme}.foreground;
          ramp-3-foreground = colors.${theme}.foreground;
          ramp-4-foreground = colors.${theme}.highlight;
          format-warn = "<label-warn>";
          label-warn = "  %temperature-c%";
          label-warn-padding = 1;
          label-warn-foreground = colors.${theme}.highlight;
        };
        "module/mem" = {
          type = "internal/memory";
          interval = 3;
          format = "<label>";
          label-padding = 1;
          label = " |[]| %percentage_used%%";
        };
      };
      script = ''
        polybar top &
      '';
    };

    dunst = {
      enable = true;
      settings = {
        global = {
          geometry = "320x5-10+30";
          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 = false;
      latitude = "40.80";
      longitude = "-81.52";
      temperature = {
        # orange = 1000, white = 5000
        day = 5000;
        night = 1800;
      };
    };

    random-background = {
      enable = false;
      enableXinerama = true;
      imageDirectory = "%h/wallpaper";
      interval = "30m";
    };

    gpg-agent = {
      defaultCacheTtl = 7200; # 2 hours
      enable = true;
      enableExtraSocket= true;
      enableScDaemon = true; # smart card support
      enableSshSupport = true;
      maxCacheTtl = 86400; # 24 hours
      pinentryFlavor = "curses";
      verbose = true;
      extraConfig = ''
        allow-emacs-pinentry
      '';
    };
  };

  xsession = {
    enable = true;
    windowManager = {
      xmonad = {
        enable = true;
        extraPackages = xmonadPackages;
        enableContribAndExtras = true;
        config = ./xmonad.hs;
      };
    };
  };

  programs = {
    git.signing.signByDefault = true;
    qutebrowser = {
      enable = true;
      settings = {
        "colors.webpage.darkmode.enabled" =
          if theme == "dark" then true else false;

        "content.user_stylesheets" = "~/.userstyles.css"; # generated in common.nix

        # attempting to fix gl rendering
        "qt.force_software_rendering" = "qt-quick";
        #"backend" = "webkit";
      };
    };

    rofi = {
      enable = true;
      pass.enable = true;
      font = "Fira Mono 16";
      theme = "purple";
      plugins = with pkgs; [
        rofi-calc
        rofi-emoji
        rofi-file-browser
        rofi-menugen
        rofi-mpd
        rofi-pass
        rofi-systemd
      ];
    };
  };

  systemd.user = {
    services = {
      #"gpg-refresh".Service.ExecStart = "${pkgs.gnupg}/bin/gpg --refresh";
      # make it the same as 'systemctl show mpd | grep LimitMEMLOCK"
      # might not be necessary?
      #mpd.Service.LimitMEMLOCK = "2085444096";
    };
    timers = {
      #"gpg-refresh" = {
      #  Install.WantedBy = ["timers.target"];
      #  Timer.OnCalendar = "daily";
      #};
    };
  };
}