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

with lib;
let
  mkFontOption = kind: default: {
    family = mkOption {
      type = types.str;
      default = default.family;
      description = "Family name for ${kind} font profile";
      example = "Fira Code";
    };
    package = mkOption {
      type = types.package;
      default = default.package;
      description = "Package for ${kind} font profile";
      example = "pkgs.fira-code";
    };
    size = mkOption {
      type = types.number;
      default = default.size;
      description = "${kind} font profile size, px";
      example = "11";
    };
  };
  cfg = config.modules.desktop.fonts;
in {
  options.modules.desktop.fonts = {
    enable = mkEnableOption "Enable the font configuration module, configuring your chosen fonts";
    baseFonts = mkEnableOption "Add an extra set of base fonts";
    
    fonts = {
      sansSerif = mkFontOption "sansSerif" {
        package = pkgs.my.atkinson-hyperlegible-pro;
        family = "Atkinson Hyperlegible Pro";

        size = 11;
      };
      serif = mkFontOption "serif" {
        package = pkgs.my.atkinson-hyperlegible-pro;
        family = "Atkinson Hyperlegible Pro";

        size = 11;
      };
      monospace = mkFontOption "monospace" {
        package = pkgs.cozette;
        family = "CozetteVector";

        size = 13;
      };
      monospaceBitmap = mkFontOption "bitmap monospace" {
        package = pkgs.cozette;
        family = "Cozette";

        size = 13;
      };
      emoji = mkFontOption "emoji" {
        package = pkgs.twitter-color-emoji;
        family = "Twitter Color Emoji";

        size = 13; # not applicable, but whatever
      };
    };
  };

  config = mkIf cfg.enable (mkMerge [
    {
      fonts = {
        fontDir.enable = true;
        fontconfig.enable = true;
        fontconfig.defaultFonts = {
          sansSerif = [ cfg.fonts.sansSerif.family ];
          serif = [ cfg.fonts.serif.family ];
          monospace = [ cfg.fonts.monospace.family ];
          emoji = [ cfg.fonts.emoji.family ];
        };
        enableGhostscriptFonts = true;
        packages = with pkgs; [  	
          corefonts
          noto-fonts
          noto-fonts-cjk-sans
          liberation_ttf
        ] ++ [
          cfg.fonts.sansSerif.package
          cfg.fonts.serif.package
          cfg.fonts.monospace.package
          cfg.fonts.monospaceBitmap.package
          cfg.fonts.emoji.package
        ];
      };

      hm.gtk.enable = true;
      hm.gtk.font = with cfg.fonts.sansSerif; { 
        package = package;
        name = family;

        size = size;
      };

      hm.dconf.settings = {
        "org/gnome/desktop/interface".font-name = with cfg.fonts.sansSerif; "${family} ${toString size}";
        "org/gnome/desktop/interface".document-font-name = with cfg.fonts.serif; "${family} ${toString size}";
        "org/gnome/desktop/interface".monospace-font-name = with cfg.fonts.monospace; "${family} ${toString size}";
      };
    }
    (mkIf cfg.baseFonts {
      fonts.enableDefaultPackages = true;
      fonts.packages = with pkgs; [
        fira-code
        fira-code-symbols
        mplus-outline-fonts.githubRelease
        dina-font
        proggyfonts
        atkinson-hyperlegible
        cozette
        twemoji-color-font
        noto-fonts-color-emoji
        noto-fonts-monochrome-emoji
        font-awesome
      ];
    })
  ]);
}
