/*
 * Copyright (c) 2016 Samuel Marchildon-Lavoie
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package io.github.djxy.multiscripts.scripts.lua;

import io.github.djxy.multiscripts.objects.ArrayObject;
import io.github.djxy.multiscripts.objects.FunctionObject;
import io.github.djxy.multiscripts.objects.MapObject;
import io.github.djxy.multiscripts.objects.Value;
import io.github.djxy.multiscripts.objects.values.BooleanValue;
import io.github.djxy.multiscripts.objects.values.NumberValue;
import io.github.djxy.multiscripts.objects.values.StringValue;
import io.github.djxy.multiscripts.scripts.ValueContainer;
import org.luaj.vm2.LuaBoolean;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;

/**
 * Created by samuelmarchildon-lavoie on 16-11-07.
 */
public class Util {

    public static Value[] toValue(Varargs varargs){
        if(varargs.narg() == 0)
            return new Value[0];

        Value values[] = new Value[varargs.narg()];

        for(int i = 0; i < varargs.narg(); i++)
            values[i] = toValue(varargs.arg(i+1));

        return values;
    }

    public static Value[] toValue(LuaValue... args){
        Value values[] = new Value[args.length];

        for(int i = 0; i < args.length; i++)
            values[i] = toValue(args[i]);

        return values;
    }

    public static Value toValue(LuaValue luaValue){
        if(luaValue == null)
            return Value.VOID;

        if(luaValue instanceof ValueContainer)
            return ((ValueContainer) luaValue).getValueObject();

        if(luaValue.isnumber())
            return NumberValue.valueOf(luaValue.checkdouble());

        if(luaValue.isboolean())
            return BooleanValue.valueOf(luaValue.checkboolean());

        if(luaValue.isstring())
            return StringValue.valueOf(luaValue.checkjstring());

        if(luaValue.isfunction())
            return new LuaFunctionObject(luaValue);

        if(luaValue.istable())
            return new LuaArrayObject(luaValue);

        return Value.VOID;
    }

    public static LuaValue toLuaValue(Value value){
        if(value == null)
            return LuaValue.NIL;

        if(value.isBoolean())
            return LuaBoolean.valueOf((Boolean) value.get());

        if(value.isString())
            return new LuaStringValue(value);

        if(value.isNumber())
            return new LuaNumberValue(value);

        if(value.isFunction())
            return new LuaFunction(value);

        if(value.isArray())
            return new LuaArray(value);

        if(value.isMap())
            return new LuaMap(value);

        return LuaValue.NIL;
    }

    public static LuaValue[] toLuaValue(Value... args){
        LuaValue values[] = new LuaValue[args.length];

        for(int i = 0; i < args.length; i++)
            values[i] = toLuaValue(args[i]);

        return values;
    }

}
