/*
 *  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  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.
 */

package org.wso2.carbon.uuf.renderablecreator.hbs.core.js;

import jdk.nashorn.api.scripting.ScriptObjectMirror;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

@FunctionalInterface
public interface LogFunction {

    String NAME = "log";

    void call(Object... args);

    default LogLevel getLogLevel(Object arg0) {
        if (!(arg0 instanceof String)) {
            throw new IllegalArgumentException(
                    "Log level must be a string " + Arrays.toString(LogLevel.values()) + ". Instead found" + " '" +
                            arg0.getClass().getName() + "'.");
        }
        try {
            return LogLevel.valueOf((String) arg0);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(
                    "Log level must be " + Arrays.toString(LogLevel.values()) + ". Instead found '" + arg0 + "'.");
        }
    }

    default String getLogMessage(Object arg) {
        if (arg == null) {
            return "null";
        }
        if (arg instanceof ScriptObjectMirror) {
            ScriptObjectMirror jsObject = (ScriptObjectMirror) arg;
            // TODO: 6/27/16 stringify jsObject correctly
            return jsObject.toString();
        } else if ((arg instanceof Number) || (arg instanceof Character) || (arg instanceof String) ||
                (arg instanceof Collection) || (arg instanceof Map)) {
            return arg.toString();
        } else {
            return "{" + arg.getClass().getName() + "}";
        }
    }

    enum LogLevel {
        INFO, DEBUG, TRACE, WARN, ERROR
    }
}

@@ -713,128 +713,23 @@
 rt j
-dk.nashorn.api.scripting.ScriptObjectMirror;%0A%0Aimport java.util.Arrays;%0Aimport java.util.Collection;%0Aimport java.util.Map
+ava.util.Arrays
 ;%0A%0A@
@@ -1084,13 +1084,8 @@
 ound
-%22 + %22
  '%22 
@@ -1312,32 +1312,32 @@
 umentException(%0A
+
                 
@@ -1457,634 +1457,8 @@
  %7D%0A%0A
-    default String getLogMessage(Object arg) %7B%0A        if (arg == null) %7B%0A            return %22null%22;%0A        %7D%0A        if (arg instanceof ScriptObjectMirror) %7B%0A            ScriptObjectMirror jsObject = (ScriptObjectMirror) arg;%0A            // TODO: 6/27/16 stringify jsObject correctly%0A            return jsObject.toString();%0A        %7D else if ((arg instanceof Number) %7C%7C (arg instanceof Character) %7C%7C (arg instanceof String) %7C%7C%0A                (arg instanceof Collection) %7C%7C (arg instanceof Map)) %7B%0A            return arg.toString();%0A        %7D else %7B%0A            return %22%7B%22 + arg.getClass().getName() + %22%7D%22;%0A        %7D%0A    %7D%0A%0A
     

remove unnecessary default method from LogFunction interface