/*
 * Copyright 2015 JBoss Inc
 *
 * 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.
 */
package org.dashbuilder.displayer.client.widgets;

import java.util.ArrayList;
import java.util.List;

import com.github.gwtbootstrap.client.ui.Image;
import com.github.gwtbootstrap.client.ui.constants.ImageType;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.web.bindery.event.shared.HandlerRegistration;
import org.dashbuilder.displayer.DisplayerSubType;
import org.dashbuilder.displayer.DisplayerType;
import org.dashbuilder.displayer.client.RendererLibLocator;
import org.dashbuilder.displayer.client.RendererLibrary;
import org.dashbuilder.displayer.client.resources.i18n.DisplayerTypeLiterals;
import org.dashbuilder.displayer.client.resources.images.DisplayerImagesResources;

public class DisplayerSubtypeSelector extends Composite {

    public interface SubTypeChangeListener {
        void displayerSubtypeChanged(DisplayerSubType displayerSubType);
    }

    private SubTypeChangeListener listener;
    private List<DisplayerSubTypeImageWidget> imageWidgetList;
    RendererLibLocator rendererLibLocator;

    private FlexTable subtypes;
    private VerticalPanel subtypePanel;

    public DisplayerSubtypeSelector(SubTypeChangeListener subTypeChangeListener) {
        listener = subTypeChangeListener;
        rendererLibLocator = RendererLibLocator.get();
        imageWidgetList = new ArrayList<DisplayerSubTypeImageWidget>(5);

        subtypes = new FlexTable();
        subtypePanel = new VerticalPanel();
        subtypePanel.add(subtypes);
        initWidget(subtypePanel);
    }

    public void select(String renderer, DisplayerType type, DisplayerSubType selectedSubType) {
        subtypes.removeAllRows();
        imageWidgetList.clear();

        if (renderer == null || renderer.length() == 0) renderer = rendererLibLocator.getDefaultRenderer(type);
        RendererLibrary rendererLibrary = rendererLibLocator.lookupRenderer(renderer);
        if (rendererLibrary != null) {
            DisplayerSubType[] supportedSubTypes = rendererLibrary.getSupportedSubtypes(type);
            if (supportedSubTypes != null && supportedSubTypes.length > 0) {
                for (int i = 0; i < supportedSubTypes.length; i++) {
                    final DisplayerSubType subtype = supportedSubTypes[ i ];

                    // Double check the renderer library for invalid subtypes for this type
                    if (!type.getSubTypes().contains(subtype)) throw new RuntimeException("Wrong subtype (" + subtype + ") indicated for type " + type + " by renderer library " + rendererLibrary.getUUID());

                    ImageResource selectedIR = (ImageResource)DisplayerImagesResources.INSTANCE.getResource(type.toString() + "_" + subtype.toString() + DisplayerImagesResources.SELECTED_SUFFIX);
                    ImageResource unselectedIR = (ImageResource)DisplayerImagesResources.INSTANCE.getResource(type.toString() + "_" + subtype.toString() + DisplayerImagesResources.UNSELECTED_SUFFIX);
                    String tooltip = DisplayerTypeLiterals.INSTANCE.getString(type.toString() + "_" + subtype.toString() + "_tt");

                    boolean initiallySelected = selectedSubType != null? subtype == selectedSubType : i == 0;
                    final DisplayerSubTypeImageWidget dstiw = new DisplayerSubTypeImageWidget(  selectedIR,
                                                                                                unselectedIR,
                                                                                                tooltip,
                                                                                                initiallySelected);
                    imageWidgetList.add(dstiw);

                    if (initiallySelected) listener.displayerSubtypeChanged(subtype);

                    dstiw.setSelectClickHandler(
                            new ClickHandler() {
                                @Override
                                public void onClick(ClickEvent event) {
                                    if (!dstiw.isSelected) select(dstiw);
                                    if (listener != null) listener.displayerSubtypeChanged(subtype);
                                }
                            });

                    subtypes.setWidget(i, 0, dstiw);
                }
            } else {
                // Show a default image for those chart types that don't have any subtypes
                ImageResource selectedIR = (ImageResource)DisplayerImagesResources.INSTANCE.getResource(type.toString() + DisplayerImagesResources.DEFAULT_SUFFIX );
                String tooltip = DisplayerTypeLiterals.INSTANCE.getString(type.toString() + DisplayerImagesResources.DEFAULT_SUFFIX + "_tt");

                DisplayerSubTypeImageWidget dstiw = new DisplayerSubTypeImageWidget(  selectedIR,
                        null,
                        tooltip,
                        true);

                subtypes.setWidget(0, 0, dstiw);
            }
        }
    }

    private void select(DisplayerSubTypeImageWidget dstiw) {
        for (DisplayerSubTypeImageWidget imageWidget : imageWidgetList) {
            if (imageWidget == dstiw) imageWidget.select();
            else imageWidget.unselect();
        }
    }

    public class DisplayerSubTypeImageWidget extends Composite {

        private FlexTable container = new FlexTable();

        private boolean isSelected = false;
        private Image selected;
        private Image unselected;

        public DisplayerSubTypeImageWidget( ImageResource selectedImage,
                                            ImageResource unselectedImage,
                                            String tooltip,
                                            boolean initiallySelected) {

            initWidget(container);

            isSelected = initiallySelected;

            if (selectedImage != null) {
                selected = new Image(selectedImage);
                selected.setType(ImageType.POLAROID);
                selected.setTitle(tooltip);
                container.setWidget(0, 0, selected);
                selected.setVisible(isSelected);
            }

            if (unselectedImage != null) {
                unselected = new Image(unselectedImage);
                unselected.setType(ImageType.POLAROID);
                unselected.setTitle(tooltip);
                container.setWidget(0, 1, unselected);
                unselected.setVisible(!isSelected);
            }
        }

        public HandlerRegistration setSelectClickHandler(ClickHandler selectedClickHandler) {
            return unselected != null ? unselected.addClickHandler(selectedClickHandler) : null;
        }

        public void select() {
            isSelected = true;
            selected.setVisible(true);
            unselected.setVisible(false);
        }

        public void unselect() {
            isSelected = false;
            selected.setVisible(false);
            unselected.setVisible(true);
        }
    }
}

@@ -3384,24 +3384,112 @@
 etUUID());%0A%0A
+                    String resourcePrefix = type.toString() + %22_%22 + subtype.toString();%0A
             
@@ -3530,32 +3530,33 @@
  (ImageResource)
+ 
 DisplayerImagesR
@@ -3589,50 +3589,22 @@
 rce(
-type.toString() + %22_%22 + subtype.toString()
+resourcePrefix
  + D
@@ -3701,32 +3701,33 @@
  (ImageResource)
+ 
 DisplayerImagesR
@@ -3760,50 +3760,22 @@
 rce(
-type.toString() + %22_%22 + subtype.toString()
+resourcePrefix
  + D
@@ -3900,50 +3900,22 @@
 ing(
-type.toString() + %22_%22 + subtype.toString()
+resourcePrefix
  + %22
@@ -6886,35 +6886,31 @@
     
-container.setWidget(0, 0, s
+selected.setVisible(isS
 elec
@@ -6936,38 +6936,114 @@
    selected.
-setVisible(isS
+addStyleName(%22selDispSubtype%22); //for selenium%0A                container.setWidget(0, 0, s
 elected);%0A  
@@ -7277,89 +7277,89 @@
     
-container.setWidget(0, 1, unselected);%0A                unselected.setVisible(!isS
+unselected.setVisible(!isSelected);%0A                container.setWidget(0, 1, uns
 elec

Add style class for selected subtype image