/*
 * 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.statement.OptimizedStatement;
import org.apache.shardingsphere.core.optimize.statement.sharding.dml.insert.ShardingInsertOptimizedStatement;
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.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement;
import org.apache.shardingsphere.core.rewrite.token.pojo.InsertSetEncryptValueToken;
import org.apache.shardingsphere.core.rule.EncryptRule;
import org.apache.shardingsphere.spi.encrypt.ShardingEncryptor;

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

/**
 * Insert set encrypt value token generator.
 *
 * @author panjuan
 */
public final class InsertSetEncryptValueTokenGenerator implements CollectionSQLTokenGenerator<EncryptRule> {
    
    @Override
    public Collection<InsertSetEncryptValueToken> generateSQLTokens(final OptimizedStatement optimizedStatement, final List<Object> parameters, final EncryptRule encryptRule) {
        Optional<SetAssignmentsSegment> setAssignmentsSegment = optimizedStatement.getSQLStatement().findSQLSegment(SetAssignmentsSegment.class);
        if (!(optimizedStatement.getSQLStatement() instanceof InsertStatement && setAssignmentsSegment.isPresent())) {
            return Collections.emptyList();
        }
        return createInsertSetEncryptValueTokens((InsertStatement) optimizedStatement.getSQLStatement(), encryptRule, (ShardingInsertOptimizedStatement) optimizedStatement, setAssignmentsSegment.get());
    }
    
    private Collection<InsertSetEncryptValueToken> createInsertSetEncryptValueTokens(final InsertStatement insertStatement, final EncryptRule encryptRule, final ShardingInsertOptimizedStatement optimizedStatement, final SetAssignmentsSegment segment) {
        Collection<InsertSetEncryptValueToken> result = new LinkedList<>();
        for (AssignmentSegment each : segment.getAssignments()) {
            Optional<InsertSetEncryptValueToken> insertSetEncryptValueToken = getInsertSetEncryptValueToken(insertStatement, encryptRule, optimizedStatement, each);
            if (insertSetEncryptValueToken.isPresent()) {
                result.add(insertSetEncryptValueToken.get());
            }
        }
        return result;
    }
    
    private Optional<InsertSetEncryptValueToken> getInsertSetEncryptValueToken(final InsertStatement insertStatement, final EncryptRule encryptRule, final ShardingInsertOptimizedStatement optimizedStatement, final AssignmentSegment segment) {
        Optional<ShardingEncryptor> shardingEncryptor = encryptRule.getEncryptorEngine().getShardingEncryptor(insertStatement.getTables().getSingleTableName(), segment.getColumn().getName());
        if (shardingEncryptor.isPresent() && !(segment.getValue() instanceof ParameterMarkerExpressionSegment)) {
            return Optional.of(new InsertSetEncryptValueToken(segment.getValue().getStartIndex(), segment.getValue().getStopIndex(), optimizedStatement.getUnits().get(0).getColumnSQLExpression(segment.getColumn().getName())));
        }
        return Optional.absent();
    }
}

@@ -2524,16 +2524,33 @@
 eTokens(
+%0A                
 (InsertS
@@ -2787,24 +2787,37 @@
 ValueTokens(
+%0A            
 final Insert
@@ -3192,19 +3192,22 @@
 Token = 
-get
+create
 InsertSe
@@ -3509,19 +3509,22 @@
 eToken%3E 
-get
+create
 InsertSe
@@ -3542,16 +3542,29 @@
 ueToken(
+%0A            
 final In
@@ -4087,16 +4087,37 @@
 ueToken(
+%0A                    
 segment.

check style