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

let
  laptop = builtins.readFile "/etc/hostname" == "syl\n";
in
{
  home.file.".config/hypr/hyprlock.conf" = {
    text = ''
      auth {
         fingerprint:enable = false
      }
      background {
         monitor =
         path = /home/chris/pics/wallpapers/tech.jpg   # only png supported for now
         blur_passes = 3 # 0 disables blurring
         blur_size = 12
         noise = 0.1
         contrast = 0.8916
         brightness = 0.8172
         vibrancy = 0.1696
         vibrancy_darkness = 0.0
      }
      input-field {
         monitor =
         size = 400, 50
         outline_thickness = 2
         dots_size = 0.33 # Scale of input-field height, 0.2 - 0.8
         dots_spacing = 0.15 # Scale of dots' absolute size, 0.0 - 1.0
         dots_center = true
         dots_rounding = -1 # -1 default circle, -2 follow input-field rounding
         outer_color = rgb(90, 247, 142)
         inner_color = rgb(40, 42, 54)
         font_color = rgb(90, 247, 142)
         fade_on_empty = true
         fade_timeout = 1000 # Milliseconds before fade_on_empty is triggered.
         placeholder_text = <i>Input Password...</i> # Text rendered in the input box when it's empty.
         hide_input = false
         rounding = -1 # -1 means complete rounding (circle/oval)
         check_color = rgb(255, 159, 67)
         fail_color = rgb(255, 92, 87) # if authentication failed, changes outer_color and fail message color
         fail_text = <i>$FAIL <b>($ATTEMPTS)</b></i> # can be set to empty
         fail_transition = 300 # transition time in ms between normal outer_color and fail_color
         capslock_color = -1
         numlock_color = -1
         bothlock_color = -1 # when both locks are active. -1 means don't change outer color (same for above)
         invert_numlock = false # change color if numlock is off
         swap_font_color = false # see below
        
         position = 0, -20
         halign = center
         valign = center
      }
    '';
  };
  home.file.".config/hypr/hypridle.conf" = {
    text = ''
    general {
      lock_cmd = pidof hyprlock || hyprlock       # avoid starting multiple hyprlock instances.
      before_sleep_cmd = loginctl lock-session    # lock before suspend.
      after_sleep_cmd = hyprctl dispatch dpms on && sleep 5 && systemctl --user restart jellyfin-mpv-shim
     }

     listener {
       timeout = 150                                # 2.5min.
       on-timeout = brightnessctl -s set 10         # set monitor backlight to minimum, avoid 0 on OLED monitor.
       on-resume = brightnessctl -r                 # monitor backlight restore.
     }
     ${if laptop then ''
     listener {
       timeout = 300                                 # 5min
       on-timeout = loginctl lock-session            # lock screen when timeout has passed
     }
     '' else ""}
     listener {
       timeout = 330                                 # 5.5min
       on-timeout = hyprctl dispatch dpms off        # screen off when timeout has passed
       on-resume = hyprctl dispatch dpms on          # screen on when activity is detected after timeout has fired.
     }
     ${if laptop then ''
     listener {
       timeout = 1800                                # 30min
       on-timeout = systemctl suspend                # suspend pc
       on-resume = systemctl --user restart jellyfin-mpv-shim.service # restart jellyfin
     }
     '' else ""}
    '';
  };
  home.file.".config/hypr/hyprpaper.conf" = {
    source = ../../.config/hypr/hyprpaper.conf;
  };

  wayland.windowManager.hyprland = {
    enable = true;
    systemd.enable = false;
    systemd.variables = ["--all"];
    settings = {
      monitor = [
        "eDP-1,2256x1504@60,0x0,1.566667"
        "HDMI-A-1,preferred,0x0,1"
        "DP-1,preferred,1080x0,1.5"
        "DP-2,preferred,3640x0,1"
        "HDMI-A-1,transform,1"
      ];
      workspace = [
        "1,name:code,monitor:eDP-1,1,default:true,persistent:true"
        "2,name:browse,monitor:DP-1,default:true,persistent:true"
        "9,name:monitor,monitor:DP-2,gapsout:0,rounding:false,default:true,persistent:true"
        "1,monitor:DP-1,1,default:true,persistent:true"
        "special,monitor:DP-1,1,default:true"
      ];
      input = {
        repeat_rate = 140;
        repeat_delay = 180;
        natural_scroll = 0;

        sensitivity = 0.75;
        accel_profile = "adaptive";

        follow_mouse = 2;
        float_switch_override_focus = 0;
        
        touchpad = {
          natural_scroll = 1;
          clickfinger_behavior = 1;
        };
      };

     #debug = {
     #  enable_stdout_logs = false;
     #};

      general = {
        gaps_in = 15;
        gaps_out = 20;
        border_size = 0;
        "col.active_border" = "0x66ee1111";
        "col.inactive_border" = "0x66333333";

        #damage_tracking=full # leave it on full unless you hate your GPU and want to make it suffer
        layout = "master";
        # no_cursor_warps = false;
      };

      cursor = {
        inactive_timeout = 3;
      };

      gestures = {
        workspace_swipe = true;
        workspace_swipe_fingers = 3;
      };

      decoration = {
        rounding = 18;
        # multisample_edges=true
        active_opacity = 0.90;
        inactive_opacity = 0.70;
        fullscreen_opacity = 1.0;

        blur = {
          enabled = true;
          size = 15; # minimum 1;
          passes = 3; # minimum 1, more passes = more resource intensive.;
          new_optimizations = true;
          ignore_opacity = false;
          special = false;
          noise = 0.2;
        };

        shadow = {
          enabled = true;
          render_power = 1;
          color = "rgba(00000099)";
          offset = "5 5";
          scale = 0.998;
          range = 20;
        };
        dim_inactive = true;
        dim_strength = 0.2;
        dim_special = 0;
      };

      group = {
        "col.border_active" = "rgba(282a3600) rgba(57c7ff00) rgba(5af78e00) 45deg";
        "col.border_inactive" = "rgba(282a3655) rgba(57c7ff55) rgba(5af78e55) 45deg";
        groupbar = {
          rounding = 16;
          gradients = true;
          gradient_rounding = 12;
          "col.active" = "rgba(282a36ff)";
          "col.inactive" = "rgba(282a3655)";
        };
      };

      bezier = "snapslide,0.07,0.66,0.04,1.02";

      animations = {
        enabled = 1;
        animation = [
          "windows,1,2,snapslide"
          "fadeIn,1,2,snapslide"
          "workspaces,1,2,snapslide"
          "specialWorkspace,1,2,snapslide,slidevert"
        ];
      };

      dwindle = {
        pseudotile = 0; # enable pseudotiling on dwindle
      };

      master = {
        special_scale_factor = 0.8;
        mfact = 0.65;
        # new_is_master = false;
        new_on_top = false;
      };

      # misc:disable_autoreload = true
      misc = {
        focus_on_activate  =  true;
        mouse_move_enables_dpms  =  true;
        key_press_enables_dpms  =  true;
        disable_hyprland_logo  =  true;
      };

      # example window rules
      # for windows named/classed as abc and xyz
      windowrule = [
        "opaque,class:ff"
        # if !laptop then "workspace 1,mpv"
        "float,class:org.kde.dolphin"
        "float,class:thunar"
        "float,class:title:dadired"
        "float,class:com.system76.CosmicFiles"
        "float,class:org.freedesktop.impl.portal.desktop.kde"
        "float,class:nm-tray"
        "size 60% 60%,class:org.kde.dolphin"
        "size 60% 60%,class:thunar"
        "size 60% 60%,title:dadired"
        "size 60% 60%,class:com.system76.CosmicFiles"
        "size 60% 60%,class:org.freedesktop.impl.portal.desktop.kde"
        "float,class:mpv"
        "float,title:Picture-in-Picture"
        "center,title:Picture-in-Picture"
        "size ${if laptop then "90% 76%" else "85% 85%"},class:mpv"
        "${if laptop then "workspace 1,class:idk #dummy rule" else "workspace 9,class:mpv"}"
        "${if laptop then "workspace 1,class:idk #dummy rule" else "workspace 9,title:Picture-In-Picture"}"
        "${if laptop then "workspace 1,class:idk #dummy rule" else "fullscreen,title:Picture-In-Picture"}"
        "center,class:mpv"
        "opaque,class:mpv"
        "nodim,class:mpv"
        "float,class:pulsemixer"
        "workspace 2,class:ff"
        "workspace 2,class:vivaldi-stable"
        "workspace 2,class:org.qutebrowser.qutebrowser"
        "workspace 1,class:emacs"
        "float,class:btop"
        "size 70% 70%,class:btop"
        "center,class:btop"
        "float,class:lumina"
        "size 80% 80%,class:lumina"
        "center,class:lumina"
        "opaque,class:lumina"
        "float,class:'org.chriscochrun.lumina'"
        "size 80% 80%,class:'org.chriscochrun.lumina'"
        "center,class:'org.chriscochrun.lumina'"
        "opaque,class:'org.chriscochrun.lumina'"
        "workspace ${if laptop then "1" else "3"}, title:presentation-window"
      ];

      # example binds
      bind = [
        "SUPER,RETURN,exec,alacritty"
        "SUPER,C,killactive,"
        "SUPERSHIFT,Q,exit,"
        "SUPERSHIFT,D,exec,dolphin"
        "SUPERSHIFT,F,togglefloating,"
        "SUPER,w,exec,rofi -i -show window"
        "SUPER,E,exec,/home/chris/bin/emacslof"
        "SUPER,d,exec,emacsclient -c -F '`(name . \"dadired\")' -e '(dired-jump)'"
        "SUPER,v,exec,cliphist list | rofi -p '󱃔 ' -dmenu | cliphist decode | wl-copy"
        "SUPER,B,exec,/home/chris/bin/qblof"
        "SUPER,A,exec,alacritty --class pulsemixer -e pulsemixer"
        "SUPERCTRL,i,exec,alacritty --class btop -e btop"
        "SUPERSHIFT,p,exec,${pkgs.hyprshot}/bin/hyprshot -m region"
        "SUPERALT,p,exec,${pkgs.hyprshot}/bin/hyprshot -m window"
        "SUPERCTRL,p,exec,${pkgs.hyprshot}/bin/hyprshot -m active"
        "SUPERSHIFT,mouse_down,exec,zoomin in"
        "SUPERSHIFT,mouse_up,exec,zoomin out"
        "SUPERALT,r,exec,zoomin in"
        "SUPERALT,m,exec,zoomin out"
        "SUPERALT,f,exec,hyprctl keyword cursor:zoom_factor 1.0"

        "SUPER,P,exec,/home/chris/bin/rbw.sh"
        "SUPER,M,fullscreen,1"
        "SUPER,F,fullscreen,0"

        "SUPER,h,movefocus,l"
        "SUPER,l,movefocus,r"
        "SUPER,k,layoutmsg,cycleprev"
        "SUPER,j,layoutmsg,cyclenext"
        "SUPERSHIFT,h,movewindow,l"
        "SUPERSHIFT,l,movewindow,r"
        "SUPERALT,j,layoutmsg,orientationnext"
        "SUPERALT,k,layoutmsg,orientationprev"
        "SUPERSHIFT,k,layoutmsg,swapprev"
        "SUPERSHIFT,j,layoutmsg,swapnext"
        "SUPERCTRL,l,splitratio,+0.05"
        "SUPERCTRL,h,splitratio,-0.05"
        "SUPERSHIFT,c,centerwindow"
        "SUPERSHIFT,Right,moveintogroup,right"
        "SUPERSHIFT,Left,moveintogroup,left"
        "SUPERSHIFT,Up,moveintogroup,up"
        "SUPERSHIFT,Down,moveintogroup,down"
        "SUPERSHIFT,g,togglegroup"

        "SUPER,1,workspace,1"
        "SUPER,2,workspace,2"
        "SUPER,3,workspace,3"
        "SUPER,4,workspace,4"
        "SUPER,5,workspace,5"
        "SUPER,6,workspace,6"
        "SUPER,7,workspace,7"
        "SUPER,8,workspace,8"
        "SUPER,9,workspace,9"
        "SUPER,0,workspace,10"

        "SUPERALT,l,workspace,m+1"
        "SUPERALT,h,workspace,m-1"

        "SUPERSHIFT,1,movetoworkspace,1"
        "SUPERSHIFT,2,movetoworkspace,2"
        "SUPERSHIFT,3,movetoworkspace,3"
        "SUPERSHIFT,4,movetoworkspace,4"
        "SUPERSHIFT,5,movetoworkspace,5"
        "SUPERSHIFT,6,movetoworkspace,6"
        "SUPERSHIFT,7,movetoworkspace,7"
        "SUPERSHIFT,8,movetoworkspace,8"
        "SUPERSHIFT,9,movetoworkspace,9"
        "SUPERSHIFT,0,movetoworkspace,10"

        "SUPER,o,movewindow,mon:l"
        "SUPER,y,togglespecialworkspace"
        "SUPER,i,movewindow,left"
        "SUPERSHIFT,p,pin"
        "SUPER,n,movetoworkspace,special"
        "SUPERALT,a,movetoworkspace,special:agenda"
        "SUPER,g,togglespecialworkspace,agenda"

        "SUPERSHIFT,w,exec,waybar.sh"
        "SUPERALT,n,exec,eww update rightside=true"
        "SUPERCTRL,f,exec,forms-fill.sh"
      ];

      binde = [
        ", XF86AudioMute, exec, pamixer -t"
        ", XF86AudioRaiseVolume, exec, /home/chris/bin/volup"
        ", XF86AudioLowerVolume, exec, /home/chris/bin/voldown"
        ",F1, exec, pamixer -t"
        ",F3, exec, /home/chris/bin/volup"
        ",F2, exec, /home/chris/bin/voldown"
        ", XF86MonBrightnessUp, exec, brightnessctl s +10%"
        ", XF86MonBrightnessDown, exec, brightnessctl s 10%-"
      ];

      bindr = [
        "SUPER,Super_L,exec,rofi -no-lazy-grab -modi combi -show combi -combi-modi 'drun,run'"
        "ALT,Alt_R,exec,rofi -i -show window"
      ];

      bindm = [
        "SUPER,mouse:272,movewindow"
        "SUPER,mouse:273,resizewindow"
      ];

      # Blur waybar
      # blurls = "gtk-layer-shell";
      layerrule = [
        "blur, notifications"
        # "blur, panel"
        "ignorezero, notifications"
        "ignorezero, panel"
        "ignorezero, gtk-layer-shell"
        "animation snapslide, gtk-layer-shell"
        "animation snapslide, panel"
      ];
      # blurls=notifications
      
      exec-once = [
        "kwalletd6"
        # "eww daemon"
        "hyprpaper"
        # "eww open ${if laptop then "bar0" else "bar1"}"
        # "dunst"
        "waybar.sh"
        "swww-daemon"
        "rbw-agent"
        "hyprctl dispatch --batch 'splitratio 1; splitration -0.35'"
        "dbus-update-activation-environment --systemd --all"
        "/usr/lib/kdeconnectd"
        "systemctl --user restart jellyfin-mpv-shim"
        "systemctl --user restart nextcloud-client"
        # "nextcloud"
        "kdeconnect-indicator"
        "wl-paste --watch cliphist store"
        "hyprctl setcursor phinger-cursors-light 24"
        "nm-tray"
        "swww img /home/chris/pics/wallpapers/tech.jpg --resize crop -t grow --transition-bezier .14,0,.14,.99"
        # "emacs --daemon"
      ];
      xwayland = {
        force_zero_scaling = true;
      };
    };
  };
}