const electron = require('electron')
const app = electron.app;
const Menu = electron.Menu;
const BrowserWindow = electron.BrowserWindow;
function menuTpl( mainWindow ,appIcon){
	return template = {
		contextMenu : Menu.buildFromTemplate([
		      // {
		      //       type: 'separator'
		      // },
		      {
		            label: '打开阿米协同',
		            click : function(){
		                  if( process.platform == 'darwin' ){
		                        app.emit('activate')
		                  }
		                  mainWindow.show();
		                  mainWindow.focus();
		            }
		      },
		      {
		            label: '退出阿米协同',
		            click : function(){
		                  app.exit();
		            }
		      }
		]),
		MenuItems : [
		      {
			      label: 'FromScratch',
			      submenu: [
				     {
					   label: '关于阿米协同',
					   role: 'about'
				  },
				  {
					  type: 'separator'
				  },
				  {
					  label: '隐藏阿米协同',
					  accelerator: 'CmdOrCtrl+H',
					  click: function() {
						 app.hide();
					  }
				  },
				  {
					  label: '隐藏其它应用',
					  accelerator: 'Command+Alt+H',
					  role: 'hideothers'
				  }, {
					  label: '显示全部',
					  role: 'unhide'
				  },
				  {
					  type: 'separator'
				  },
				  {
					  label: '退出阿米协同',
					  accelerator: 'CmdOrCtrl+Q',
					  click: function() {
						  app.exit();
					  }
				  }]
			  },
			  {
				  label: '编辑',
				  submenu: [{
					  label: '撤消',
					  accelerator: 'CmdOrCtrl+Z',
					  selector: 'undo:'
				  },
				  {
					  label: '重做',
					  accelerator: 'Shift+CmdOrCtrl+Z',
					  selector: 'redo:'
				  },
				  {
					  type: 'separator'
				  },
				  {
					  label: '剪切',
					  accelerator: 'CmdOrCtrl+X',
					  selector: 'cut:'
				  },
				  {
					  label: '复制',
					  accelerator: 'CmdOrCtrl+C',
					  selector: 'copy:'
				  },
				  {
					  label: '粘贴',
					  accelerator: 'CmdOrCtrl+V',
					  selector: 'paste:'
				  },
				  {
					  label: '全选',
					  accelerator: 'CmdOrCtrl+A',
					  selector: 'selectAll:'
				  }]
			  },
			  {
				  label: '窗口',
				  submenu: [{
					  label: '最小化',
					  accelerator: 'CmdOrCtrl+M',
					  click: function() { mainWindow.minimize(); }
				  },
				  {
					  label: '关闭窗口',
					  accelerator: 'CmdOrCtrl+W',
					  click: function() {
						  mainWindow.hide();
					  }
				  },
				  {
					  type: 'separator'
				  },
				  {
					  label: '前置全部窗口',
					  role: 'front'
				  },
				  {
					  label: '清除缓存并重新载入',
					//   accelerator: 'CmdOrCtrl+R',
					  click: function (item, focusedWindow) {
						  app.dock.setBadge('');
						  appIcon.setTitle('');
						  if (focusedWindow) {
							  // 重载之后, 刷新并关闭所有的次要窗体
							  if (focusedWindow.id === 1) {
								  BrowserWindow.getAllWindows().forEach(function (win) {
									  if (win.id > 1) {
										  win.close()
									  }
								  })
							  }
							// focusedWindow.reload()
							focusedWindow.webContents.reloadIgnoringCache();
						  }
					  }
				  }]
			  }
		  ]
	}
}
exports.menuTemp = menuTpl;
