/*
 * Copyright 2017 Crown Copyright
 *
 * 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 stroom.search.elastic.client.presenter;

import stroom.data.grid.client.DataGridView;
import stroom.data.grid.client.DataGridViewImpl;
import stroom.data.grid.client.EndColumn;
import stroom.entity.client.presenter.HasDocumentRead;
import stroom.query.api.v2.DocRef;
import stroom.search.elastic.shared.ElasticIndex;
import stroom.search.elastic.shared.ElasticIndexField;
import stroom.widget.customdatebox.client.ClientDateUtil;

import com.google.gwt.cell.client.TextCell;
import com.google.gwt.user.cellview.client.Column;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.MyPresenterWidget;
import com.gwtplatform.mvp.client.View;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class ElasticIndexFieldListPresenter extends MyPresenterWidget<ElasticIndexFieldListPresenter.ElasticIndexFieldListView> implements HasDocumentRead<ElasticIndex> {
    private final DataGridView<ElasticIndexField> dataGridView;
    private List<ElasticIndexField> fields;
    private ElasticIndexFieldDataProvider<ElasticIndexField> dataProvider;

    @Inject
    public ElasticIndexFieldListPresenter(final EventBus eventBus,
                                          final ElasticIndexFieldListView view
    ) {
        super(eventBus, view);

        dataGridView = new DataGridViewImpl<>(true, true);
        view.setDataGridView(dataGridView);

        addColumns();
    }

    private void addColumns() {
        addStringColumn("Name", 150, ElasticIndexField::getFieldName);
        addStringColumn("Use", row -> row.getFieldUse().getDisplayValue());
        addStringColumn("Type", ElasticIndexField::getFieldType);
        addBooleanColumn("Stored", ElasticIndexField::isStored);
        addBooleanColumn("Indexed", ElasticIndexField::isIndexed);
        dataGridView.addEndColumn(new EndColumn<>());
    }

    private void addStringColumn(final String name, final Function<ElasticIndexField, String> function) {
        addStringColumn(name, 100, function);
    }

    private void addStringColumn(final String name, final int width, final Function<ElasticIndexField, String> function) {
        dataGridView.addResizableColumn(new Column<ElasticIndexField, String>(new TextCell()) {
            @Override
            public String getValue(final ElasticIndexField row) {
                return function.apply(row);
            }
        }, name, width);
    }

    private void addBooleanColumn(final String name, final Function<ElasticIndexField, Boolean> function) {
        dataGridView.addResizableColumn(new Column<ElasticIndexField, String>(new TextCell()) {
            @Override
            public String getValue(final ElasticIndexField row) {
                return getYesNoString(function.apply(row));
            }
        }, name, 100);
    }

    private String getYesNoString(final boolean bool) {
        if (bool) {
            return "Yes";
        }
        return "No";
    }

    private void refresh() {
        if (fields == null) {
            fields = new ArrayList<>();
        }

        if (dataProvider == null) {
            this.dataProvider = new ElasticIndexFieldDataProvider<>();
            dataProvider.addDataDisplay(dataGridView.getDataDisplay());
        }

        dataProvider.setList(fields);
        dataProvider.refresh();
    }

    @Override
    public void read(final DocRef docRef, final ElasticIndex index) {
        fields = index.getFields();

        final StringBuilder sb = new StringBuilder();
        String now = ClientDateUtil.toISOString(System.currentTimeMillis());
        sb
            .append("Field list updated at: ")
            .append(now);

        getView().setStatusMessage(sb.toString());
        refresh();
    }

    public interface ElasticIndexFieldListView extends View {
        void setDataGridView(final View view);

        void setStatusMessage(final String syncState);
    }
}

@@ -1633,16 +1633,48 @@
 idView;%0A
+    private ElasticIndex index;%0A
     priv
@@ -4103,24 +4103,86 @@
 ex index) %7B%0A
+        this.index = index;%0A%0A        if (index != null) %7B%0A    
         fiel
@@ -4202,24 +4202,28 @@
 tFields();%0A%0A
+    
         fina
@@ -4272,16 +4272,20 @@
         
+ 
+   
 String n
@@ -4357,11 +4357,19 @@
     
+   
+ 
 sb%0A
+    
     
@@ -4415,32 +4415,36 @@
  %22)%0A            
+    
 .append(now);%0A%0A 
@@ -4450,16 +4450,20 @@
         
+   
+ 
 getView(
@@ -4493,24 +4493,35 @@
 oString());%0A
+        %7D%0A%0A
         refr

Add index null check