/**
 * 打包后运行主函数
 */
const electron = require('electron');
const app = electron.app;
const dialog = electron.dialog;
const ipcMain = electron.ipcMain;
const shell = electron.shell;
const path=require('path');
const os=require('os');
const process=require('process')
const log = require("."+path.sep+"setting"+path.sep+"set_log").clog;
const globalShortcut = electron.globalShortcut;
const contextMenu = require('electron-context-menu');
//加载三个客户端js函数,设置js函数及打印函数
const load_set = require("."+path.sep+"setting"+path.sep+"load_set");
const load_pharmacy=require("."+path.sep+"pharmacy"+path.sep+"load_pharmacy");
const load_doctor=require("."+path.sep+"doctor"+path.sep+"load_doctor");
const load_pharmacist=require("."+path.sep+"pharmacist"+path.sep+"load_pharmacist");

let main ={
  webPath:null,
  createMainWindow:function (configs, apppath,respath,updateHandle) {
    log.info("开始加载窗口...");
    this.addContextMenu(respath);
    log.info("加载右键菜单成功...");
    this.setCacheAndPath(respath,apppath,configs.cache_url);
    log.info(this.webPath)
    if(this.webPath == 'pharmacy'){
      load_pharmacy.createPhamWindow(apppath,respath,this,configs.pharmacy,updateHandle);
    }else if(this.webPath == 'doctor'){
      load_doctor.createDocWindow(apppath,respath,this,configs.doctor,updateHandle);
    }else if(this.webPath=='pharmacist'){
      load_pharmacist.createPhacWindow(apppath,respath,this,configs.pharmacist,updateHandle);
    }else{
      load_set.createSetWindow(apppath,respath,this,configs.lnks_url,updateHandle);
    }
  },
  //设置缓存及访问客户端(非开发模式)
  setCacheAndPath:function(respath,apppath,cpath){
    let params=null;
    if(respath==apppath){
      params = process.argv[2];
    }else{
      params = process.argv[1];
    }
    //统一路径斜杠
    if(os.type()=='Windows_NT'){
      cpath=cpath.replaceAll("/",path.sep);
    }
    log.info("快捷方式传过来的参数【"+process.argv+"】");
    log.info()
    if (params != null) {
      this.webPath = params.substring(0, params.lastIndexOf("--"));
      let cachPath = params.substring(params.lastIndexOf("--") + 2);
      //设置应用程序的缓存路径
      app.setPath("userData", os.homedir()+path.sep+cpath +path.sep+ cachPath);
      log.info("设置缓存目录【"+os.homedir()+path.sep+cpath +path.sep+ cachPath+"】成功");
    }
  },
  //退出软件
  exitSoft: function (respath) {
    dialog.showMessageBox({
      type: 'info',
      title: '退出程序',
      defaultId: 0,
      icon:respath + path.sep+"static"+ path.sep+"images"+ path.sep+"justar.ico",
      message: "确定要退出程序吗？",
      buttons: ['确定', '取消']
    }).then((index) => {
      if (index.response === 0) {
        app.exit();
      }
    });
  },
  //注册开发者工具
  registerDevTool: function (win) {
    shortcut = globalShortcut.register("Ctrl+u", () => {
      win.webContents.openDevTools();
    });
  },
  //任务栏闪烁监听
  taskbarStar:function (win){
    ipcMain.on("taskbar_star", function () {
      win.flashFrame(true);
    });
  },
  //窗口置顶监听
  winTop:function(win){
    ipcMain.on('win_top', function () {
      win.moveTop();
    });
  },
  //添加全局菜单
  addContextMenu: function (respath) {
    //全局上下文菜单模板
    contextMenu({
      menu: () => [],
      prepend: (actions, params, browserWindow, dictionarySuggestions) => [
        {
          label: "刷新",
          icon: respath + path.sep+"static"+path.sep+"images"+path.sep+"refresh.png",
          click: () => {
            browserWindow.reload();
           /* browserWindow.webContents.send('reloadInMain',[]);*/
          }
        },
        {
          label: '关键字搜索',
          icon: respath + path.sep+"static"+ path.sep+"images"+ path.sep+"search.png",
          visible: params.selectionText.trim().length > 0,
          click: () => {
            shell.openExternal(`https://www.baidu.com/s?wd=${encodeURIComponent(params.selectionText)}`)
          }
        },
        {
          label: '剪切(CTRL+X)',
          icon: respath + path.sep+"static"+ path.sep+"images"+ path.sep+"cut.png",
          visible: params.selectionText.length > 0,
          click: () => {
            browserWindow.webContents.cut();
          }
        },
        {
          label: '复制(CTRL+C)',
          icon: respath + path.sep+"static"+ path.sep+"images"+ path.sep+"copy.png",
          visible: params.selectionText.trim().length > 0,
          click: () => {
            browserWindow.webContents.copy();
          }
        },
        {
          label: '粘贴(CTRL+V)',
          icon: respath + path.sep+ "static"+ path.sep+"images"+ path.sep+"paste.png",
          click: () => {
            browserWindow.webContents.paste();
          }
        }, {
          label: '全选(CTRL+A)',
          icon: respath + path.sep+ "static"+ path.sep+"images"+ path.sep+"select_all.png",
          click: () => {
            browserWindow.webContents.selectAll();
          }
        },
        {
          label:'清除缓存',
          icon:respath+ path.sep+"static"+ path.sep+"images"+ path.sep+"clear.png",
          click:()=>{
            browserWindow.webContents.session.clearStorageData();
            browserWindow.reload();
            /*browserWindow.webContents.send('reloadInMain',[]);*/
          }
        },
        {
          label: '退出程序',
          icon: respath + path.sep+ "static"+ path.sep+"images"+ path.sep+"exit.png",
          click: () => {
            this.exitSoft(respath);
          }
        },
        {
          label:'版本号:2.2.3',
          icon: respath + path.sep+ "static"+ path.sep+"images"+ path.sep+"paste.png",
        },]
    });
  }
}


module.exports=main
