{ pkgs, ... }:

let
  extensionUrl = "https://git.mizuki.guru/imnyang/thisiswindowsfigma/releases/download/v1.0.0/d168a70cb6a24fe4a478-1.0.0.xpi";
  extensionId = "figma-windows-ua-spoofer@imnyang.local";
in
{
  programs.firefox = {
    enable = true;
    package = pkgs.firefox-devedition;

    policies = {
      DisableAppUpdate = true;
      ExtensionSettings = {
        ${extensionId} = {
          installation_mode = "force_installed";
          install_url = extensionUrl;
        };
      };
    };

    profiles.imnyang = {
      id = 0;
      name = "imnyang";
      isDefault = true;

      settings = {
        "toolkit.legacyUserProfileCustomizations.stylesheets" = true;
        "browser.uidensity" = 0;
        "browser.theme.dark-private-windows" = false;
      };

      userChrome = ''
        :root {
          --mizuki-bg: #fcf8f9;
          --mizuki-fg: #191017;
          --mizuki-muted: #707d8a;
          --mizuki-border: #f2e6ea;
          --mizuki-surface: #fdf7f9;
          --mizuki-hover: #8165700f;
          --mizuki-active: #8165701a;
          --mizuki-selection: #ecb7d5;
        }

        @media (prefers-color-scheme: dark) {
          :root {
            --mizuki-bg: #191017;
            --mizuki-fg: #fafafa;
            --mizuki-muted: #a9a9b1;
            --mizuki-border: #2c1f2d;
            --mizuki-surface: #191017;
            --mizuki-hover: #2c1f2d80;
            --mizuki-active: #2c1f2d;
            --mizuki-selection: #604562;
          }
        }

        #navigator-toolbox,
        #TabsToolbar,
        #nav-bar,
        #PersonalToolbar,
        #sidebar-box,
        #sidebar-header {
          background-color: var(--mizuki-bg) !important;
          color: var(--mizuki-fg) !important;
        }

        #navigator-toolbox {
          border-bottom: 1px solid var(--mizuki-border) !important;
        }

        #urlbar-background,
        #searchbar {
          background-color: var(--mizuki-surface) !important;
          border-color: var(--mizuki-border) !important;
          color: var(--mizuki-fg) !important;
        }

        #urlbar[focused] > #urlbar-background,
        #searchbar:focus-within {
          border-color: var(--mizuki-selection) !important;
          box-shadow: 0 0 0 1px var(--mizuki-selection) !important;
        }

        .tab-background {
          border-radius: 10px 10px 0 0 !important;
        }

        .tab-background[selected] {
          background-color: var(--mizuki-active) !important;
        }

        .tabbrowser-tab:not([selected]):hover .tab-background,
        toolbarbutton:hover,
        #urlbar:hover > #urlbar-background {
          background-color: var(--mizuki-hover) !important;
        }

        toolbarbutton[open],
        toolbarbutton[checked],
        toolbarbutton:active {
          background-color: var(--mizuki-active) !important;
        }

        #TabsToolbar .tabbrowser-tab,
        #urlbar-input,
        #identity-icon-label,
        toolbarbutton {
          color: var(--mizuki-fg) !important;
        }

        .tabbrowser-tab:not([selected]) .tab-label,
        #sidebar-title {
          color: var(--mizuki-muted) !important;
        }
      '';

      userContent = ''
        @-moz-document url("about:home"), url("about:newtab"), url("about:privatebrowsing") {
          body,
          .outer-wrapper,
          .activity-stream {
            background: #fcf8f9 !important;
            color: #191017 !important;
          }

          @media (prefers-color-scheme: dark) {
            body,
            .outer-wrapper,
            .activity-stream {
              background: #191017 !important;
              color: #fafafa !important;
            }
          }
        }
      '';
    };
  };

  xdg.mimeApps.defaultApplications = {
    "text/html" = [ "firefox-devedition.desktop" ];
    "x-scheme-handler/http" = [ "firefox-devedition.desktop" ];
    "x-scheme-handler/https" = [ "firefox-devedition.desktop" ];
    "x-scheme-handler/about" = [ "firefox-devedition.desktop" ];
    "x-scheme-handler/unknown" = [ "firefox-devedition.desktop" ];
  };
}
