'use strict';

const Addon = require('ee-core/addon');
const { Controller } = require('ee-core');
const config = require('../config/config.default');
console.log(config);

/**
 * example
 * @class
 */
  class WindowsController extends Controller {

  constructor(ctx) {
    super(ctx);
  }


  async openMain (config) {


    if( this.app.electron[config.id]) return;
    const win = new BrowserWindow({
      ...config,
      webPreferences: {
        webSecurity: false,
        contextIsolation: false, // false -> 可在渲染进程中使用electron的api，true->需要bridge.js(contextBridge)
        nodeIntegration: true,
        preload: path.join('../preload/preload.js','../preload/bridge.js'),

      },
    });
    win.loadURL(config.url); // 设置窗口的 URL
    // 监听窗口关闭事件

    win.webContents.openDevTools(config.openDevTools);
    win.on('close', () => {
      delete this.app.electron[config.id]; // 删除窗口引用
    });
    this.app.electron[config.id] = win ;
  }

  closeWindow(id) {
    if(this.app.electron[id]) this.app.electron[id].close()
  }

  minimizeWindow(id) {
    if(this.app.electron[id]) this.app.electron[id].minimize()
  }
}

WindowsController.toString = () => '[class WindowsController]';
module.exports = WindowsController;
