import { Addon as VirtualCamera } from "./addon";
import { exec } from '@vscode/sudo-prompt';
import { relative, resolve } from "path";
import { red } from "@tosee/color";

export enum CameraType {
    RGBA,
    I420
}

/**
 * 创建虚拟摄像头
 * @param type 虚拟摄像头输入图片的类型
 * @param width 虚拟摄像头的宽
 * @param height 虚拟摄像头的高
 * @returns 
 */
export function createVirturalCamera(type: CameraType, width: number, height: number) {
    if (process.platform === 'win32') {
        return VirtualCamera.createVirturalCamera(type, width, height);
    } else {
        console.log(red`virtualcamera is only support windows now`);
    }
}

/**
 * 推送数据到虚拟摄像头
 * @param data 输入图片的buffer
 * @param width 输入图片的宽
 * @param height 输入图片的高
 * @returns 
 */
export function pushDataToVirturalCamera(data: Buffer, width: number, height: number) {
    if (process.platform === 'win32') {
        return VirtualCamera.pushDataToVirturalCamera(data, width, height);
    } else {
        console.log(red`virtualcamera is only support windows now`);
    }
}

/**
 * 重新设置虚拟摄像头的分辨率
 * @param width 虚拟摄像头的宽
 * @param height 虚拟摄像头的高
 * @returns 
 */
export function changeResolutionOfVirturalCamera(width: number, height: number) {
    if (process.platform === 'win32') {
        return VirtualCamera.changeResolutionOfVirturalCamera(width, height);
    } else {
        console.log(red`virtualcamera is only support windows now`);
    }
}

/**
 * 注册虚拟摄像头
 * @returns 
 */
export async function registeVirturalCamera(): Promise<void> {
    if (process.platform === 'win32') {
        const x86 = relative('.', resolve(__dirname, '../libraries/x86/obs-virtualsource.dll'));
        const x64 = relative('.', resolve(__dirname, '../libraries/x64/obs-virtualsource.dll'));
        return new Promise((resolve, reject) => {
            exec(`regsvr32 /u ${x86} /s
        regsvr32 /u ${x64} /s
        regsvr32 ${x86} /s
        regsvr32 ${x64} /s`, {
                name: "Registe Virtual Camera",
            }, (err) => {
                if (err) {
                    reject(err);
                } else {
                    resolve();
                }
            });
        })
    } else {
        console.log(red`virtualcamera is only support windows now`);
    }
}

/**
 * 卸载虚拟摄像头
 * @returns 
 */
export async function unregisteVirturalCamera(): Promise<void> {
    if (process.platform === 'win32') {
        const x86 = relative('.', resolve(__dirname, '../libraries/x86/obs-virtualsource.dll'));
        const x64 = relative('.', resolve(__dirname, '../libraries/x64/obs-virtualsource.dll'));
        return new Promise((resolve, reject) => {
            exec(`regsvr32 /u ${x86} /s
        regsvr32 /u ${x64} /s`, {
                name: "Uninstall Virtual Camera",
            }, (err) => {
                if (err) {
                    reject(err);
                } else {
                    resolve();
                }
            });
        })
    } else {
        console.log(red`virtualcamera is only support windows now`);
    }
}