/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.shardingsphere.core.rewrite.token.generator;

import com.google.common.base.Optional;
import org.apache.shardingsphere.core.optimize.api.statement.OptimizedStatement;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.ColumnSelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.SelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.SelectItemsSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.rewrite.builder.ParameterBuilder;
import org.apache.shardingsphere.core.rewrite.token.pojo.SelectCipherItemToken;
import org.apache.shardingsphere.core.rule.EncryptRule;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;

/**
 * Select cipher item token generator.
 *
 * @author panjuan
 */
public final class SelectCipherItemTokenGenerator implements CollectionSQLTokenGenerator<EncryptRule> {
    
    private EncryptRule encryptRule;
    
    private OptimizedStatement optimizedStatement;
    
    @Override
    public Collection<SelectCipherItemToken> generateSQLTokens(final OptimizedStatement optimizedStatement, 
                                                               final ParameterBuilder parameterBuilder, final EncryptRule rule, final boolean isQueryWithCipherColumn) {
        if (!isNeedToGenerateSQLToken(optimizedStatement)) {
            return Collections.emptyList();
        }
        initParameters(rule, optimizedStatement);
        return createSelectCipherItemTokens();
    }
    
    private boolean isNeedToGenerateSQLToken(final OptimizedStatement optimizedStatement) {
        if (!isSelectStatementWithTable(optimizedStatement)) {
            return false;
        }
        Optional<SelectItemsSegment> selectItemsSegment = optimizedStatement.getSQLStatement().findSQLSegment(SelectItemsSegment.class);
        return selectItemsSegment.isPresent() && !selectItemsSegment.get().getSelectItems().isEmpty();
    }
    
    private boolean isSelectStatementWithTable(final OptimizedStatement optimizedStatement) {
        return optimizedStatement.getSQLStatement() instanceof SelectStatement && !optimizedStatement.getTables().isEmpty();
    }
    
    private void initParameters(final EncryptRule rule, final OptimizedStatement optimizedStatement) {
        encryptRule = rule;
        this.optimizedStatement = optimizedStatement;
    }
    
    private Collection<SelectCipherItemToken> createSelectCipherItemTokens() {
        Collection<SelectCipherItemToken> result = new LinkedList<>();
        SelectItemsSegment selectItemsSegment = optimizedStatement.getSQLStatement().findSQLSegment(SelectItemsSegment.class).get();
        String tableName = optimizedStatement.getTables().getSingleTableName();
        Collection<String> logicColumns = encryptRule.getLogicColumns(tableName);
        for (SelectItemSegment each : selectItemsSegment.getSelectItems()) {
            if (isLogicColumn(each, logicColumns)) {
                result.add(createSelectCipherItemToken(each, tableName));
            }
        }
        return result;
    }
    
    private boolean isLogicColumn(final SelectItemSegment each, final Collection<String> logicColumns) {
        return each instanceof ColumnSelectItemSegment && logicColumns.contains(((ColumnSelectItemSegment) each).getName());
    }
    
    private SelectCipherItemToken createSelectCipherItemToken(final SelectItemSegment each, final String tableName) {
        return new SelectCipherItemToken(each.getStartIndex(),
                each.getStopIndex(), encryptRule.getCipherColumn(tableName, ((ColumnSelectItemSegment) each).getName()));
    }
}

@@ -1890,24 +1890,74 @@
 ement;%0A    %0A
+    private boolean isQueryWithCipherColumn;%0A    %0A
     @Overrid
@@ -1958,16 +1958,16 @@
 verride%0A
-
     publ
@@ -2398,16 +2398,41 @@
 tatement
+, isQueryWithCipherColumn
 );%0A     
@@ -3247,32 +3247,71 @@
 timizedStatement
+, final boolean isQueryWithCipherColumn
 ) %7B%0A        encr
@@ -3376,24 +3376,88 @@
 dStatement;%0A
+        this.isQueryWithCipherColumn = isQueryWithCipherColumn;%0A
     %7D%0A    %0A 
@@ -4517,24 +4517,257 @@
 ableName) %7B%0A
+        String columnName = ((ColumnSelectItemSegment) each).getName();%0A        Optional%3CString%3E plainColumn = encryptRule.getPlainColumn(tableName, columnName);%0A        if (!isQueryWithCipherColumn && plainColumn.isPresent()) %7B%0A    
         retu
@@ -4816,16 +4816,57 @@
 Index(),
+ each.getStopIndex(), plainColumn.get());
 %0A       
@@ -4866,23 +4866,80 @@
         
+%7D%0A
        
+ return new SelectCipherItemToken(each.getStartIndex(),
  each.ge
@@ -4995,50 +4995,18 @@
 me, 
-((C
+c
 olumn
-SelectItemSegment) each).get
 Name
-()
 ));%0A

add isQueryWithCipherColumn