#include "commoninclude.h"
#include "app.h"
#include "logwrapper.h"
#include "device.h"
#include "fw.h"
#include "bt.h"
#include "camera.h"
#include "settings.h"
#include "remotemmi.h"


#define YL_EXPORTS(name) exports.Set(Napi::String::New(env, #name), Napi::Function::New(env, napi_##name));

/**
 * Here we set nodejs exports for all native code functions declared in the various .cc modules.
 *
 * Requires a naming pattern where all native code functions is prefixed with "napi_" and
 * the exported javascript function is identical except it is without the "napi_" prefix.
  */
Napi::Object Init(Napi::Env env, Napi::Object exports) {
    // sdk
    YL_EXPORTS(Initialize)
    YL_EXPORTS(UnInitialize)
    YL_EXPORTS(ConnectToYealinkApplication)
    YL_EXPORTS(DisconnectFromYealinkApplication)
    YL_EXPORTS(SetSoftphoneReady)
    YL_EXPORTS(IsSoftphoneInFocus)
    YL_EXPORTS(GetVersion)

    // Setup logging.
    YL_EXPORTS(NativeAddonLog)
    YL_EXPORTS(GetNativeAddonLogConfig)

    // device
    YL_EXPORTS(GetSupportedFeatures)
    YL_EXPORTS(IsFeatureSupported)
    YL_EXPORTS(IsRingSupported)
    YL_EXPORTS(SetRing)
    YL_EXPORTS(IsOffHookSupported)
    YL_EXPORTS(SetOffHook)
    YL_EXPORTS(IsMuteSupported)
    YL_EXPORTS(SetMute)
    YL_EXPORTS(IsHoldSupported)
    YL_EXPORTS(SetHold)
    YL_EXPORTS(IsYlHidStdHidSupported)
    YL_EXPORTS(SetHidWorkingState)
    YL_EXPORTS(GetHidWorkingState)
    YL_EXPORTS(SetStdHidEventsFromYealinkDevices)
    YL_EXPORTS(IsStdHidEventsFromYealinkDevices)
    YL_EXPORTS(EnterDialCall);
    YL_EXPORTS(RingBackCall);
    YL_EXPORTS(IncomingCall);
    YL_EXPORTS(OutgoingCall);
    YL_EXPORTS(TerminateCall);
    YL_EXPORTS(AnswerCall);
    YL_EXPORTS(HoldCall);
    YL_EXPORTS(ResumeCall);
    YL_EXPORTS(MuteCall);
    YL_EXPORTS(UnmuteCall);
    YL_EXPORTS(UpdateContact);
    YL_EXPORTS(IsSetDateTimeSupported)
    YL_EXPORTS(SetDateTime)
    YL_EXPORTS(IsUploadRingtoneSupported)
    YL_EXPORTS(UploadRingtone)
    YL_EXPORTS(PlayRingtone)
    YL_EXPORTS(IsBusylightSupported)
    YL_EXPORTS(SetBusylightStatus)
    YL_EXPORTS(GetBusylightStatus)
    YL_EXPORTS(IsEqualizerSupported)
    YL_EXPORTS(GetEqualizerMode)
    YL_EXPORTS(SetEqualizerMode)
    YL_EXPORTS(IsWizardModeSupported)
    YL_EXPORTS(SetWizardMode)
    YL_EXPORTS(GetWizardMode)
    YL_EXPORTS(FactoryReset)
    YL_EXPORTS(RebootDevice)
    YL_EXPORTS(StartExportAudio)
    YL_EXPORTS(StopExportAudio)
    YL_EXPORTS(IsAudioModeSupported)
    YL_EXPORTS(ChangeAudioMode)
    YL_EXPORTS(HasDectInDocked)
    YL_EXPORTS(GetRegionVersion)
    YL_EXPORTS(SetCurrentTalkContact)
    YL_EXPORTS(SetContactInfoList)
    YL_EXPORTS(GetBatteryStatus)
    YL_EXPORTS(GetDeviceImagePath)
    YL_EXPORTS(ExportDevLog)
    YL_EXPORTS(UploadDevLogToCloud)
    YL_EXPORTS(GetESN)

    // settings
    YL_EXPORTS(GetSetting)
    YL_EXPORTS(GetSettings)
    YL_EXPORTS(SetSettings)

    // bluetooth
    YL_EXPORTS(ConnectBTDevice)
    YL_EXPORTS(GetConnectedBTDeviceName)
    YL_EXPORTS(ConnectNewDevice)
    YL_EXPORTS(ConnectPairedDevice)
    YL_EXPORTS(DisconnectBTDevice)
    YL_EXPORTS(DisconnectPairedDevice)
    YL_EXPORTS(GetAutoConnecting)
    YL_EXPORTS(SetAutoConnecting)
    YL_EXPORTS(IsPairingListSupported)
    YL_EXPORTS(GetPairingList)
    YL_EXPORTS(ClearPairingList)
    YL_EXPORTS(SetBTDiscoverable)
    YL_EXPORTS(ScanNewDevices)
    YL_EXPORTS(StopBTScan)
    YL_EXPORTS(ClearPairedDevice)
    YL_EXPORTS(GetSearchDeviceList)

    // firmware
    YL_EXPORTS(GetFirmwareVersion)
    YL_EXPORTS(GetLatestFirmwareInformation)
    YL_EXPORTS(DownloadFirmware)
    YL_EXPORTS(UpdateFirmware)
    YL_EXPORTS(GetFirmwareFilePath)
    YL_EXPORTS(CancelFirmwareDownload)
    YL_EXPORTS(CheckForFirmwareUpdate)

    // remotemmi
    YL_EXPORTS(SetRemoteMmiAction)
    YL_EXPORTS(ReleaseRemoteMmiFocus)
    YL_EXPORTS(GetRemoteMmiFocus)
    YL_EXPORTS(IsRemoteMmiInFocus)
    YL_EXPORTS(GetRemoteMmiTypes)

    // camera
    YL_EXPORTS(CameraIsReady)
    YL_EXPORTS(CameraDirectionCtrl)
    YL_EXPORTS(CameraZoomCtrl)
    YL_EXPORTS(CameraPTZReset)
    YL_EXPORTS(CameraAddPreset)
    YL_EXPORTS(CameraDelPreset)
    YL_EXPORTS(CameraDelAllPreset)
    YL_EXPORTS(CameraGetAllPreset)
    YL_EXPORTS(CameraUpdatePreset)
    YL_EXPORTS(CameraApplyPreset)
    YL_EXPORTS(CameraGetSmartTrackingOptInfo)
    YL_EXPORTS(CameraSetSmartTrackingOpt)
    YL_EXPORTS(CameraGetSamrtTrackingOpt)
    YL_EXPORTS(CameraSetFaceDetection)
    YL_EXPORTS(CameraIsFaceDetectionEnable)
    YL_EXPORTS(CameraIsInIdle)

    try
    {
        _LogWrapper.Config();
    }
    catch (const std::exception &e)
    {
        std::cerr << "error, config log wrapper failed:" << e.what() << std::flush;
    }
    catch (...)
    {
        std::cerr << "error, config log wrapper failed:" << std::flush;
    }

    return exports;
}

NODE_API_MODULE(sdkintegration, Init)
