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

let 
  cfg = config.jade.desktop;
  window-screenshot = pkgs.writeShellScriptBin "window-screenshot.sh" (builtins.readFile ../../other/scripts/desktop/window-screenshot.sh);
  # desktop-ctl = pkgs.writeShellScriptBin "desktop-ctl.sh" (builtins.readFile ../scripts/desktop/desktop-ctl.sh);
  desktop-ctl = import ../../other/scripts/desktop/desktopctl.nix { inherit pkgs; };
  # search and open in browser
  searchwolf = import ../../other/scripts/desktop/searchwolf.nix { inherit pkgs; };
  # permaclip
  pc-get = pkgs.writeShellScriptBin "pc-get.sh" (builtins.readFile ../../other/scripts/desktop/permaclip/pc-get.sh);
  pc-set = pkgs.writeShellScriptBin "pc-set.sh" (builtins.readFile ../../other/scripts/desktop/permaclip/pc-set.sh);
  em-record = pkgs.writeShellScriptBin "em-record.sh" (builtins.readFile ../../other/scripts/desktop/macros/record.sh);
  em-play = pkgs.writeShellScriptBin "em-play.sh" (builtins.readFile ../../other/scripts/desktop/macros/play.sh);
  em-play-loop = pkgs.writeShellScriptBin "em-play-loop.sh" (builtins.readFile ../../other/scripts/desktop/macros/play-loop.sh);
  ocr-screenshot = pkgs.writeShellScriptBin "ocr-screenshot.sh" (builtins.readFile ../../other/scripts/desktop/ocr-screenshot.sh);
  # rofi calculator copied from https://github.com/barbuk/menu-qalc
  menu-qalc = pkgs.writeShellScriptBin "menu-qalc" (builtins.readFile ../../other/scripts/desktop/menu-qalc.sh);
  #bitw = pkgs.buildGo118Module rec {
    #pname = "bitw";
    #version = "0.1.0";
    #src = pkgs.fetchFromGitHub {
      #owner = "mvdan";
      #repo = "bitw";
      #rev = "9da6ca0287ae8e4b1aa7fcdf7c4e8c2736718c3b";
      #sha256 = "sha256-qe9y6BEsfsEiPFXlM/SFliSd+2fnfDMHyw/O6EE5hpA=";
    #};
    #nativeBuildInputs = with pkgs; [ gitMinimal ];
    #doCheck = false;
    #vendorHash = "sha256-KQr0DtyH3xzlFwsDl3MGLRRLQC4+EtdTOG7IhmNCzV4=";
    #vendorHash = null;
    #GOFLAGS = "-mod=readonly";
    #proxyVendor = true;
    #deleteVendor = true;
    #preBuild = ''
      #go mod vendor
    #'';
  #};
in with lib; {
  imports = (mkIf cfg.enable [ 
    ./audio.nix
    ./gaming.nix
    ./creative.nix # TODO: more fine grained choices, not every setup needs fspy or rawtherapee
    ./compositing.nix
    ./ios-compat.nix
    ./syncthing.nix
    ./kdeconnect.nix
    ./themeing.nix
    ./cloud.nix
    ./networking.nix
    ./polybar.nix
    ./social.nix
    #./xkeysnail.nix
    ./mail.nix
    ./specific-hardware
    ./xmonad.nix
    ./i3
  ]).content;
  options.jade.desktop = {
    enable = mkEnableOption "Enable the i3 setup";
  };
  config = mkIf cfg.enable {
    fonts.fonts = with pkgs; [
      (nerdfonts.override { fonts = [ "FiraCode" ]; }) 
      montserrat 
      noto-fonts 
      atkinson-hyperlegible
    ];
    fonts.fontDir.enable = true;

    i18n.inputMethod = {
        enabled = "fcitx5";
        fcitx5.addons = with pkgs; [
            fcitx5-mozc
            fcitx5-gtk
        ];
    };

    hardware.bluetooth.enable = true;
    hardware.keyboard.uhk.enable = true;
    #hardware.spacenavd.enable = true;
    programs.seahorse.enable = true;
    security.pam.services.jade.enableGnomeKeyring = true;

    virtualisation.libvirtd.enable = true;

    # environment.systemPackages = with pkgs; [ gnome.gdm ];
    services = {
      printing.enable = true;
      gnome.gnome-keyring.enable = true;

      xserver = {
        layout = "us";
        xkbVariant = "altgr-intl";

        enable = true;

        wacom.enable = true;
        libinput = {
          enable = true;
        };

        desktopManager = {
          xterm.enable = false;
        };
       
        displayManager = {
          #defaultSession = "none+i3";
          gdm.enable = true;
        };

        windowManager.xmonad = {
          enable = true;
        };
      };


    };
    # sensible modules:
    # - [x] audio
    # - [x] compositing (picom configs)
    # - [x] gaming (steam, lutris, prism...)
    # - [x] cloud (pw manager, nextcloud, fluent reader)
    # - [x] syncthing
    # - [x] social (evolution, discord, matrix?)
    #   - [ ] set up mail accounts properly
    # - [x] creative (obs, blender, gimp n shit)
    # - [x] apple-stuff (ifuse etc)
    # - [x] themeing
    # - [x] vpn/networking stuff
    # - [ ] keymap
    #   - [ ] seperate keymap by modules like autostarts
    # - [x] kdeconnect
    # - [x] polybar
    # - [ ] bluetooth
    # - [ ] spacenav support (with custom systemd service)
    # - [ ] wacom config
    # - [x] xkeysnail
    # - [ ] oneko 
    # also useful:
    # - every custom script its own flake n stuff

    programs.xss-lock = {
      enable = true;
      lockerCommand = "${pkgs.i3lock}/bin/i3lock";
    };

    services.illum.enable = true;
    services.avahi.enable = true;

    services.smartd = {
      enable = true;
      notifications.x11.enable = true;
      notifications.test = true; 
    };
    services.colord.enable = true;

    home-manager.users.jade = { pkgs, ... }: {
      programs.bat = {
        # TODO: more config
        enable = true;
      };
      programs.exa = {
        # TODO: more config
        enable = true;
      };
      programs.nushell = {
        enable = true;
        package = config.users.defaultUserShell;
        configFile.source = ../../other/config.nu;
        extraConfig = ''
          source ${pkgs.nu_scripts}/share/nu_scripts/modules/nix/nix.nu;
        '';
      };
      programs.ripgrep = {
        enable = true;
      };
      services.dunst = {
        enable = true;
        settings = {
          global = {
            dmenu = "${pkgs.rofi}/bin/rofi -theme gruvbox-dark -dmenu -p dunst";
            browser = "${pkgs.librewolf}/bin/librewolf";
            mouse_left_click = "context";
            mouse_middle_click = "close_current";           
            background = "#282828";
            foreground = "#ebdbb2";
            frame_color = "#504945";
            frame_width = 2;
          };
        };
      };
      programs.rofi  = {
        enable = true;
        theme =  ../../other/rofi-themes/applauncher.rasi;      
      };
      home.packages = with pkgs; [
        #nushell
        nu_scripts direnv

        magic-wormhole-rs

        argyllcms
        displaycal

        # carapace completer
        carapace
        
        i3lock rofimoji feh xorg.xinput arandr flameshot
        tesseract5 imagemagick xclip xmacro libwacom wacomtablet xorg.xev

        # categories
        # filemanager
        xfce.thunar xfce.tumbler xfce.thunar-archive-plugin gnome.file-roller
        # media/file viewers 
        vlc evince nomacs

        # from environment.systemPackages cleanup
        font-manager xdotool xorg.xwininfo  gparted librewolf firefox uhk-agent
        cool-retro-term xdg-desktop-portal-gtk obsidian zettlr virt-manager
        ddccontrol-db firebird-emu gitg gpick qdirstat ffmpeg_5


        # external
        libnotify i3lock rofi rofimoji feh xorg.xinput arandr
        flameshot tesseract5 imagemagick xclip kitty xmacro
        brightnessctl drawing libqalculate ddgr jq

        # custom scripts
        window-screenshot desktop-ctl em-record em-play em-play-loop
        ocr-screenshot menu-qalc pc-get pc-set searchwolf

        # meow
        oneko
      ];
      xsession = {
        enable = true;
      };
    };
  };
}
