/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * 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.
 * </p>
 */

package io.shardingsphere.core.executor.sql.execute.result;

import com.google.common.base.Function;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import io.shardingsphere.core.executor.sql.execute.row.QueryRow;
import io.shardingsphere.core.merger.QueryResult;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.SneakyThrows;

import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Distinct query result.
 *
 * @author panjuan
 */
@Getter(AccessLevel.PROTECTED)
public class DistinctQueryResult implements QueryResult {
    
    private final Multimap<String, Integer> columnLabelAndIndexMap;
    
    private final Iterator<QueryRow> resultData;
    
    private QueryRow currentRow;
    
    protected DistinctQueryResult(final Multimap<String, Integer> columnLabelAndIndexMap, final Iterator<QueryRow> resultData) {
        this.columnLabelAndIndexMap = columnLabelAndIndexMap;
        this.resultData = resultData;
    }
    
    @SneakyThrows
    public DistinctQueryResult(final Collection<QueryResult> queryResults, final List<String> distinctColumnLabels) {
        this.columnLabelAndIndexMap = getColumnLabelAndIndexMap(queryResults.iterator().next());
        resultData = getResultData(queryResults, distinctColumnLabels);
    }
    
    @SneakyThrows
    private Multimap<String, Integer> getColumnLabelAndIndexMap(final QueryResult queryResult) {
        Multimap<String, Integer> result = HashMultimap.create();
        for (int columnIndex = 1; columnIndex <= queryResult.getColumnCount(); columnIndex++) {
            result.put(queryResult.getColumnLabel(columnIndex), columnIndex);
        }
        return result;
    }
    
    @SneakyThrows
    private Iterator<QueryRow> getResultData(final Collection<QueryResult> queryResults, final List<String> distinctColumnLabels) {
        Set<QueryRow> resultData = new LinkedHashSet<>();
        List<Integer> distinctColumnIndexes = Lists.transform(distinctColumnLabels, new Function<String, Integer>() {
    
            @Override
            public Integer apply(final String input) {
                return getColumnIndex(input);
            }
        });
        for (QueryResult each : queryResults) {
            fill(resultData, each, distinctColumnIndexes);
        }
        return resultData.iterator();
    }
    
    private void fill(final Set<QueryRow> resultData, final QueryResult queryResult, final List<Integer> distinctColumnIndexes) throws SQLException {
        while (queryResult.next()) {
            List<Object> rowData = new ArrayList<>(queryResult.getColumnCount());
            for (int columnIndex = 1; columnIndex <= queryResult.getColumnCount(); columnIndex++) {
                rowData.add(queryResult.getValue(columnIndex, Object.class));
            }
            resultData.add(new QueryRow(rowData, distinctColumnIndexes));
        }
    }
    
    /**
     * Divide one distinct query result to multiple child ones.
     *
     * @return multiple child distinct query results
     */
    public List<DistinctQueryResult> divide() {
        return Lists.newArrayList(Iterators.transform(resultData, new Function<QueryRow, DistinctQueryResult>() {
    
            @Override
            public DistinctQueryResult apply(final QueryRow row) {
                Set<QueryRow> resultData = new LinkedHashSet<>();
                resultData.add(row);
                return new DistinctQueryResult(columnLabelAndIndexMap, resultData.iterator());
            }
        }));
    }
    
    @Override
    public boolean next() {
        if (resultData.hasNext()) {
            currentRow = resultData.next();
            return true;
        }
        currentRow = null;
        return false;
    }
    
    @Override
    public Object getValue(final int columnIndex, final Class<?> type) {
        return currentRow.getColumnValue(columnIndex);
    }
    
    @Override
    public Object getValue(final String columnLabel, final Class<?> type) {
        return currentRow.getColumnValue(getColumnIndex(columnLabel));
    }
    
    @Override
    public Object getCalendarValue(final int columnIndex, final Class<?> type, final Calendar calendar) {
        return currentRow.getColumnValue(columnIndex);
    }
    
    @Override
    public Object getCalendarValue(final String columnLabel, final Class<?> type, final Calendar calendar) {
        return currentRow.getColumnValue(getColumnIndex(columnLabel));
    }
    
    @Override
    public InputStream getInputStream(final int columnIndex, final String type) {
        return (InputStream) currentRow.getColumnValue(columnIndex);
    }
    
    @Override
    public InputStream getInputStream(final String columnLabel, final String type) {
        return (InputStream) currentRow.getColumnValue(getColumnIndex(columnLabel));
    }
    
    @Override
    public boolean wasNull() {
        return null == currentRow;
    }
    
    @Override
    public int getColumnCount() {
        return columnLabelAndIndexMap.size();
    }
    
    @Override
    public String getColumnLabel(final int columnIndex) throws SQLException {
        for (Entry<String, Integer> entry : columnLabelAndIndexMap.entries()) {
            if (columnIndex == entry.getValue()) {
                return entry.getKey();
            }
        }
        throw new SQLException("Column index out of range", "9999");
    }
    
    protected Integer getColumnIndex(final String columnLabel) {
        return new ArrayList<>(columnLabelAndIndexMap.get(columnLabel)).get(0);
    }
}

@@ -1096,18 +1096,128 @@
 java.io.
-In
+ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.InputStream;%0Aimport java.io.ObjectOut
 putStrea
@@ -5606,33 +5606,35 @@
 %0A        return 
-(
+get
 InputStream) cur
@@ -5620,34 +5620,33 @@
 n getInputStream
-) 
+(
 currentRow.getCo
@@ -5659,32 +5659,33 @@
 lue(columnIndex)
+)
 ;%0A    %7D%0A    %0A   
@@ -5795,17 +5795,19 @@
  return 
-(
+get
 InputStr
@@ -5809,18 +5809,17 @@
 utStream
-) 
+(
 currentR
@@ -5856,32 +5856,498 @@
 ex(columnLabel))
+);%0A    %7D%0A    %0A    @SneakyThrows%0A    private InputStream getInputStream(final Object value) %7B%0A        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();%0A        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);%0A        objectOutputStream.writeObject(value);%0A        objectOutputStream.flush();%0A        objectOutputStream.close();%0A        return new ByteArrayInputStream(byteArrayOutputStream.toByteArray())
 ;%0A    %7D%0A    %0A   

fix bug for InputStream