package org.gwtbootstrap3.extras.gallery.client.ui;

/*
 * #%L
 * GwtBootstrap3
 * %%
 * Copyright (C) 2013 - 2014 GwtBootstrap3
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import com.google.gwt.dom.client.AnchorElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import org.gwtbootstrap3.client.ui.base.ComplexWidget;
import org.gwtbootstrap3.client.ui.base.HasHref;

/**
 * Gallery Image
 *
 * @author Ben Dol
 */
public class GalleryImage extends ComplexWidget implements HasHref {

    private Image image;

    @UiConstructor
    public GalleryImage(String url) {
        setElement(Document.get().createAnchorElement());
        getElement().setAttribute("data-gallery", "data-gallery");
        getElement().getStyle().setDisplay(Display.INLINE_TABLE);

        setHref(url);

        image = new Image(url);
        add(image);
    }

    @Override
    public void add(Widget child) {
        if(child instanceof Image) {
            if(image != null) {
                image.removeFromParent();
            }

            image = (Image) child;
            setHref(image.getUrl());

            super.add(image);
        } else if(child instanceof HasClickHandlers) {
            ((HasClickHandlers) child).addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    event.stopPropagation();
                }
            });

            Style style = child.getElement().getStyle();
            style.setPosition(Position.RELATIVE);
            style.setBottom((double)image.getHeight(), Unit.PX);
            style.setLeft(4, Unit.PX);

            super.add(child);
        } else {
            super.add(child);
        }
    }

    @Override
    public void setHref(String href) {
        AnchorElement.as(getElement()).setHref(href);
    }

    @Override
    public String getHref() {
        return AnchorElement.as(getElement()).getHref();
    }

    @Override
    public void setWidth(String width) {
        super.setWidth(width);
        image.setWidth(width);
    }

    @Override
    public void setHeight(String height) {
        super.setHeight(height);
        image.setHeight(height);

        for(Widget child : this) {
            if(child instanceof HasClickHandlers) {
                Style style = child.getElement().getStyle();
                style.setBottom((double)image.getHeight(), Unit.PX);
            }
        }
    }
}

@@ -698,16 +698,61 @@
 L%25%0A */%0A%0A
+import com.google.gwt.core.client.Scheduler;%0A
 import c
@@ -787,24 +787,24 @@
 horElement;%0A
-
 import com.g
@@ -1230,16 +1230,59 @@
 ructor;%0A
+import com.google.gwt.user.client.Command;%0A
 import c
@@ -1975,16 +1975,22 @@
 oid add(
+final 
 Widget c
@@ -2515,24 +2515,159 @@
        %7D);%0A%0A
+            Scheduler.get().scheduleDeferred(new Command() %7B%0A                @Override%0A                public void execute() %7B%0A        
             
@@ -2715,32 +2715,40 @@
 ();%0A            
+        
 style.setPositio
@@ -2773,32 +2773,40 @@
 E);%0A            
+   
+     
 style.setBottom(
@@ -2805,32 +2805,33 @@
 tBottom((double)
+ 
 image.getHeight(
@@ -2847,32 +2847,40 @@
 X);%0A            
+      
+  
 style.setLeft(4,
@@ -2889,16 +2889,50 @@
 nit.PX);
+%0A                %7D%0A            %7D);
 %0A%0A      
@@ -3501,35 +3501,179 @@
     
-for(Widget child : this) %7B%0A
+Scheduler.get().scheduleDeferred(new Command() %7B%0A            @Override%0A            public void execute() %7B%0A                for(Widget child : GalleryImage.this) %7B%0A        
     
@@ -3708,36 +3708,73 @@
 HasClickHandlers
-) %7B%0A
+ && !(child instanceof Image)) %7B%0A        
                 
@@ -3826,32 +3826,40 @@
                 
+      
+  
 style.setBottom(
@@ -3887,32 +3887,72 @@
 ht(), Unit.PX);%0A
+                    %7D%0A                %7D%0A
             %7D%0A  
@@ -3941,32 +3941,32 @@
 %7D%0A            %7D%0A
-
         %7D%0A    %7D%0A
@@ -3954,21 +3954,23 @@
  %7D%0A        %7D
+);
 %0A    %7D%0A%7D%0A

Fix a potential race condition.