{ pkgs, config }:

let
  rendersvg = pkgs.runCommand "rendersvg" { } ''
    mkdir -p $out/bin
    ln -s ${pkgs.resvg}/bin/resvg $out/bin/rendersvg
  '';
  scheme = config.colorScheme;
in pkgs.stdenv.mkDerivation rec {
  name = "generated-gtk-theme-${scheme.slug}";
  src = pkgs.fetchFromGitHub {
    owner = "vinceliuice";
    repo = "Orchis-theme";
    rev = "5b73376721cf307101e22d7031c1f4b1344d1f63";
    sha256 = "sha256-+2/CsgJ+rdDpCp+r5B/zys3PtFgtnu+ohTEUOtJNd1Y=";
  };

  nativeBuildInputs = with pkgs; [ gtk3 sassc ];
  buildInputs = with pkgs; [ gnome-themes-extra ];
  propagatedUserEnvPkgs = with pkgs; [ gtk-engine-murrine ];

  preInstall = ''
    mkdir -p $out/share/themes
    cat > src/_sass/_color-palette-${scheme.slug}.scss << 'EOF'
      $red-light: #${scheme.palette.low0F};
      $red-dark: #${scheme.palette.high0F};

      $pink-light: #${scheme.palette.low0E};
      $pink-dark: #${scheme.palette.high0E};

      $purple-light: #${scheme.palette.low0D};
      $purple-dark: #${scheme.palette.high0D};

      $blue-light: #${scheme.palette.low0C};
      $blue-dark: #${scheme.palette.high0C};

      $teal-light: #${scheme.palette.low0B};
      $teal-dark: #${scheme.palette.high0B};

      $green-light: #${scheme.palette.low0A};
      $green-dark: #${scheme.palette.high0A};
      $sea-light: #${scheme.palette.alt_low0B};
      $sea-dark: #${scheme.palette.alt_high0B};

      $yellow-light: #${scheme.palette.low09};
      $yellow-dark: #${scheme.palette.low09};

      $orange-light: #${scheme.palette.low08};
      $orange-dark: #${scheme.palette.high08};

      $grey-050: #${scheme.palette.base07};
      $grey-100: #${scheme.palette.base07};
      $grey-150: #${scheme.palette.base06};
      $grey-200: #${scheme.palette.base06};
      $grey-250: #${scheme.palette.base05};
      $grey-300: #${scheme.palette.base05};
      $grey-350: #${scheme.palette.base04};
      $grey-400: #${scheme.palette.base04};
      $grey-450: #${scheme.palette.base03};
      $grey-500: #${scheme.palette.base03};
      $grey-550: #${scheme.palette.base02};
      $grey-600: #${scheme.palette.base02};
      $grey-650: #${scheme.palette.base02};
      $grey-700: #${scheme.palette.base01};
      $grey-750: #${scheme.palette.base01};
      $grey-800: #${scheme.palette.base01};
      $grey-850: #${scheme.palette.base00};
      $grey-900: #${scheme.palette.base00};
      $grey-950: #${scheme.palette.base00};

      $white: #${scheme.palette.base07};
      $black: #${scheme.palette.base00};

      $button-close: #${scheme.palette.base0F};
      $button-max: #${scheme.palette.base0A};
      $button-min: #${scheme.palette.base08};
    EOF

    sed -i "/\@import/s/color-palette-default/color-palette-${scheme.slug}/" src/_sass/_tweaks.scss
    sed -i "/\$colorscheme:/s/default/${scheme.slug}/" src/_sass/_tweaks.scss

  '';

  installPhase = ''
    runHook preInstall
    bash install.sh -d $out/share/themes \
      -t default \
      -n ${scheme.slug} \
      -c ${scheme.variant} \
      -s standard \
      --tweaks primary \
      --round ${scheme.palette.border-radius}px

    runHook postInstall
  '';

}
