/*
 * This work is licensed unde the MIT License (MIT)
 * Copyright (c) 2020-2021 (overlord)
 * https://opensource.org/licenses/MIT
 */
using System;
using System.Numerics;
using System.Collections.Generic;

using SMath.Manager;
using SMath.Math;

namespace BigIntegerArithmetics.SMathFunctions
{
    /// <summary>
    /// Base 10 logarithm of big integer written as strings.
    /// </summary>
    /// 
    public static class bigLog10
    {
        public static string SMathFunctionName = "bigLog10";

        public static TermInfo GetTermInfo()
        {
            return new TermInfo (
                SMathFunctionName,
                TermType.Function,
                "Logarithm value of \"1:integer\" in base 10 written as strings.",
                FunctionSections.Unknown,
                true,
                new ArgumentInfo(ArgumentSections.String)
               ){ IsUndefinedArgumentsSupported = true };
        }

        public static bool IsSupport(Term root)
        {
            if (root.ArgsCount == 1 && root.Text == SMathFunctionName)
                return true;

            return false;
        }

        public static bool ExpressionEvaluation(Term root, Term[][] args, ref Store store, ref Term[] result)
        {
            Term[]
                arg1 = Computation.Preprocessing(args[0], ref store);

            result = Log10Integers(arg1, store);

            return true;
        }

        public static Term[] Log10Integers(Term[] int1, Store store)
        {
            if (int1.Length > 1 || !int1[0].Text.StartsWith(Symbols.StringChar))
                throw new GeneralException(Errors.ArgumentMustBeString);
            BigInteger num1 = BigInteger.Parse(int1[0].Text.Substring(1, int1[0].Text.Length - 2));
            return new[] {new Term(Symbols.StringChar + BigInteger.Log10(num1).ToString().Replace(",", ".") + Symbols.StringChar, TermType.Operand, 0)};
        }
    }
}