const {remote, shell, desktopCapturer} = require('electron'); // Renderer process modules
const {screen} = require('electron').remote; // Main process modules

const fs = require('fs');
const os = require('os');
const path = require('path');

// setInterval(screenCapture, 5000);
screenCapture();

function screenCapture() {
    console.log('Testing');
    const thumbSize = determineScreenShot();
    let options = { types: ['window', 'screen'], thumbnailSize: thumbSize };

    desktopCapturer.getSources(options, function(error, sources){
        if(error) return console.log(error.message);

        sources.forEach(function(source){
          console.log(source.name);
            if(source.name === "Amazon.in - Contact Us"){
                const screenShotPath = path.join(os.tmpdir(), 'screenshot.png');

                fs.writeFile(screenShotPath, source.thumbnail.toPNG(), function(err){
                    if(err) return console.log(err.message);

                    shell.openExternal('file://' + screenShotPath);
                    //Sent Data to Server
                });
            }
        });
    });
}

function determineScreenShot(){
    const screenSize = screen.getPrimaryDisplay().workAreaSize;
    const maxDimension = Math.max(screenSize.width, screenSize.height);
    return {
        width: maxDimension * window.devicePixelRatio,
        height: maxDimension * window.devicePixelRatio
    }
}
