/*
 * 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.base.Predicate;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import io.shardingsphere.core.merger.QueryResult;
import io.shardingsphere.core.parsing.parser.context.selectitem.AggregationDistinctSelectItem;
import io.shardingsphere.core.parsing.parser.context.selectitem.AggregationSelectItem;
import io.shardingsphere.core.parsing.parser.sql.dql.select.SelectStatement;
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.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Aggregation distinct query result.
 *
 * @author panjuan
 */
public final class AggregationDistinctQueryResult extends DistinctQueryResult {
    
    private final Multimap<String, Integer> aggregationExpressionAndColumnIndexes = HashMultimap.create();
    
    private final Map<Integer, Integer> derivedCountIndexAndDistinctIndexes = new LinkedHashMap<>();
    
    private final Map<Integer, Integer> derivedSumIndexAndDistinctIndexes = new LinkedHashMap<>();
    
    private AggregationDistinctQueryResult(final Multimap<String, Integer> columnLabelAndIndexMap, final Iterator<List<Object>> resultData,
                                           final Multimap<String, Integer> aggregationExpressionAndColumnIndexes,
                                           final Map<Integer, Integer> derivedCountIndexAndDistinctIndexes, final Map<Integer, Integer> derivedSumIndexAndDistinctIndexes) {
        super(columnLabelAndIndexMap, resultData);
        this.aggregationExpressionAndColumnIndexes.putAll(aggregationExpressionAndColumnIndexes);
        this.derivedCountIndexAndDistinctIndexes.putAll(derivedCountIndexAndDistinctIndexes);
        this.derivedSumIndexAndDistinctIndexes.putAll(derivedSumIndexAndDistinctIndexes);
    }
    
    @SneakyThrows
    public AggregationDistinctQueryResult(final Collection<QueryResult> queryResults, final SelectStatement selectStatement) {
        super(queryResults);
        init(selectStatement);
    }
    
    private void init(final SelectStatement selectStatement) {
        for (AggregationSelectItem each : selectStatement.getAggregationSelectItems()) {
            initAggregationExpressionAndColumnIndexes(each);
            initDerivedIndexAndDistinctIndexes(each);
        }
    }
    
    private void initDerivedIndexAndDistinctIndexes(final AggregationSelectItem selectItem) {
        List<AggregationSelectItem> derivedAggregationSelectItems = selectItem.getDerivedAggregationSelectItems();
        if (!derivedAggregationSelectItems.isEmpty()) {
            derivedCountIndexAndDistinctIndexes.put(derivedAggregationSelectItems.get(0).getIndex(), selectItem.getIndex());
            derivedSumIndexAndDistinctIndexes.put(derivedAggregationSelectItems.get(1).getIndex(), selectItem.getIndex());
        }
    }
    
    private void initAggregationExpressionAndColumnIndexes(final AggregationSelectItem selectItem) {
        if (selectItem instanceof AggregationDistinctSelectItem) {
            AggregationDistinctSelectItem distinctSelectItem = (AggregationDistinctSelectItem) selectItem;
            aggregationExpressionAndColumnIndexes.put(distinctSelectItem.getInnerExpression(), distinctSelectItem.getIndex());
        }
    }
    
    /**
     * Divide one distinct query result to multiple child ones.
     *
     * @return multiple child distinct query results
     */
    @Override
    public List<DistinctQueryResult> divide() {
        return Lists.newArrayList(Iterators.transform(getResultData(), new Function<List<Object>, DistinctQueryResult>() {
    
            @Override
            public DistinctQueryResult apply(final List<Object> input) {
                Set<List<Object>> resultData = new LinkedHashSet<>();
                resultData.add(input);
                return new AggregationDistinctQueryResult(getColumnLabelAndIndexMap(),
                        resultData.iterator(), aggregationExpressionAndColumnIndexes, derivedCountIndexAndDistinctIndexes, derivedSumIndexAndDistinctIndexes);
            }
        }));
    }
    
    private Object getValue(final int columnIndex) {
        if (derivedCountIndexAndDistinctIndexes.keySet().contains(columnIndex)) {
            return 1;
        }
        if (derivedSumIndexAndDistinctIndexes.keySet().contains(columnIndex)) {
            return getCurrentRow().get(derivedSumIndexAndDistinctIndexes.get(columnIndex) - 1);
        }
        return getCurrentRow().get(columnIndex - 1);
    }
    
    private Object getValue(final String columnLabel) {
        return getValue(getIndexByColumnLabel(columnLabel));
    }
    
    @Override
    public Object getValue(final int columnIndex, final Class<?> type) {
        return getValue(columnIndex);
    }
    
    @Override
    public Object getValue(final String columnLabel, final Class<?> type) {
        return getValue(columnLabel);
    }
    
    @Override
    public Object getCalendarValue(final int columnIndex, final Class<?> type, final Calendar calendar) {
        return getValue(columnIndex);
    }
    
    @Override
    public Object getCalendarValue(final String columnLabel, final Class<?> type, final Calendar calendar) {
        return getValue(columnLabel);
    }
    
    @Override
    public InputStream getInputStream(final int columnIndex, final String type) {
        return (InputStream) getValue(columnIndex);
    }
    
    @Override
    public InputStream getInputStream(final String columnLabel, final String type) {
        return (InputStream) getValue(columnLabel);
    }
    
    @Override
    public boolean wasNull() {
        return null == getCurrentRow();
    }
    
    @Override
    public int getColumnCount() {
        return getColumnLabelAndIndexMap().size();
    }
    
    @Override
    public String getColumnLabel(final int columnIndex) throws SQLException {
        for (Entry<String, Integer> entry : getColumnLabelAndIndexMap().entries()) {
            if (columnIndex == entry.getValue()) {
                if (distinctColumnNameAndAggregationExpressions.containsKey(entry.getKey())) {
                    return distinctColumnNameAndAggregationExpressions.get(entry.getKey());
                }
                return entry.getKey();
            }
        }
        throw new SQLException("Column index out of range", "9999");
    }
    
    private Integer getIndexByColumnLabel(final String columnLabel) {
        String result = columnLabel;
        Map<String, String> filtered = new HashMap<>();
        if (distinctColumnNameAndAggregationExpressions.containsValue(columnLabel)) {
            filtered.putAll(Maps.filterValues(distinctColumnNameAndAggregationExpressions, new Predicate<String>() {
                @Override
                public boolean apply(final String input) {
                    return columnLabel.equals(input);
                }
            }));
        }
        return filtered.isEmpty() ? new ArrayList<>(getColumnLabelAndIndexMap().get(columnLabel)).get(0) - 1 : ;
    }
}

@@ -7014,35 +7014,45 @@
 y : 
-getColumnLabelAndIndexMap()
+aggregationExpressionAndColumnIndexes
 .ent
@@ -7132,68 +7132,15 @@
     
-if (distinctColumnNameAndAggregationExpressions.containsKey(
+return 
 entr
@@ -7149,20 +7149,17 @@
 getKey()
-)) %7B
+;
 %0A       
@@ -7159,32 +7159,34 @@
             
+%7D%0A
         
 return disti
@@ -7177,79 +7177,94 @@
     
-return distinctColumnNameAndAggregationExpressions.get(entry.getKey());
+%7D%0A        for (Entry%3CString, Integer%3E entry : getColumnLabelAndIndexMap().entries()) %7B
 %0A   
@@ -7264,37 +7264,70 @@
 ) %7B%0A            
-    %7D
+if (columnIndex == entry.getValue()) %7B
 %0A               

modify getColumnLabel()