{ config, lib, pkgs, ... }:
let
  restartEwwBar = monitor: pkgs.writeShellScript "restart-eww-bar-after-kanshi-${toString monitor}" ''

    sleep 1
    ${lib.getExe pkgs.eww} close bar || true
    ${lib.getExe pkgs.eww} open bar --screen ${toString monitor}
  '';
in {

  config = lib.mkIf (config.usercfg.wm == "Wayland") {
    services.kanshi = {
      enable = true;
      systemdTarget = "graphical-session.target";
      settings = [
        {
          profile.name = "tower_0";
          profile.exec = [ "${restartEwwBar 1}" ];
          profile.outputs = [
            {
              criteria = "AOC 24E1W1 GNSKCHA086899";
              mode = "1920x1080@60.000";
              position = "0,0";
              status = "enable";
              scale = 1.0;
              adaptiveSync = true;
            }
            {
              criteria = "AOC 24E1W1 GNSKBHA080346";
              mode = "1920x1080@60.000";
              position = "1920,0";
              status = "enable";
              scale = 1.0;
              adaptiveSync = true;
            }
          ];
        }
        {
          profile.name = "tower_1";
          profile.exec = [ "${restartEwwBar 1}" ];
          profile.outputs = [
            {
              criteria = "AOC 24E1W1 GNSKCHA086899";
              mode = "1920x1080@60.000";
              position = "0,0";
              status = "enable";
              scale = 1.0;
              adaptiveSync = true;
            }
            {
              criteria = "AOC 24E1W1 GNSKBHA080346";
              mode = "1920x1080@60.000";
              position = "0,0";
              status = "enable";
              scale = 1.0;
              adaptiveSync = true;
            }
            {
              criteria = "LG UNKNOWN_TBD";
              mode = "1920x1080@144.000";
              position = "0,0";
              status = "enable";
              scale = 1.0;
              adaptiveSync = true;
            }
          ];
        }
        {
          profile.name = "laptop_0";
          profile.exec = [ "${restartEwwBar 0}" ];
          profile.outputs = [{
            criteria = "LG Display 0x060A Unknown";
            mode = "1920x1080@60.020";
            position = "0,0";
            scale = 1.0;
            status = "enable";
          }];
        }
        {
          profile.name = "laptop_1";
          profile.exec = [ "${restartEwwBar 1}" ];
          profile.outputs = [
            {
              criteria = "CEX CX133 0x00000001";
              mode = "2560x1600@59.972";
              position = "0,0";
              scale = 1.0;
              status = "enable";
            }
            {
              criteria = "LG Display 0x060A Unknown";
              mode = "1920x1080@60.020";
              position = "2560,0";
              scale = 1.0;
              status = "enable";
            }
          ];
        }
        {
          profile.name = "laptop_2";
          profile.exec = [ "${restartEwwBar 1}" ];
          profile.outputs = [
            {
              criteria = "AOC 16G3 1DDP7HA000348";
              mode = "1920x1080@144.000";
              position = "0,0";
              status = "enable";
              scale = 1.0;
              adaptiveSync = true;
            }
            {
              criteria = "LG Display 0x060A Unknown";
              mode = "1920x1080@60.020";
              position = "1920,0";
              scale = 1.0;
              status = "enable";
            }
          ];
        }
      ];
    };
  };
}
