/*
 * MIT License
 *
 * Copyright (c) 2020 Jannis Weis
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package com.github.weisj.darklaf.graphics;

import java.awt.*;
import java.util.Optional;

import javax.swing.*;

import com.github.weisj.darklaf.util.PropertyUtil;
import com.github.weisj.darklaf.util.PropertyValue;
import com.github.weisj.darklaf.util.SystemInfo;

/**
 * @author Konstantin Bulenkov
 * @author Jannis Weis
 */
public final class GraphicsUtil {

    public static final String DESKTOP_HINTS_KEY = "awt.font.desktophints";
    public static final String KEY_OPAQUE_BUFFERED = "JComponent.opaqueBuffered";

    private GraphicsUtil() {}

    public static void setOpaqueBuffered(final JComponent c, final boolean opaqueBuffered) {
        if (c != null) c.putClientProperty(KEY_OPAQUE_BUFFERED, opaqueBuffered);
    }

    public static boolean isOpaqueBuffered(final JComponent c) {
        if (!SystemInfo.isWindows) return false;
        return PropertyUtil.getBooleanProperty(c, KEY_OPAQUE_BUFFERED);
    }

    public static GraphicsContext setupAntialiasing(final Graphics g2) {
        return setupAntialiasing(g2, true, false);
    }

    public static GraphicsContext setupAntialiasing(final Graphics g2, final boolean enableAA,
            final boolean ignoreSystemSettings) {
        GraphicsContext config = new GraphicsContext(g2);
        if (ignoreSystemSettings && g2 instanceof Graphics2D) {
            Graphics2D g = (Graphics2D) g2;
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                    enableAA ? RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HBGR : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        }
        return config;
    }

    public static GraphicsContext setupAAPainting(final Graphics g) {
        GraphicsContext config = new GraphicsContext(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
        return config;
    }

    public static GraphicsContext paintWithAlpha(final Graphics g, final float alpha) {
        assert 0.0F <= alpha && alpha <= 1.0F : "alpha should be in range 0.0f .. 1.0f";

        GraphicsContext config = new GraphicsContext(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        return config;
    }

    public static GraphicsContext setupStrokePainting(final Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        GraphicsContext context = new GraphicsContext(g2);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
                useQuartz() ? RenderingHints.VALUE_STROKE_PURE : RenderingHints.VALUE_STROKE_NORMALIZE);
        return context;
    }

    public static boolean isHighDpiEnabled() {
        return PropertyValue.TRUE.equalsIgnoreCase(System.getProperty("hidpi"));
    }

    public static boolean useQuartz() {
        return SystemInfo.isMac && PropertyValue.TRUE.equals(System.getProperty("apple.awt.graphics.UseQuartz"));
    }

    public static boolean supportsTransparency(final Window window) {
        Optional<GraphicsDevice> gd =
                Optional.ofNullable(window).map(Window::getGraphicsConfiguration).map(GraphicsConfiguration::getDevice);
        return gd.map(d -> d.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT))
                .orElse(false);
    }

    public static boolean supportsTransparency() {
        return !GraphicsEnvironment.isHeadless() && GraphicsEnvironment
                .getLocalGraphicsEnvironment()
                .getDefaultScreenDevice()
                .isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT);
    }
}

@@ -1187,35 +1187,8 @@
 t.*;
-%0Aimport java.util.Optional;
 %0A%0Aim
@@ -4327,97 +4327,86 @@
     
-Optional%3CGraphicsDevice%3E gd =%0A                Optional.ofNullable(window).map(Window::get
+if (window == null %7C%7C GraphicsEnvironment.isHeadless()) return false;%0A        
 Grap
@@ -4426,14 +4426,24 @@
 tion
-).map(
+ gc = window.get
 Grap
@@ -4463,167 +4463,100 @@
 tion
-::getDevice);%0A        return gd.map(d -%3E d.isWindowTranslucencySupported(GraphicsDevice.Window
+();%0A        if (gc == null) return false;%0A        return supports
 Trans
-luc
+par
 ency
-.PERPIXEL_TRANSLUCENT))%0A                .orElse(false
+(gc.getDevice()
 );%0A 
@@ -4624,16 +4624,12 @@
     
-return !
+if (
 Grap
@@ -4656,20 +4656,68 @@
 adless()
- && 
+) return false;%0A        return supportsTransparency(
 Graphics
@@ -4727,33 +4727,16 @@
 ironment
-%0A                
 .getLoca
@@ -4757,33 +4757,16 @@
 onment()
-%0A                
 .getDefa
@@ -4786,99 +4786,312 @@
 ce()
+);
 %0A    
+%7D%0A%0A
     
-        .isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_
+public static boolean supportsTransparency(final GraphicsDevice gd) %7B%0A        return gd != null%0A                && gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT)%0A                && gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.
 TRAN

Check for headless environment if determining transparency capabilities.