/*
 * Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
 * license agreements.  See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.  Crate 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.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial agreement.
 */

package io.crate.metadata;

import io.crate.analyze.symbol.Function;
import io.crate.analyze.symbol.Literal;
import io.crate.analyze.symbol.Symbol;
import io.crate.operation.Input;

import java.util.Collection;
import java.util.List;

/**
 * evaluable function implementation
 *
 * @param <ReturnType> the class of the returned value
 */
public abstract class Scalar<ReturnType, InputType> implements FunctionImplementation {

    public abstract ReturnType evaluate(Input<InputType>... args);

    /**
     * Returns a optional compiled version of the scalar implementation.
     */
    public Scalar<ReturnType, InputType> compile(List<Symbol> arguments) {
        return this;
    }

    @Override
    public Symbol normalizeSymbol(Function symbol, TransactionContext transactionContext) {
        try {
            return evaluateIfLiterals(this, symbol);
        } catch (Throwable t) {
            return symbol;
        }
    }

    protected static boolean anyNonLiterals(Collection<? extends Symbol> arguments) {
        for (Symbol symbol : arguments) {
            if (!symbol.symbolType().isValueSymbol()) {
                return true;
            }
        }
        return false;
    }

    /**
     * This method will evaluate the function using the given scalar if all arguments are literals.
     * Otherwise it will return the function as is or NULL in case it contains a null literal
     */
    private static <ReturnType, InputType> Symbol evaluateIfLiterals(Scalar<ReturnType, InputType> scalar, Function function) {
        Input[] inputs = new Input[function.arguments().size()];
        int idx = 0;
        for (Symbol arg : function.arguments()) {
            if (arg instanceof Input) {
                Input inputArg = (Input) arg;
                inputs[idx] = inputArg;
                idx++;
            } else {
                return function;
            }
        }
        //noinspection unchecked
        return Literal.of(function.info().returnType(), scalar.evaluate(inputs));
    }
}

@@ -2565,84 +2565,53 @@
     
-Input%5B%5D inputs = new Input%5Bfunction.arguments().size()%5D;%0A        int idx = 0
+List%3CSymbol%3E arguments = function.arguments()
 ;%0A  
@@ -2635,20 +2635,16 @@
  arg
- : function.
+ument : 
 argu
@@ -2648,18 +2648,16 @@
 rguments
-()
 ) %7B%0A    
@@ -2671,12 +2671,19 @@
  if 
+(!
 (arg
+ument
  ins
@@ -2696,16 +2696,17 @@
 f Input)
+)
  %7B%0A     
@@ -2720,37 +2720,38 @@
     
-Input inputArg = (Input) arg;
+return function;%0A            %7D
 %0A   
@@ -2755,24 +2755,26 @@
         
+%7D%0A
         
 inputs%5Bi
@@ -2769,107 +2769,151 @@
     
-i
+I
 nput
-s%5Bidx%5D = inputArg;%0A                idx++;%0A            %7D else %7B%0A                return function
+%5B%5D inputs = new Input%5Barguments.size()%5D;%0A        int idx = 0;%0A        for (Symbol arg : arguments) %7B%0A            inputs%5Bidx%5D = (Input) arg
 ;%0A  
@@ -2914,33 +2914,38 @@
 rg;%0A            
-%7D
+idx++;
 %0A        %7D%0A     

Optimize evaluateIfLiterals