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

{
  # Display manager
  services.xserver.enable = true;
  services.xserver.displayManager = {
    defaultSession = "sway";
    lightdm = {
      enable = true;
      autoLogin = {
        enable = true;
        user = "coolneng";
      };
      greeter.package = pkgs.lightdm_gtk_greeter;
    };
  };

  # Window manager
  programs.sway = {
    enable = true;
    extraPackages = with pkgs; [
      xwayland
      swaylock
      swayidle
      swaybg
      rofi
      waybar
      clipman
      wl-clipboard
      grim
      slurp
      light
      imv
      kanshi
      kitty
    ];
    extraSessionCommands = ''
      export MOZ_ENABLE_WAYLAND=1
      export MOZ_DBUS_REMOTE=1
      export GRIM_DEFAULT_DIR=/home/coolneng/Photos/Screenshots
      export _JAVA_AWT_WM_NONREPARENTING=1
      export QT_QPA_PLATFORM=wayland
      export QT_WAYLAND_DISABLE_WINDOWDECORATION="1"
      export SDL_VIDEODRIVER=wayland
      export GTK_THEME=Adapta-Eta
    '';
  };

  # Backlight
  programs.light.enable = true;

  # Blue light filter
  location = {
    latitude = 35.89;
    longitude = -5.32;
    provider = "manual";
  };

  services.redshift = {
    package = pkgs.redshift-wlr;
    enable = true;
    temperature = {
      day = 6500;
      night = 5300;
    };
  };

  # Bar
  programs.waybar.enable = true;

  # GPG agent
  programs.gnupg.agent = {
    enable = true;
    pinentryFlavor = "gtk2";
  };

  # Tmux
  programs.tmux = {
    enable = true;
    historyLimit = 5000;
    keyMode = "vi";
    shortcut = "x";
    terminal = "screen-256";
    baseIndex = 1;
    extraConfig = ''
      ## set terminal bell
      set -g bell-action any

      # i3 split bindings
      bind g split-window -h
      bind v split-window -v
      unbind '"'
      unbind %
      unbind m

      # vim split navigation
      bind h select-pane -L
      bind j select-pane -D
      bind k select-pane -U
      bind l select-pane -R

      # equalize splits with x
      unbind space
      bind space select-layout even-vertical

      # window switching
      unbind n
      unbind c
      bind c new-window
      unbind ,
      unbind .
      bind , previous-window
      bind . next-window
      bind-key n command-prompt "rename-window '%%'"

      # change kill window bind
      unbind &
      unbind x
      bind x kill-session

      # switch to last session
      bind S switch-client -l

      # force a reload of the config file
      unbind r
      bind r source-file ~/.tmux.conf

      set -g status on
      set -g pane-base-index 1
      set -g base-index 1
      set -g set-titles on
      set -g aggressive-resize on
      set-option -g set-titles-string '#{pane_current_command}'
      set-option -g visual-activity on
      set-option -g status-position bottom
      set-option -g renumber-windows on
      set-window-option -g monitor-activity off
      set-option -g allow-rename off

      # copy mode
      unbind [
      bind Escape copy-mode
      bind-key -T copy-mode-vi 'v' send -X begin-selection # Begin selection in copy mode.
      bind-key -T copy-mode-vi 'C-v' send -X rectangle-toggle # Begin selection in copy mode.
      bind-key -T copy-mode-vi 'y' send -X copy-selection # Yank selection in copy mode.

      set -g mouse on
      bind -T root WheelUpPane   if-shell -F -t = "#{alternate_on}" "send-keys -M" "select-pane -t =; copy-mode -e; send-keys -M"
      bind -T root WheelDownPane if-shell -F -t = "#{alternate_on}" "send-keys -M" "select-pane -t =; send-keys -M"

      # Colorscheme
      set -g status-justify "centre"
      set -g status "on"
      set -g status-left-style "none"
      set -g message-command-style "fg=#c6c8d1,bg=#2e3244"
      set -g status-right-style "none"
      set -g pane-active-border-style "fg=#454b68"
      set -g status-style "none,bg=#1e2132"
      set -g message-style "fg=#c6c8d1,bg=#2e3244"
      set -g pane-border-style "fg=#2e3244"
      set -g status-right-length "100"
      set -g status-left-length "100"
      setw -g window-status-activity-style "none,fg=#454b68,bg=#1e2132"
      setw -g window-status-separator ""
      setw -g window-status-style "none,fg=#c6c8d1,bg=#1e2132"
      set -g status-left "#[fg=#c6c8d1,bg=#454b68,bold] #S #[fg=#454b68,bg=#2e3244,nobold,nounderscore,noitalics]#[fg=#c6c8d1,bg=#1e2132] #[fg=#1e2132,bg=#1e2132,nobold,nounderscore,noitalics]"
      set -g status-right "#[fg=#1e2132,bg=#1e2132,nobold,nounderscore,noitalics]#[fg=#c6c8d1,bg=#1e2132]  #[fg=#2e3244,bg=#1e2132,nobold,nounderscore,noitalics]#[fg=#c6c8d1,bg=#2e3244] %b %d %Y #[fg=#454b68,bg=#2e3244,nobold,nounderscore,noitalics]#[fg=#c6c8d1,bg=#454b68,bold] %R #{prefix_highlight} "
      setw -g window-status-format "#[fg=#1e2132,bg=#1e2132,nobold,nounderscore,noitalics] #[fg=#c6c8d1] #I  #W #[fg=#1e2132,bg=#1e2132,nobold,nounderscore,noitalics]"
      setw -g window-status-current-format "#[fg=#1e2132,bg=#2e3244,nobold,nounderscore,noitalics]#[fg=#c6c8d1,bg=#2e3244] #I  #W  #[fg=#2e3244,bg=#1e2132,nobold,nounderscore,noitalics]"
    '';
  };

  # Automount external storage
  services.autofs = {
    enable = true;
    timeout = 5;
    autoMaster = let
      mapConf = pkgs.writeText "auto" ''
        usb  -fstype=auto      :/dev/sdb1
        sdcard  -fstype=auto   :/dev/mmcblk0p1
      '';
    in ''
      /media file:${mapConf} --timeout=5
    '';
  };

  # Upgrade Doom Emacs daily
  systemd.user.services.doom-upgrade = {
    description = "Upgrade Doom Emacs";
    wantedBy = [ "default.target" ];
    path = [ pkgs.emacs pkgs.git ];
    serviceConfig = {
      Type = "oneshot";
      ExecStart = "/bin/sh /home/coolneng/.emacs.d/bin/doom -y upgrade";
    };
  };

  systemd.user.timers.doom-upgrade = {
    description = "Daily upgrade of Doom Emacs";
    wantedBy = [ "default.target" ];
    timerConfig = {
      OnCalendar = "20:00:00";
      Unit = "doom-upgrade.service";
    };
  };

}