/*
 * Copyright 2014 CyberVision, 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.kaaproject.kaa.server.admin.client.mvp.view.schema;

import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.Header;
import com.google.gwt.user.cellview.client.SafeHtmlHeader;
import org.kaaproject.avro.ui.gwt.client.widget.grid.cell.ActionButtonCell;
import org.kaaproject.avro.ui.gwt.client.widget.grid.event.RowActionEvent;
import org.kaaproject.kaa.common.dto.AbstractSchemaDto;
import org.kaaproject.kaa.server.admin.client.mvp.view.grid.AbstractKaaGrid;
import org.kaaproject.kaa.server.admin.client.mvp.view.grid.KaaRowAction;
import org.kaaproject.kaa.server.admin.client.util.Utils;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.cellview.client.DataGrid;

public class BaseSchemasGrid<T extends AbstractSchemaDto> extends AbstractKaaGrid<T, String> {

    private Column<T, T> downloadSchemaColumn;

    public BaseSchemasGrid() {
        super(Unit.PX, false);
    }

    @Override
    protected float constructColumnsImpl(DataGrid<T> table) {
        float prefWidth = 0;

        prefWidth += constructStringColumn(table,
                Utils.constants.version(),
                new StringValueProvider<T>() {
                    @Override
                    public String getValue(T item) {
                        return item.getMajorVersion() + "." + item.getMinorVersion();
                    }
                }, 80);

        prefWidth += constructStringColumn(table,
                Utils.constants.name(),
                new StringValueProvider<T>() {
                    @Override
                    public String getValue(T item) {
                        return item.getName();
                    }
                }, 80);

        prefWidth += constructStringColumn(table,
                Utils.constants.author(),
                new StringValueProvider<T>() {
                    @Override
                    public String getValue(T item) {
                        return item.getCreatedUsername();
                    }
                }, 80);

        prefWidth += constructStringColumn(table,
                Utils.constants.dateCreated(),
                new StringValueProvider<T>() {
                    @Override
                    public String getValue(T item) {
                        return Utils.millisecondsToDateString(item.getCreatedTime());
                    }
                }, 80);

        prefWidth += constructStringColumn(table,
                Utils.constants.numberOfEps(),
                new StringValueProvider<T>() {
                    @Override
                    public String getValue(T item) {
                        return item.getEndpointCount() + "";
                    }
                }, 80);

        return prefWidth;
    }

    @Override
    protected float constructActions(DataGrid<T> table, float prefWidth) {
        float result = 0;
        if (!embedded && (downloadSchemaColumn == null || table.getColumnIndex(downloadSchemaColumn) == -1)) {
            Header<SafeHtml> downloadRecordSchemaHeader = new SafeHtmlHeader(
                    SafeHtmlUtils.fromSafeConstant(Utils.constants.downloadRecordSchema()));
            downloadSchemaColumn = constructDownloadSchemaColumn("");
            table.addColumn(downloadSchemaColumn, downloadRecordSchemaHeader);
            table.setColumnWidth(downloadSchemaColumn, ACTION_COLUMN_WIDTH, Unit.PX);
            result += ACTION_COLUMN_WIDTH;
        }
        return result;
    }

    protected Column<T, T> constructDownloadSchemaColumn(String text) {
        ActionButtonCell<T> cell = new ActionButtonCell<T>(Utils.resources.download(), text, embedded,
                new ActionButtonCell.ActionListener<T>() {
                    @Override
                    public void onItemAction(T value) {
                        Integer schemaVersion = value.getMajorVersion();
                        RowActionEvent<String> rowDownloadSchemaEvent = new RowActionEvent<>(String.valueOf(schemaVersion), KaaRowAction.DOWNLOAD_SCHEMA);
                        fireEvent(rowDownloadSchemaEvent);
                    }
                }, new ActionButtonCell.ActionValidator<T>() {
            @Override
            public boolean canPerformAction(T value) {
                return !embedded;
            }
        });
        Column<T, T> column = new Column<T, T>(cell) {
            @Override
            public T getValue(T item) {
                return item;
            }
        };
        return column;
    }

}


@@ -672,262 +672,30 @@
 ort 
-com.google.gwt.safehtml.shared.SafeHtml;%0Aimport com.google.gwt.safehtml.shared.SafeHtmlUtils;%0Aimport com.google.gwt.user.cellview.client.Column;%0Aimport com.google.gwt.user.cellview.client.Header;%0Aimport com.google.gwt.user.cellview.client.SafeHtmlHeader;
+java.util.Comparator;%0A
 %0Aimp
@@ -1179,37 +1179,299 @@
 gwt.
-user.cellview.client.DataGrid
+safehtml.shared.SafeHtml;%0Aimport com.google.gwt.safehtml.shared.SafeHtmlUtils;%0Aimport com.google.gwt.user.cellview.client.Column;%0Aimport com.google.gwt.user.cellview.client.DataGrid;%0Aimport com.google.gwt.user.cellview.client.Header;%0Aimport com.google.gwt.user.cellview.client.SafeHtmlHeader
 ;%0A%0Ap
@@ -2130,32 +2130,292 @@
              %7D, 
+%0A                new Comparator%3CT%3E() %7B%0A                    @Override%0A                    public int compare(T o1, T o2) %7B%0A                        return o1.compareTo(o2);%0A                    %7D%0A                %7D,%0A                Boolean.FALSE, %0A                
 80);%0A%0A        pr
@@ -2704,32 +2704,313 @@
              %7D, 
+%0A                new Comparator%3CT%3E() %7B%0A                    @Override%0A                    public int compare(T o1, T o2) %7B%0A                        return o1.getName().compareToIgnoreCase(o2.getName());%0A                    %7D%0A                %7D,%0A                null, %0A                
 80);%0A%0A        pr

Add schemas sort by version with descending sort order by default.