const {
    app,
    BrowserWindow,
    Menu,
    dialog,
    ipcMain
} = require('electron');

//alwaysOnTop

var win = null;

var pot = {};
var s = null;

ipcMain
.on('close_pot',(event,arg)=>{
    console.log('CLOSE')
    pot.close(event);
})
.on('pot_run',(event,arg)=>{
    pot_open_ev.sender.send('pot_play',arg);
})

.on('pot_real_open',(event,arg)=>{
    console.log('POT EVENT SET')
    pot_open_ev = event;
})
.on('open-pot',(event,arg)=>{

    pot.open(event);
})

pot.isOpen = false;

pot.open = (e) => {
    s = e;
    if (win !== null) {
        e.sender.send('play-in-Window',{do :false});
        win.close();
    }
    var x = new playerOnTop(e);
}
pot.close = ()=>{
    console.log('CLOSE')
    s.sender.send('play-in-Window',{do :false});
    win.close();
}


class playerOnTop {
    constructor(e) {
        pot.isOpen = true;
        this.win = new BrowserWindow({
            width: 320,
            height: 180,
            show: false,
            frame: false,
            alwaysOnTop: true,
            icon: global.dir + '/app.ico'
        })
        e.sender.send('play-in-Window',{do :true});
        win = this.win;
        win.loadURL(`file://${global.dir}/app/view/pot.html`)
        win.once('ready-to-show', () => {
            win.show();
        })
        win.on('resize', function () {
          setTimeout(function () {
            var size = win.getSize();
            win.setSize(size[0], parseInt(size[0] * 9 / 16));
          }, 0);
        });
        win.on('closed', () => {
            pot.isOpen = false;
            e.sender.send('play-in-Window',{do :false});
            win = null;
        });
    }
}

module.exports = pot;
