#include <iostream>
#include <cstdlib>
// #include <nan.h>
#include <string.h>

// #include "dylib.hh"

#include <napi.h>
#include "helloworld.hh"

using std::cout;
using std::cerr;
using std::endl;
using std::string;

#ifdef __APPLE__
#define IS_SUPPORTED_PLATFORM true
    const string SHARED_LIBRARY_EXT = "dylib";
#endif

#if !defined IS_SUPPORTED_PLATFORM
  cerr << "Unsupported Platform";
  exit(EXIT_FAILURE);
#endif


/*
struct Transform {
  char* app_id;
  char* window_id;
  char* transform_type;
  float x;
  float y;
};

typedef void (*TransformCallback)(const struct Transform *data);
*/

/*
typedef void (*TransformCallback)(
        const char* app_id, 
        const char* window_id, 
        const char* transform_type, 
        float x, 
        float y);
typedef void (*RegisterCallbacksFunc)(TransformCallback);
typedef void (*StartWatcherFunc)();
*/

/*
const string SWIFT_SHARED_LIBRARY_PATH = "./SwiftPackage/.build/debug/libSwiftPackage."+SHARED_LIBRARY_EXT;
const auto swiftLib = DLOpenOrDie(SWIFT_SHARED_LIBRARY_PATH);
*/

/*
void transformCallback(const struct Transform *data) {
    cout << "CALLBACK!" << endl;
    cout << data->app_id << " " << data->window_id << " " << data->transform_type << endl;
    cout << data->x << " " << data->y << " " << endl;
}
*/

void transformCallback(
        const char* app_id, 
        const char* window_id, 
        const char* transform_type, 
        float x,
        float y
) {
    cout << "CALLBACK!" << endl;
    cout << app_id << " " << window_id << " " << transform_type << endl;
    cout << x << " " << y << " " << endl;

    if (strncmp(transform_type, "move", 4) == 0) {
        cout << "move" << endl;
    } else if (strncmp(transform_type, "resize", 6) == 0) {
        cout << "resize" << endl;
    } 
}

/*
void RegisterCallbacks() {
    const auto _RegisterCallbacks = (RegisterCallbacksFunc)DLSymOrDie(swiftLib, "register_callbacks");
    _RegisterCallbacks(transformCallback);
}

void StartWatcher() {
    const auto _StartWatcher = (StartWatcherFunc)DLSymOrDie(swiftLib, "start_watcher");
    _StartWatcher();
}
*/

/*
int main() {
  // The while loop isn't needed because the Swift module is essentially an app
  // which is preventing this program from quitting.
  // RegisterCallbacks(transformCallback);
  // StartWatcher();
  
  // SayHello();

  return 0;
}
*/

Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
  return helloworld::Init(env, exports);
}

NODE_API_MODULE(swift_package, InitAll)

