let LVRTCEngineNativePath = "";
let os = require("os");
var isMac = true;
if (os.platform() === "win32") {
    LVRTCEngineNativePath = "./sdk/x86/linkv_engine";
    isMac = false;
} else if (os.platform() === "darwin") {
    LVRTCEngineNativePath = "./sdk/mac/linkv_engine";
} else {
    throw ("Platform not supported")
}

var camereOpened = false;

const RTC = require(LVRTCEngineNativePath);

class LVRTCEngine {

    buildVersion(){
        return RTC.buildVersion();
    }

    versionName(){
        return RTC.versionName();
    }

    versionNumber(){
        return RTC.versionNumber();
    }

    setPublishQualityMonitorCycle(second){
        RTC.setPublishQualityMonitorCycle(second);
    }

    setPlayQualityMonitorCycle(second){
        RTC.setPlayQualityMonitorCycle(second);
    }

    setLogLevel(level){
        RTC.setLogLevel(level);
    }

    setLogLevelWithDir(level, dir){
        RTC.setLogLevel(level, dir);
    }

    setDebugServerIp(ip_port){
        RTC.setDebugServerIp(ip_port);
    }

    setDecoderPixelType(pixelType){
        RTC.setDecoderPixelType(pixelType);
    }

    setEncoderPixelType(pixelType){
        RTC.setEncoderPixelType(pixelType);
    }

    setISOCountryCode(country_code){
        RTC.setISOCountryCode(country_code);
    }

    setUseTestEnv(isTest){
        RTC.setUseTestEnv(isTest);
    }

    auth(appId, appSecret, userId, callback) {
        RTC.auth(appId, appSecret, userId, callback);
    }

    loginRoom(userId, roomId, isHost, isAudioOnly){
        RTC.loginRoom(userId, roomId, isHost, isAudioOnly);
    }

    GetVideoCaptureDevice() {
        return RTC.GetVideoCaptureDevice();
    }

    // [{name:"Default Microphone", id:0, guid:"32893-3293839"}, {name:"Default Microphone", id:0, guid:"32893-3293839"}]
    GetAudioCaptureDevice(){
        return RTC.GetAudioCaptureDevice();
    }

    SetRecordingDevice(id){
        RTC.SetRecordingDevice(id);
    }

    SetBeautyParameter(enable, beautyLevel, brightLevel, toneLevel){
        return RTC.SetBeautyParameter(enable, beautyLevel, brightLevel, toneLevel);
    }

    GetCameraResolution(cameraName) {
        return RTC.GetCameraResolution(cameraName);
    }

    GetCameraColorType(cameraName, width, height) {
        if (os.platform() == "darwin"){
            return ["0"];
        }
        return RTC.GetCameraColorType(cameraName, width, height);
    }

    initCameraCapture(cameraName, colorFormat, width, height) {
        RTC.initCameraCapture(cameraName, colorFormat, width, height);
    }

    startCapture(){
        camereOpened = true;
        RTC.startCapture();
    }

    stopCapture(){
        camereOpened = false;
        RTC.stopCapture();
    }

    startPublishing(){
        RTC.startPublishing();
    }

    stopPublishing(){
        RTC.stopPublishing();
    }

    startPlayingStream(userId) {
        RTC.startPlayingStream(userId);
    }

    stopPlayingStream(userId){
        RTC.stopPlayingStream(userId);
    }

    linkRoom(roomId, userId){
        RTC.linkRoom(roomId, userId);
    }

    unlinkRoom(roomId, userId){
        RTC.unlinkRoom(roomId, userId);
    }

    enableMic(enable){
        RTC.enableMic(enable);
    }

    enableSpeakerphone(enable){
        RTC.enableSpeakerphone(enable);
    }

    setAudioRecordFlag(flag){
        RTC.setAudioRecordFlag(flag);
    }

    startSoundLevelMonitor(timeInMS){
        RTC.startSoundLevelMonitor(timeInMS);
    }

    stopSoundLevelMonitor(){
        RTC.stopSoundLevelMonitor();
    }


    setPlayVolume(volume){
        RTC.setPlayVolume(volume);
    }

    setAVConfig(config){
        if (!isMac){
            RTC.setAVConfig(config);
        }
    }

    setRecorderConfig(presignUrl, token, localDir, uid, callback){
        RTC.setRecorderConfig(presignUrl, token, localDir, uid, callback);
    }

    setUploadCallback(callback) {
        RTC.setUploadCallback(callback);
    }

    logoutRoom(){
        RTC.logoutRoom();
    }

    startRecorder(userId, path, type){
        RTC.startRecorder(userId, path, type);
    }

    stopRecorder(userId){
        return RTC.stopRecorder(userId);
    }

    StartVVorkRecorder(taskId, videoPrefix, imagePrefix, imageInterval, type, useSystemAudio){
        RTC.startRecorder(taskId, videoPrefix, imagePrefix, imageInterval, type, useSystemAudio);
    }

    StopVVorkRecorder(){
        return RTC.stopRecorder();
    }

    PauseVVorkRecorder() {
        RTC.pauseRecorder();
    }

    ResumeVVorkRecorder() {
        RTC.resumeRecorder();
    }

    GetScreenList(){
        return RTC.GetWindowsList(0);
    }

    GetWindowsList(){
        return RTC.GetWindowsList(1);
    }

     // 获取 screenId 下面的所有窗口
    GetWindowsListGroupBy(screenId){
        return RTC.GetWindowsListGroupBy(screenId);
    }

    SnapshotWindows(sourceIdList, type){
        return RTC.SnapshotWindows(sourceIdList, type);
    }

    // 打开和关闭光标采集功能，该开关是全局控制，如需单独控制，需要在下列接口调用之前单独调用
    // 在 InitCapture2、GetWindowsList、GetScreenList、GetWindowsListGroupBy、SnapshotWindows
    SetMouseCursorEnable(enable){
        RTC.SetMouseCursorEnable(enable);
    }

    InitCapture(type) {
        RTC.InitCapture(type);
    }

    // （width, height）指定录制分辨率大小，rect 指定录制选区{x:0, y:0, width:1280, height:720}
    InitCapture2(type, width, height, rect) {
        RTC.InitCapture(type, width, height, rect);
    }

    StartScreenCapture(sourceId, framerate){
        return RTC.StartScreenCapture(sourceId, framerate);
    }

    StopScreenCapture(){
        RTC.StopScreenCapture();
    }

    RemoveVVorkTask(taskId){
        RTC.removeTask(taskId);
    }

    StartAudioRecording(){
        RTC.StartAudioRecording();
    }

    StopAudioRecording(){
        RTC.StopAudioRecording();
    }

    StopAudioDeviceModule(){
        RTC.StopAudioDeviceModule();
    }

    SetExcludedWindow(enable, nativeWindow) {

    }

    SetExcludedWindowEnable(enable){
        RTC.SetExcludedWindowEnable(enable);
    }

    OpenPluginApp(isDebug){
        return RTC.OpenPluginApp(isDebug);
    }

    OnDeviceChange() {
        if (os.platform() != "darwin"){
            RTC.OnDeviceChange();
        }
    }

    RequestScreenCaptureAccess(){
        if (os.platform() != "darwin"){
            return true;
        }
        return RTC.RequestScreenCaptureAccess();
    }
    
    SetUseDefaultResultion(use){
        RTC.SetUseDefaultResultion(use);
    }

    SetUseBlackHole(){
        RTC.SetUseBlackHole();
    }

    UploadLog(){
        RTC.UploadLog();
    }

    SetBgmDeviceEnabled(enable){
        RTC.SetBgmDeviceEnabled(enable);
    }

    GetClipVideo(start, end, clipTaskId){
        return RTC.GetClipVideo(start, end, clipTaskId);
    }

    SetRecorderSourceType(sourceType) {
        RTC.SetRecorderSourceType(sourceType);
    }

    GetAudioPluginDeviceName(){
        return RTC.GetAudioPluginDeviceName();
    }

    SetRecorderAVConfig(short_side, framerate, bitrate){
        RTC.SetRecorderAVConfig(short_side, framerate, bitrate);
    }

    SetScreenScaleFactor(sourceId, scaleX, scaleY){
        if (!isMac) {
            RTC.SetScreenScaleFactor(sourceId, scaleX, scaleY);
        }
    }

    SetOnlyCameraCapture(){
        RTC.SetOnlyCameraCapture();
    }

    SetCameraMetalSpeedupEnable(enable) {
        if (isMac) {
            RTC.SetCameraMetalSpeedupEnable(enable);
        }
    }

    SetRecorderFailedUrl(url) {
        RTC.SetRecorderFailedUrl(url);
    }

    GetMicrophoneVolume(deviceName){
        return RTC.GetMicrophoneVolume(deviceName);
    }

    SetMicrophoneVolume(deviceName, volume){
        RTC.SetMicrophoneVolume(deviceName, volume);
    }

    StopWithoutSystemAudio(){
        if (isMac) {
            RTC.StopWithoutSystemAudio();
        }
    }

    AddImageTicker(){
        RTC.AddImageTicker();
    }

    GetCurrentRunningApplications(){
        if (isMac) {
            return RTC.GetCurrentRunningApplications();
        }
        return [];
    }

    CamereOpened(){
        return camereOpened;
    }

    /**
     * OnEnterRoomComplete : (code, userList)
     * OnRoomReconnected :  ()
     * OnExitRoomComplete : () 
     * OnRoomDisconnected : (code) 
     * OnMixComplete : (code) 
     * OnPublishStateUpdate : (code)
     * AudioMixerCurrentPlayingTime : (time_ms) 
     * AudioMixerPlayerDidFinished : () 
     * OnReceivedFirstVideoFrame : (userId, streamId)
     * OnReceivedFirstAudioFrame : (userId, streamId)
     * OnKickOff : (userId)
     * OnPlayStateUpdate : (state)
     * OnDeleteRemoter : (object)
     * OnAddRemoter : (object)
     * OnAudioVolumeUpdate : (object)
     * OnPublishQualityUpdate : (object)
     * OnPlayQualityUpate : (object)
     * OnCaptureVideoFrame : (frame, width, height)
     * OnDrawFrame : (userId, frame, width, height)
     * OnCaptureScreenVideoFrame (frame, width, height)
     * OnAudioVolume(volume)
     * OnUploading(taskId, percent)
     * OnSystemAudioIsPlaying(isPlaying)
     * OnSystemAudioStateChanged(state)
     * 
     * example:
     * on("OnEnterRoomComplete", function (code, userList){
     * 
     * 
     * });
     * 
    */
    on(event, callback) {
        RTC.on(event, callback);
    }
}

const linkv = new LVRTCEngine;
// navigator.mediaDevices.ondevicechange = function(event) {
//     linkv.OnDeviceChange();
// }
module.exports = linkv;