/**
 * Copyright (C) 2014 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.kie.dockerui.client.widgets.container.navigator.item;

import com.github.gwtbootstrap.client.ui.AccordionGroup;
import com.github.gwtbootstrap.client.ui.Column;
import com.github.gwtbootstrap.client.ui.FluidRow;
import com.github.gwtbootstrap.client.ui.Heading;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;
import org.kie.dockerui.client.resources.i18n.Constants;

import java.util.List;

public class CompositeNavigationItemView extends Composite {

    interface CompositeNavigationItemViewBinder extends UiBinder<Widget, CompositeNavigationItemView> {}
    private static CompositeNavigationItemViewBinder uiBinder = GWT.create(CompositeNavigationItemViewBinder.class);

    public static CompositeNavigationItemView build(final boolean isDefaultOpen, final NavigationItemView.NavigationItemSelectedEventHandler handler) {
        final CompositeNavigationItemView navigationItemView = GWT.create(CompositeNavigationItemView.class);
        navigationItemView.addNavigationItemSelectedEventHandler(handler);
        navigationItemView.accordionGroup.setDefaultOpen(isDefaultOpen);
        return navigationItemView;
    }

    @UiField
    AccordionGroup accordionGroup;
    
    @UiField
    Heading title;
    
    @UiConstructor
    public CompositeNavigationItemView() {
        initWidget(uiBinder.createAndBindUi(this));
        accordionGroup.getWidget(0).getElement().getStyle().setBackgroundColor("whitesmoke");
    }

    public void show(final CompositeNavigationItem navigationItem) {
        final String _title = navigationItem.getTitle();
        final List<NavigationItem> items = navigationItem.getItems();
        configureHeader(_title);
        if (items != null && !items.isEmpty()) {
            final int itemsCount = items.size();
            int colCount = 12;
            int rowCount = 0;
            FluidRow row = new FluidRow();
            for (final NavigationItem item : items) {
                int offset = -1;
                if (colCount == 12) {
                    row = new FluidRow();
                    accordionGroup.add(row);
                    colCount = 0;
                    
                    // Pre-calculate the first column offset in order to align items at row's center position.
                    offset = itemsCount - ( rowCount * 12 );
                    if (offset > 0 && offset < 12) {
                        offset = ( 12 - offset ) / 2;
                    } else {
                        offset = -1;
                    }
                    rowCount++;
                }
                
                final Column column = new Column(1, offset > -1 ? offset : 0);
                showItemView(column, item);
                row.add(column);
                colCount++;
            }
        } else {
            showEmptyView();
        }
    }

    private void showEmptyView() {
        accordionGroup.add(new Heading(5, Constants.INSTANCE.noData()))    ;    
    }

    private void configureHeader(final String _title) {
        title.setText(_title);
    }

    private void showItemView(final Panel parent, final NavigationItem item) {
        final NavigationItemView itemView = NavigationItemView.build(navigationItemSelectedEventHandler);
        itemView.getElement().getStyle().setMarginLeft(50, Style.Unit.PX);
        itemView.show(item);
        parent.add(itemView);
    }

    public HandlerRegistration addNavigationItemSelectedEventHandler(final NavigationItemView.NavigationItemSelectedEventHandler handler) {
        return addHandler(handler, NavigationItemView.NavigationItemSelectedEvent.TYPE);
    }

    private final NavigationItemView.NavigationItemSelectedEventHandler navigationItemSelectedEventHandler = new NavigationItemView.NavigationItemSelectedEventHandler() {
        @Override
        public void onNavigationItemSelected(NavigationItemView.NavigationItemSelectedEvent event) {
            CompositeNavigationItemView.this.fireEvent(event);
        }
    };
    
}

@@ -1431,16 +1431,65 @@
 site %7B%0A%0A
+    private static final int COLUMNS_BY_ROW = 6;%0A
     inte
@@ -2814,18 +2814,30 @@
 Count = 
-12
+COLUMNS_BY_ROW
 ;%0A      
@@ -3022,18 +3022,30 @@
 ount == 
-12
+COLUMNS_BY_ROW
 ) %7B%0A    
@@ -3348,18 +3348,30 @@
 Count * 
-12
+COLUMNS_BY_ROW
  );%0A    
@@ -3413,18 +3413,30 @@
 ffset %3C 
-12
+COLUMNS_BY_ROW
 ) %7B%0A    
@@ -3470,10 +3470,22 @@
 = ( 
-12
+COLUMNS_BY_ROW
  - o
@@ -3701,17 +3701,17 @@
  Column(
-1
+2
 , offset
@@ -3729,11 +3729,116 @@
 set 
-: 0
+* ( 12 / COLUMNS_BY_ROW) : 0);%0A                column.getElement().getStyle().setProperty(%22minWidth%22,%22150px%22
 );%0A 

fix calendar size