{pkgs, config, lib, ...}:
let
  cfg = config.nix-config.style;
in
{
    options.nix-config.style = {
        cursor = {
            size = lib.mkOption {
                type = lib.types.int;
                default = 22;
                description = "Size of the cursor in pixels.";
            };
        };
        fontSizes = {
            applications = lib.mkOption {
                type = lib.types.int;
                default = 12;
                description = "Font size for applications.";
            };
            desktop = lib.mkOption {
                type = lib.types.int;
                default = 12;
                description = "Font size for desktop icons.";
            };
            popups = lib.mkOption {
                type = lib.types.int;
                default = 12;
                description = "Font size for popups.";
            };
            terminal = lib.mkOption {
                type = lib.types.int;
                default = 12;
                description = "Font size for terminal.";
            };
        };

        terminalOpacity = lib.mkOption {
            type = lib.types.float;
            default = 0.8;
            description = "Terminal default opacity";
        };

        monospaceFont = lib.mkOption {
            type = lib.types.str;
            default = "FiraCode Nerd Font Mono";
            description = "Monospace font. Needs to be installed.";
        };
    };


    config = {

        stylix = {
            # Stylix is already enabled since the system is configured to use it
            enable = true;
            image = ./../../wallpapers/magicpattern-87PP9Zd7MNo-unsplash.jpg;
            # don't theme applications by default
            autoEnable = false;

            cursor = {
                package = pkgs.bibata-cursors;
                name = "Bibata-Modern-Ice";
                size = cfg.cursor.size;
            };

            iconTheme = {
                enable = true;
                package = pkgs.tela-icon-theme;
            };



            polarity = lib.mkDefault "dark";

            fonts = {

                serif = {
                    package = pkgs.dejavu_fonts;
                    name = "DejaVu Serif";
                };

                sansSerif = {
                    package = pkgs.dejavu_fonts;
                    name = "DejaVu Sans";
                };

                monospace = {
                    package = pkgs.nerd-fonts.fira-code;
                    name = cfg.monospaceFont;
                };

                emoji = {
                    package = pkgs.noto-fonts-color-emoji;
                    name = "Noto Color Emoji";
                };

                sizes = {
                    applications = cfg.fontSizes.applications;
                    desktop = cfg.fontSizes.desktop;
                    popups = cfg.fontSizes.popups;
                    terminal = cfg.fontSizes.terminal;
                };
            };

            opacity = {
                applications = 1.0;
                desktop = 1.0;
                popups = 0.9;
                terminal = cfg.terminalOpacity;
            };

            base16Scheme = {
                scheme = "onelight";
                author = "https://github.com/one-dark";
                base00 = "#fafafa";
                base01 = "#f4f4f4";
                base02 = "#e5e5e6";
                base03 = "#dfdfe0";
                base04 = "#d7d7d8";
                base05 = "#383a42";
                base06 = "#202227";
                base07 = "#090a0b";
                base08 = "#d84a3d";
                base09 = "#a626a4";
                base0A = "#c18401";
                base0B = "#50a14f";
                base0C = "#0070a8";
                base0D = "#4078f2";
                base0E = "#a626a4";
                base0F = "#986801";
            };

            targets = {
                hyprpaper.enable = true;
                hyprlock.enable = true;
                # disabled because they overwrite too many settings
                # gnome
                # wofi
                # vscode
                # waybar
                # kitty

                fontconfig.enable = true;
                font-packages.enable = true;

            };
        };


        # https://pltanton.dev/posts/2024/02/nix-based-dark-light-theme-switch/
        home.packages = [
            (lib.lowPrio (pkgs.writeShellApplication {
                name = "toggle-theme";
                runtimeInputs = with pkgs; [ home-manager coreutils ripgrep ];
                text =
                ''
                    "$(home-manager generations | head -1 | rg -o '/[^ ]*')"/specialisation/light-theme/activate
                    echo "Switched to light theme"
                '';
            }))
        ];


        specialisation.light-theme.configuration = {
            stylix = {
                polarity = "light";
            };

            home.packages = [
                (lib.hiPrio (pkgs.writeShellApplication {
                name = "toggle-theme";
                runtimeInputs = with pkgs; [ home-manager coreutils ripgrep ];
                text =
                    ''
                    "$(home-manager generations | head -2 | tail -1 | rg -o '/[^ ]*')"/activate
                    echo "Switched to dark theme"
                    '';
                }))
            ];
        };
    };
}
