/*
 * Copyright(c) 2023 GiR-Zippo
 * Licensed under the GPL v3 license. See https://github.com/BardMusicPlayer/BardMusicPlayer/blob/develop/LICENSE for full license information.
 */

namespace BardMusicPlayer.Script.BasicSharp;

internal abstract class BuiltIns
{
    public static void InstallAll(Interpreter interpreter)
    {
        interpreter.AddFunction("str", Str);
        interpreter.AddFunction("num", Num);
        interpreter.AddFunction("abs", Abs);
        interpreter.AddFunction("min", Min);
        interpreter.AddFunction("max", Max);
        interpreter.AddFunction("not", Not);
    }

    public static Value Str(Interpreter interpreter, List<Value> args)
    {
        if (args.Count < 1)
            throw new ArgumentException();

        return args[0].Convert(ValueType.String);
    }

    public static Value Num(Interpreter interpreter, List<Value> args)
    {
        if (args.Count < 1)
            throw new ArgumentException();

        return args[0].Convert(ValueType.Real);
    }

    public static Value Abs(Interpreter interpreter, List<Value> args)
    {
        if (args.Count < 1)
            throw new ArgumentException();

        return new Value(Math.Abs(args[0].Real));
    }

    public static Value Min(Interpreter interpreter, List<Value> args)
    {
        if (args.Count < 2)
            throw new ArgumentException();

        return new Value(Math.Min(args[0].Real, args[1].Real));
    }

    public static Value Max(Interpreter interpreter, List<Value> args)
    {
        if (args.Count < 1)
            throw new ArgumentException();

        return new Value(Math.Max(args[0].Real, args[1].Real));
    }

    public static Value Not(Interpreter interpreter, List<Value> args)
    {
        if (args.Count < 1)
            throw new ArgumentException();

        return new Value(args[0].Real == 0 ? 1 : 0);
    }
}