// Copyright (C) 2023 The Android Open Source Project
//
// 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.

#include "src/trace_processor/plugins/math_functions/math_functions.h"

#include <cmath>
#include <memory>
#include <vector>

#include "perfetto/base/compiler.h"
#include "perfetto/base/logging.h"
#include "src/trace_processor/core/plugin/plugin.h"
#include "src/trace_processor/perfetto_sql/engine/perfetto_sql_connection.h"
#include "src/trace_processor/sqlite/bindings/sqlite_function.h"
#include "src/trace_processor/sqlite/bindings/sqlite_result.h"
#include "src/trace_processor/sqlite/bindings/sqlite_type.h"
#include "src/trace_processor/sqlite/bindings/sqlite_value.h"
#include "src/trace_processor/sqlite/sqlite_utils.h"

namespace perfetto::trace_processor::math_functions {

namespace {

struct Ln : public sqlite::Function<Ln> {
  static constexpr char kName[] = "__intrinsic_ln";
  static constexpr int kArgCount = 1;

  static void Step(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
    PERFETTO_DCHECK(argc == 1);
    switch (sqlite::value::NumericType(argv[0])) {
      case sqlite::Type::kInteger:
      case sqlite::Type::kFloat: {
        double value = sqlite::value::Double(argv[0]);
        if (value > 0.0) {
          return sqlite::result::Double(ctx, std::log(value));
        }
        break;
      }
      case sqlite::Type::kNull:
      case sqlite::Type::kText:
      case sqlite::Type::kBlob:
        break;
    }
    return sqlite::utils::ReturnNullFromFunction(ctx);
  }
};

struct Exp : public sqlite::Function<Exp> {
  static constexpr char kName[] = "__intrinsic_exp";
  static constexpr int kArgCount = 1;

  static void Step(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
    PERFETTO_DCHECK(argc == 1);
    switch (sqlite::value::NumericType(argv[0])) {
      case sqlite::Type::kInteger:
      case sqlite::Type::kFloat:
        return sqlite::result::Double(ctx,
                                      std::exp(sqlite::value::Double(argv[0])));
      case sqlite::Type::kNull:
      case sqlite::Type::kText:
      case sqlite::Type::kBlob:
        break;
    }
    return sqlite::utils::ReturnNullFromFunction(ctx);
  }
};

struct Sqrt : public sqlite::Function<Sqrt> {
  static constexpr char kName[] = "__intrinsic_sqrt";
  static constexpr int kArgCount = 1;

  static void Step(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
    PERFETTO_DCHECK(argc == 1);
    switch (sqlite::value::NumericType(argv[0])) {
      case sqlite::Type::kInteger:
      case sqlite::Type::kFloat:
        return sqlite::result::Double(
            ctx, std::sqrt(sqlite::value::Double(argv[0])));
      case sqlite::Type::kNull:
      case sqlite::Type::kText:
      case sqlite::Type::kBlob:
        break;
    }
    return sqlite::utils::ReturnNullFromFunction(ctx);
  }
};

class MathFunctionsPlugin : public Plugin<MathFunctionsPlugin> {
 public:
  ~MathFunctionsPlugin() override;
  void RegisterFunctions(PerfettoSqlConnection*,
                         std::vector<FunctionRegistration>& out) override {
    out.push_back(MakeFunctionRegistration<Ln>(nullptr));
    out.push_back(MakeFunctionRegistration<Exp>(nullptr));
    out.push_back(MakeFunctionRegistration<Sqrt>(nullptr));
  }
};
MathFunctionsPlugin::~MathFunctionsPlugin() = default;

}  // namespace

void RegisterPlugin() {
  static PluginRegistration reg(
      []() -> std::unique_ptr<PluginBase> {
        return std::make_unique<MathFunctionsPlugin>();
      },
      MathFunctionsPlugin::kPluginId, MathFunctionsPlugin::kDepIds.data(),
      MathFunctionsPlugin::kDepIds.size());
  base::ignore_result(reg);
}

}  // namespace perfetto::trace_processor::math_functions
