/*
 * 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 org.apache.shardingsphere.core.parse.sql.context.condition.Column;
import org.apache.shardingsphere.core.parse.sql.context.condition.Condition;
import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.SetAssignmentsSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.token.impl.EncryptColumnToken;
import org.apache.shardingsphere.core.rule.EncryptRule;

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

/**
 * Encrypt column token generator.
 *
 * @author panjuan
 */
public final class EncryptColumnTokenGenerator implements CollectionSQLTokenGenerator<EncryptRule> {
    
    @Override
    public Collection<EncryptColumnToken> generateSQLTokens(final SQLStatement sqlStatement, final EncryptRule encryptRule) {
        Collection<EncryptColumnToken> result = new LinkedList<>();
        for (SQLSegment each : sqlStatement.getSqlSegments()) {
            if (each instanceof SetAssignmentsSegment) {
                result.addAll(createEncryptColumnTokensFromSetAssignment(sqlStatement, encryptRule, (SetAssignmentsSegment) each));
            }
        }
        result.addAll(createEncryptColumnTokensFromWhereCondition(sqlStatement));
        return result;
    }
    
    private Collection<EncryptColumnToken> createEncryptColumnTokensFromSetAssignment(final SQLStatement sqlStatement, final EncryptRule encryptRule, final SetAssignmentsSegment segment) {
        Collection<EncryptColumnToken> result = new LinkedList<>();
        for (AssignmentSegment each : segment.getAssignments()) {
            Column column = new Column(each.getColumn().getName(), sqlStatement.getTables().getSingleTableName());
            if (encryptRule.getEncryptorEngine().getShardingEncryptor(column.getTableName(), column.getName()).isPresent()) {
                result.add(new EncryptColumnToken(each.getColumn().getStartIndex(), each.getStopIndex(), column, false));
            }
        }
        return result;
    }
    
    private Collection<EncryptColumnToken> createEncryptColumnTokensFromWhereCondition(final SQLStatement sqlStatement) {
        Collection<EncryptColumnToken> result = new LinkedList<>();
        if (sqlStatement.getEncryptCondition().getOrConditions().isEmpty()) {
            return result;
        }
        for (Condition each : sqlStatement.getEncryptCondition().getOrConditions().get(0).getConditions()) {
            result.add(new EncryptColumnToken(each.getPredicateSegment().getStartIndex(), each.getPredicateSegment().getStopIndex(), each.getColumn(), true));
        }
        return result;
    }
}

@@ -1332,24 +1332,103 @@
 LStatement;%0A
+import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement;%0A
 import org.a
@@ -2146,39 +2146,26 @@
 l(create
-EncryptColumnTokensFrom
+FromUpdate
 SetAssig
@@ -2281,35 +2281,16 @@
 l(create
-EncryptColumnTokens
 FromWher
@@ -2399,39 +2399,26 @@
 %3E create
-EncryptColumnTokensFrom
+FromUpdate
 SetAssig
@@ -2586,32 +2586,124 @@
 LinkedList%3C%3E();%0A
+        if (sqlStatement instanceof InsertStatement) %7B%0A            return result;%0A        %7D%0A
         for (Ass
@@ -3226,27 +3226,8 @@
 eate
-EncryptColumnTokens
 From

rename to createFromUpdateSetAssignment