package org.ovirt.engine.ui.userportal.widget;

import com.google.gwt.resources.client.CssResource;
import org.ovirt.engine.ui.common.idhandler.HasElementId;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.IsEditor;
import com.google.gwt.editor.client.adapters.TakesValueEditor;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.TakesValue;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class DoublePercentageProgressBar extends Composite implements IsEditor<TakesValueEditor<Object>>, TakesValue<Object>, HasElementId {

    private static final String ZERO = "0%"; //$NON-NLS-1$
    private String title;
    private static final int FULL_WIDTH = 99;
    private static final int MINIMUM_SIZE_TO_SHOW_TEXT = 10;

    interface WidgetUiBinder extends UiBinder<Widget, DoublePercentageProgressBar> {
        WidgetUiBinder uiBinder = GWT.create(WidgetUiBinder.class);
    }

    public DoublePercentageProgressBar() {
        initWidget(WidgetUiBinder.uiBinder.createAndBindUi(this));
    }

    private Integer valueA;
    private Integer valueB;

    @UiField
    WidgetStyle style;

    @UiField
    Label percentageLabelA;

    @UiField
    Label percentageLabelB;

    @UiField
    FlowPanel percentageBarA;

    @UiField
    FlowPanel percentageBarB;

    @Override
    public void setValue(Object value) {
        assert value instanceof Integer : "Only integer values are accepted"; //$NON-NLS-1$
    }

    @Override
    public Object getValue() {
        return valueB;
    }

    public void setValueA(Integer value) {
        this.valueA = value;
    }

    public void setValueB(Integer value) {
        this.valueB = value;
    }

    public void setZeroValue() {
        percentageBarA.setVisible(true);
        percentageBarA.setTitle(title);
        percentageBarA.setStyleName(style.empty());
        percentageBarA.setWidth(FULL_WIDTH + "%"); //$NON-NLS-1$
        percentageLabelA.setText(ZERO);
        percentageLabelA.setStyleName(style.percentageLabelBlack());
    }

    public void setBars() {
        int fakeA = valueA;
        int fakeB = valueB;

        if (valueA != null && valueB != null && valueA + valueB >= FULL_WIDTH) {
            double factor = (double)(FULL_WIDTH-1) / (valueA + valueB);
            fakeA = (int)Math.round(factor * valueA);
            fakeB = (int)Math.round(factor * valueB);

            fakeA = (fakeB == 0 ? FULL_WIDTH : fakeA);
            fakeB = (fakeA == 0 ? FULL_WIDTH : fakeB);
        }
        setBar(percentageBarA, percentageLabelA, valueA, fakeA, style.percentageLabelBlack());
        setBar(percentageBarB, percentageLabelB, valueB, fakeB, style.percentageLabel());
    }

    private void setBar(FlowPanel percentageBar, Label percentageLabel, Integer value, int fakeValue, String style){
        if (value != null) {
            String percentage = value + "%"; //$NON-NLS-1$
            String fakePercentage = fakeValue + "%"; //$NON-NLS-1$
            percentageLabel.setText(value < MINIMUM_SIZE_TO_SHOW_TEXT ? "" : percentage); //$NON-NLS-1$
            percentageLabel.setStyleName(style);
            percentageBar.setWidth(fakePercentage);
            percentageBar.setVisible(value != 0);
            percentageBar.setTitle(percentage);
        }
    }

    public Object getValueA() {
        return valueA;
    }

    public Object getValueB() {
        return valueB;
    }

    @Override
    public TakesValueEditor<Object> asEditor() {
        return TakesValueEditor.of(this);
    }

    @Override
    public void setElementId(String elementId) {
        // Set percentage label element ID
        percentageLabelB.getElement().setId(elementId);
    }

    interface WidgetStyle extends CssResource {

        String percentageBarUnlimited();

        String percentageBarExceeded();

        String empty();

        String percentageBarA();

        String percentageLabelBlack();

        String percentageLabel();
    }

    @Override
    public void setTitle(String title) {
        this.title = title;
        super.setTitle(title);
    }
}

@@ -45,60 +45,8 @@
 t;%0A%0A
-import com.google.gwt.resources.client.CssResource;%0A
 impo
@@ -240,32 +240,84 @@
 kesValueEditor;%0A
+import com.google.gwt.resources.client.CssResource;%0A
 import com.googl
@@ -2280,35 +2280,57 @@
    i
-nt fak
+f (valu
 eA 
+!
 = 
-valueA;%0A
+null && valueB != null) %7B%0A  
         
 int 
@@ -2325,24 +2325,26 @@
         
+  
 int fake
 B = valu
@@ -2335,25 +2335,25 @@
 int fake
-B
+A
  = value
 B;%0A%0A    
@@ -2340,28 +2340,31 @@
 akeA = value
-B;%0A%0A
+A;%0A    
         if (
@@ -2364,47 +2364,43 @@
    i
-f (valueA != null && valueB != null && 
+nt fakeB = valueB;%0A            if (
 valu
@@ -2440,16 +2440,20 @@
         
+    
 double f
@@ -2468,16 +2468,17 @@
 (double)
+ 
 (FULL_WI
@@ -2480,17 +2480,19 @@
 LL_WIDTH
--
+ - 
 1) / (va
@@ -2511,32 +2511,36 @@
 B);%0A            
+    
 fakeA = (int)Mat
@@ -2528,32 +2528,33 @@
    fakeA = (int)
+ 
 Math.round(facto
@@ -2570,32 +2570,36 @@
 A);%0A            
+    
 fakeB = (int)Mat
@@ -2595,16 +2595,17 @@
  = (int)
+ 
 Math.rou
@@ -2622,24 +2622,28 @@
 * valueB);%0A%0A
+    
             
@@ -2689,32 +2689,36 @@
 A);%0A            
+    
 fakeB = (fakeA =
@@ -2756,18 +2756,25 @@
     
-%7D%0A
+    %7D%0A%0A  
         
 setB
@@ -2761,32 +2761,34 @@
    %7D%0A%0A          
+  
 setBar(percentag
@@ -2858,32 +2858,36 @@
 ack());%0A        
+    
 setBar(percentag
@@ -2948,24 +2948,34 @@
 geLabel());%0A
+        %7D%0A
     %7D%0A%0A    p

fix possible NPE in % progress bar