// Copyright 2019 The TAL Authors. All rights reserved.
#ifndef LIB_EXTENSION_IMPL_H_
#define LIB_EXTENSION_IMPL_H_

#include <string>
#include <memory>

#include "public/iextension.h"
#include "lib/demo_extension_engine.h"
#include "lib/extension_context.h"

// the extension should provide three function--- Init(),Unint(),CallMethod(),
// these function called by owcr framework.and when js load the library,
// owcr will call Init() to Init some extesion job.When js needs call extension
// function,owcr Calling the CallMethod() transfer the name of the function and
// args that the extension needs to call.when js Unload the library,
// owcr will call Unint() to Unit extension job.
class ExtensionImpl : public owcr::extension::IExtension {
 public:
  ExtensionImpl();
  virtual ~ExtensionImpl() {}
  static IExtension* GetGlobalExtension();
  int Init(owcr::extension::IExtensionManager* manager,
           owcr::extension::IExtensionEventNotify* event_notify) override;
  int Uninit() override;
  ExtensionContext& Context() { return context_; }
  int CallMethod(const char* method_name,
                 int method_name_length,
                 const char* json_args,
                 int json_args_length,
                 owcr::extension::IExtensionMethodCallback* call_back) override;

 private:
  void CallEngineMethodInternal(
      const std::string method,
      const std::string& params,
      owcr::extension::IExtensionMethodCallback* call_back);

  std::unique_ptr<ems::DemoExtensionEngine> demo_engine_;
  ExtensionContext context_;
};

#endif  // LIB_EXTENSION_IMPL_H_
