{ config, lib, pkgs, ... }: 
let 
  baseOutput = {
    position = "0,0";
    scale = 1.0;
    adaptiveSync = true;
    status = "enable";
      transform = "normal"; 
  };

in {

  config = lib.mkIf (config.usercfg.wm == "Wayland") {
    services.kanshi = {
      enable = true;
      systemdTarget = "graphical-session.target";
      settings = [
        {output = baseOutput//{
          criteria = "LG Electronics LG ULTRAGEAR+ 511NTDVGC194";
          # mode = "2560x1440@480.168";
          mode = "2560x1440@240.083";
          # mode = "1920x1080x240.084";
        };}
        {output = baseOutput//{
          criteria = "AOC 24E1W1 GNSKCHA086899";
          mode = "1920x1080@60.000";
          transform = "180"; 
        };}
        {output = baseOutput//{
          criteria = "AOC 24E1W1 GNSKBHA080346";
          mode = "1920x1080@60.000";
        };}
        {output = baseOutput//{
          criteria = "LG UNKNOWN_TBD";
          mode = "1920x1080@144.000";
        };}
        {output = baseOutput//{
          criteria = "LG Display 0x060A Unknown";
          mode = "1920x1080@60.020";
        };}
        {output = baseOutput//{
          criteria = "CEX CX133 0x00000001";
          mode = "2560x1600@59.972";
        };}
        {output = baseOutput//{
          criteria = "AOC 16G3 1DDP7HA000348";
          mode = "1920x1080@144.000";
        };}
        {profile = {
          name = "tower_00";
          outputs = [
            {
              criteria = "AOC 24E1W1 GNSKCHA086899";
              position = "0,0";
            }
            {
              criteria = "AOC 24E1W1 GNSKBHA080346";
              position = "0,1080";
            }
            {
              criteria = "LG Electronics LG ULTRAGEAR+ 511NTDVGC194";
              position = "1920,720";
            }
          ];
          exec = [ 
            "${pkgs.eww}/bin/eww open bar --screen 0" 
            "${pkgs.writeShellScript "kanshi-hyprland-init" ''
              #!/usr/bin/env bash
              ${pkgs.hyprland}/bin/hyprctl eval '
                hl.workspace_rule({ workspace = "1", monitor = "DP-1", default = true })
                hl.workspace_rule({ workspace = "2", monitor = "DP-2", default = true })
                hl.workspace_rule({ workspace = "3", monitor = "DP-1", default = true })
                hl.workspace_rule({ workspace = "4", monitor = "DP-1", default = true })
                hl.workspace_rule({ workspace = "5", monitor = "DP-1", default = true })
                hl.workspace_rule({ workspace = "6", monitor = "DP-1", default = true })
                hl.workspace_rule({ workspace = "7", monitor = "DP-1", default = true })
                hl.workspace_rule({ workspace = "8", monitor = "DP-2", default = true })
                hl.workspace_rule({ workspace = "9", monitor = "DP-1", default = true })
                hl.workspace_rule({ workspace = "name:X", monitor = "DP-3", default = true })
              '
              ${pkgs.hyprland}/bin/hyprctl eval 'hl.dispatch(hl.dsp.focus({ monitor = "DP-2" })); hl.dispatch(hl.dsp.focus({ workspace = "2" }));'
              ${pkgs.hyprland}/bin/hyprctl eval 'hl.dispatch(hl.dsp.focus({ monitor = "DP-3" })); hl.dispatch(hl.dsp.focus({ workspace = "name:X" }));'
              #${pkgs.hyprland}/bin/hyprctl eval 'hl.monitor({ output = "DP-3", cm = "hdr" });'

            ''}"
            "${pkgs.awww}/bin/awww restore"
          ];
        };}
        {profile = {
          name = "tower_0";
          outputs = [
            {
              criteria = "AOC 24E1W1 GNSKCHA086899";
              transform = "180"; 
              position = "0,0";
            }
            {
              criteria = "AOC 24E1W1 GNSKBHA080346";
              position = "0,1080";
            }
          ];
          exec = [ 
            
            "${pkgs.writeShellScript "kanshi-hyprland-init" ''
              #!/usr/bin/env bash
              ${pkgs.hyprland}/bin/hyprctl eval '
                hl.workspace_rule({ workspace = "1", monitor = "DP-1", default = true })
                hl.workspace_rule({ workspace = "2", monitor = "DP-2", default = true })
                hl.workspace_rule({ workspace = "3", monitor = "DP-1", default = true })
                hl.workspace_rule({ workspace = "4", monitor = "DP-1", default = true })
                hl.workspace_rule({ workspace = "5", monitor = "DP-1", default = true })
                hl.workspace_rule({ workspace = "6", monitor = "DP-1", default = true })
                hl.workspace_rule({ workspace = "7", monitor = "DP-1", default = true })
                hl.workspace_rule({ workspace = "8", monitor = "DP-2", default = true })
                hl.workspace_rule({ workspace = "9", monitor = "DP-1", default = true })
              '
              ${pkgs.hyprland}/bin/hyprctl eval 'hl.dispatch(hl.dsp.focus({ monitor = "DP-2" })); hl.dispatch(hl.dsp.focus({ workspace = "2" }));'
          
            ''}"
            "${pkgs.awww}/bin/awww restore"
            "${pkgs.eww}/bin/eww open bar --screen 0" 
           ];
        };}
        {profile = {
          name = "tower_1";
          outputs = [
            {
              criteria = "AOC 24E1W1 GNSKCHA086899";
              position = "0,0";
            }
            {
              criteria = "AOC 24E1W1 GNSKBHA080346";
              position = "0,0";
            }
            {
              criteria = "LG UNKNOWN_TBD";
              position = "0,0";
            }
          ];
        };}
        {profile = {
          name = "laptop_0";
          outputs = [{
            criteria = "LG Display 0x060A Unknown";
            position = "0,0";
          }];
        };}
        {profile = {
          name = "laptop_1";
          outputs = [
            {
              criteria = "CEX CX133 0x00000001";
              position = "0,0";
            }
            {
              criteria = "LG Display 0x060A Unknown";
              position = "2560,0";
            }
          ];
        };}
        {profile = {
          name = "laptop_2";
          outputs = [
            {
              criteria = "AOC 16G3 1DDP7HA000348";
              position = "0,0";
            }
            {
              criteria = "LG Display 0x060A Unknown";
              position = "1920,0";
            }
          ];
        };}
      ];
    };
  };
}
