#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call SumIfType(uCalcBase::Callback cb) {
   auto typeName = cb.ArgStr(1);
   double total = 0;
   string totalStr = "";
   auto i = 0;

   // Loop through all arguments starting from the second one.
   for ( i = 2; i <= cb.ArgCount(); i++) {
      auto item = cb.ArgItem(i);
      // Check if the argument's data type name matches.
      if (item.DataType().Name() == "double") {
         total = total + item.Value();
      } else if (item.DataType().Name() == "string") {
         totalStr = totalStr + item.ValueStr();
      }
   }

   if (typeName == "double") {
      totalStr = to_string(total);
   }
   cb.ReturnStr(totalStr);
}
int main() {
   uCalc uc;
   // Define the variadic function.
   uc.DefineFunction("SumIfType(typeName As String, ByHandle other As AnyType ...) As String", SumIfType);

   // This call will sum only the double values (5.0 and 10.123456), ignoring the integer.
   cout << uc.EvalStr("SumIfType('double', 5.0, 'Hello ', 10.123456, 'world!')") << endl;

   // This call will concatinate only the string values.
   cout << uc.EvalStr("SumIfType('string', 5.0, 'Hello ', 10.123456, 'world!')") << endl;
}