package com.mercury.platform.ui.adr.components.panel.ui;

import com.mercury.platform.shared.config.Configuration;
import com.mercury.platform.shared.config.configration.IconBundleConfigurationService;
import com.mercury.platform.shared.config.descriptor.adr.AdrComponentOrientation;
import com.mercury.platform.shared.config.descriptor.adr.AdrDurationComponentDescriptor;
import com.mercury.platform.shared.config.descriptor.adr.AdrIconDescriptor;
import com.mercury.platform.ui.misc.AppThemeColor;
import lombok.Getter;
import lombok.Setter;
import sun.swing.SwingUtilities2;

import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicProgressBarUI;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.text.DecimalFormat;


public abstract class BasicMercuryIconTrackerUI<T extends AdrDurationComponentDescriptor> extends ComponentUI{
    @Setter
    protected T descriptor;
    @Setter
    protected MercuryTracker tracker;
    protected IconBundleConfigurationService config;

    protected BasicMercuryIconTrackerUI(){
        this.config = Configuration.get().iconBundleConfiguration();
    }
    @Override
    public Dimension getPreferredSize(JComponent c) {
        Dimension dimension = super.getPreferredSize(c);
        if(dimension == null){
            dimension = new Dimension(150, 26);
        }
        int value = Math.max(dimension.width, dimension.height);
        dimension.setSize(value, value);
        return dimension;
    }

    protected void paintString(Graphics g, int x, int y, int width, int height, int amountFull) {

        if(descriptor.isTextEnable() && tracker.isStringPainted()) {
            float value = tracker.getValue() / 1000f;

            Graphics2D g2 = this.prepareAdapter(g);
            DecimalFormat decimalFormat = new DecimalFormat(descriptor.getTextFormat());
            String progressString = String.valueOf(decimalFormat.format(value));
            g2.setFont(tracker.getFont());
            Point renderLocation = getStringPlacement(g2, progressString,
                    x, y, width, height);
            if (value >= descriptor.getDefaultValueTextThreshold()) {
                g2.setColor(this.descriptor.getDefaultValueTextColor());
            } else if (value >= this.descriptor.getMediumValueTextThreshold()) {
                g2.setColor(this.descriptor.getMediumValueTextColor());
            } else {
                g2.setColor(this.descriptor.getLowValueTextColor());
            }
            SwingUtilities2.drawString(tracker, g2, progressString,
                    renderLocation.x, renderLocation.y);
            if(this.descriptor.getOutlineThickness() > 0) {
                FontRenderContext frc = g2.getFontRenderContext();
                TextLayout textTl = new TextLayout(progressString, tracker.getFont(), frc);
                Shape outline = textTl.getOutline(null);
                g2.translate(renderLocation.x, renderLocation.y);
                g2.setStroke(new BasicStroke(this.descriptor.getOutlineThickness()));
                g2.setColor(this.descriptor.getOutlineColor());
                g2.draw(outline);
            }
        }
    }
    protected void paintBorder(Graphics g){
        int thickness = descriptor.getThickness();
        if(thickness > 0) {
            Graphics2D g2 = (Graphics2D) g;
            Stroke oldStroke = g2.getStroke();
            if(!descriptor.isBindToTextColor()) {
                g2.setPaint(this.descriptor.getBorderColor());
            }
            g2.setStroke(new BasicStroke(thickness));
            g2.drawRect(0, 0, tracker.getWidth() - thickness, tracker.getHeight() - thickness);
            g2.setStroke(oldStroke);
        }
    }

    protected Point getStringPlacement(Graphics g, String progressString,
                                       int x,int y,int width,int height) {
        FontMetrics fontSizer = SwingUtilities2.getFontMetrics(tracker, g,
                tracker.getFont());
        int stringWidth = SwingUtilities2.stringWidth(tracker, fontSizer,
                progressString);

        if (descriptor.getOrientation() == AdrComponentOrientation.HORIZONTAL) {
            return new Point(x + Math.round(width/2 - stringWidth/2),
                    y + ((height +
                            fontSizer.getAscent() -
                            fontSizer.getLeading() -
                            fontSizer.getDescent()) / 2));
        } else {
            return new Point(x + ((width  -
                    fontSizer.getAscent() -
                    fontSizer.getLeading() -
                    fontSizer.getDescent()) / 2),
                    y + ((height +
                            fontSizer.getAscent() -
                            fontSizer.getLeading() -
                            fontSizer.getDescent()) / 2));
        }
    }
    protected Graphics2D prepareAdapter(Graphics g){
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        return g2;
    }
}

@@ -2191,395 +2191,44 @@
     
-if (value %3E= descriptor.getDefaultValueTextThreshold()) %7B%0A                g2.setColor(this.descriptor.getDefaultValueTextColor());%0A            %7D else if (value %3E= this.descriptor.getMediumValueTextThreshold()) %7B%0A                g2.setColor(this.descriptor.getMediumValueTextColor());%0A            %7D else %7B%0A                g2.setColor(this.descriptor.getLowValueTextColor());%0A            %7D
+g2.setColor(this.getColorByValue());
 %0A   
@@ -2683,32 +2683,83 @@
 derLocation.y);%0A
+                Stroke oldStroke = g2.getStroke();%0A
                 
@@ -2824,24 +2824,72 @@
 ckness()));%0A
+                Color oldColor = g2.getColor();%0A
             
@@ -2970,24 +2970,104 @@
 w(outline);%0A
+                g2.setColor(oldColor);%0A                g2.setStroke(oldStroke);%0A
             
@@ -3235,30 +3235,82 @@
 2D g2 = 
-(Graphics2D) g
+this.prepareAdapter(g);%0A            g2.setColor(getColorByValue())
 ;%0A      
@@ -3627,16 +3627,16 @@
 kness);%0A
-
         
@@ -3675,24 +3675,471 @@
      %7D%0A    %7D
+%0A    private Color getColorByValue()%7B%0A        float value = tracker.getValue() / 1000f;%0A        if (value %3E= descriptor.getDefaultValueTextThreshold()) %7B%0A            return this.descriptor.getDefaultValueTextColor();%0A        %7D else if (value %3E= this.descriptor.getMediumValueTextThreshold()) %7B%0A            return this.descriptor.getMediumValueTextColor();%0A        %7D else %7B%0A            return this.descriptor.getLowValueTextColor();%0A        %7D%0A    %7D
 %0A%0A    protec

fix border thickness