const path = require('path');
global.dir = path.join(__dirname);
const {
    app,
    BrowserWindow,
    Menu,
    dialog,
    ipcMain
}         = require('electron');
const fs  = require('fs');
const yt_dl = require('./controller/youtube-dl')
const dl   = require('./controller/download');
const browser = require('./controller/windows.js/browser');



if (!fs.existsSync(global.dir+'/tmp/inst')) {
    fs.writeFileSync(global.dir+'/tmp/inst',"out");
    var win;
    const createWindow = () =>{

        win = new BrowserWindow({
            width: 320,
            height: 500,
            show: false,
            frame: false,
            icon: __dirname + '/app.ico'
        })
        win.once('ready-to-show', () => {
        })

        win.on('closed', () => {
          win = null;
        });
    }
    app.on('ready', createWindow);
    app.on('window-all-closed', () => {
      if (process.platform !== 'darwin') {
        app.quit();
      }
    });
}else{
    var win ;


    app.getPath('documents')
    const createWindow = () =>{

        //dl.download(dl.ff(process.platform));
        /*

        win = new BrowserWindow({
            width: 1010,
            height: 800,
            minWidth: 1010,
            minHeight: 565,
            show: false,
            frame: true
        })
        */
        win = new BrowserWindow({
            width: 320,
            height: 500,
            show: false,
            frame: false,
            icon: __dirname + '/app.ico'
        })
        ipcMain
        .on('start-full', (event, arg)=>{

            var win2 = new BrowserWindow({
                width: 1010,
                height: 800,
                minWidth: 1010,
                minHeight: 565,
                show: true,
                frame: true,
                icon: __dirname + '/app.ico'
            })
            win.close();
            //var z = new browser('http://www.crunchyroll.com/');
            win2.loadURL(`file://${__dirname}/app/view/layout.html`)
            win = win2;
        });
        win.loadURL(`file://${__dirname}/app/view/init.html`)
        win.once('ready-to-show', () => {
            win.show()
            //var x = new yt_dl("https://www.youtube.com/watch?v=UbQgXeY_zi4")
            //x.download();
        })

        win.on('closed', () => {
          // Dereference the window object, usually you would store windows
          // in an array if your app supports multi windows, this is the time
          // when you should delete the corresponding element.
          win = null;
        });
    }
    app.on('ready', createWindow);
    app.on('window-all-closed', () => {
      // On OS X it is common for applications and their menu bar
      // to stay active until the user quits explicitly with Cmd + Q
      if (process.platform !== 'darwin') {
        app.quit();
      }
    });
    app.on('activate', () => {
      // On OS X it's common to re-create a window in the app when the
      // dock icon is clicked and there are no other windows open.
      if (mainWindow === null) {
        createWindow();
      }
    });
}

module.exports = app;
