import WindowOption from "./WindowOption";

export function createWindow(options: WindowOption): Promise<browser.windows.Window> {
    return browser.windows.create(options);
}

export function getCurrentWindow(): Promise<browser.windows.Window> {
    return browser.windows.getCurrent({populate: true});
}

export function getWindowById(windowId: number):Promise<browser.windows.Window> {
    return browser.windows.get(windowId, {populate: true});
}

export function updateWindowById(windowId: number, options : {drawAttention: boolean,focused: boolean}):Promise<void> {
    return browser.windows.update(windowId, options).then(()=>{});
}

export function getTabById(tabId: number):Promise<browser.tabs.Tab> {
    return browser.tabs.get(tabId);
}

export function removeWindowById(windowId: number): Promise<void> {
    return browser.windows.remove(windowId);
}

export function createTab(options:{windowId:number, index:number, url:string }): Promise<browser.tabs.Tab> {
    return browser.tabs.create(options);
}

export function removeTabs(tabIds:Array<number>): Promise<void> {
    return browser.tabs.remove(tabIds);
}


export function executeTabScript(tabId: number): Promise<boolean> {
    console.log('executre script for id:', tabId);
    return browser.tabs.executeScript(tabId, {file: 'tabScript.js'})
        .then(() => {
            return true;
        })
        .catch( e => {
            return false;
        });
}

export function sendMessageToTab(message={}, messageKind : string, tabId: number): Promise<any> {
    const messageData = Object.assign(message, {kind: messageKind});
    return browser.tabs.sendMessage(tabId, messageData);
}

export function takeScreenshot(): Promise<string> {
    return browser.tabs.captureVisibleTab(this.WindowId, {format : "jpeg",quality : 100});
}


export function captureStreamOnWindow() : Promise<{stream : MediaStream, id: number}> {
    return navigator.mediaDevices.getUserMedia({
        audio: false,
        video: true
    })
    .then((stream)=> {
        return {
            stream,
            id: parseInt(stream.id)
        }
    });
}

