{"size": 2969, "ext": "js", "max_stars_repo_path": "src/main/ipc.js", "max_stars_repo_name": "hjiachuang/pdf_tool", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/main/ipc.js", "max_issues_repo_name": "hjiachuang/pdf_tool", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/main/ipc.js", "max_forks_repo_name": "hjiachuang/pdf_tool", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.9909090909, "max_line_length": 72, "alphanum_fraction": 0.5537217918}
import path from 'path'
import fs from 'fs'
import { app, ipcMain, dialog, shell } from 'electron'

import Tool from './tool'

export default (win) => {
  ipcMain.on('minimize', () => {
    win.minimize()
  })
  ipcMain.on('maximize', () => {
    if (win.isMaximized()) {
      win.restore()
    } else {
      win.maximize()
    }
  })
  ipcMain.on('close', () => {
    win = null
    app.exit()
  })
  ipcMain.on('merge-select_file_dialog', async (event) => {
    try {
      const result = await dialog.showOpenDialog({
        title: '请选择PDF文件',
        filters: [{
          name: 'PDF File', extensions: ['pdf']
        }],
        properties: ['multiSelections']
      })
      if (result.filePaths.length > 0) {
        event.sender.send('merge-select_file_path', result.filePaths)
      }
    } catch (err) {
      event.sender.send('error-message', err)
    }
  })
  ipcMain.on('select_directory_dialog', async (event, to) => {
    try {
      const result = await dialog.showOpenDialog({
        properties: ['openDirectory']
      })
      if (result.filePaths.length > 0) {
        event.sender.send(to, result.filePaths[0])
      }
    } catch (err) {
      event.sender.send('error-message', err)
    }
  })
  ipcMain.on('open_directory_dialog', async (event, filePath) => {
    try {
      const data = JSON.parse(filePath)
      const url = path.join(data.path, data.name)
      fs.access(url, fs.constants.F_OK, async (err) => {
        if (err) {
          await shell.openPath(data.path)
        } else {
          await shell.showItemInFolder(url)
        }
      })
    } catch (err) {
      event.sender.send('error-message', err)
    }
  })
  ipcMain.on('merge-to_merge', async (event, data) => {
    const tempDate = JSON.parse(data)
    try {
      const filePath = path.join(tempDate.data.path, tempDate.data.name)
      fs.access(filePath, fs.constants.F_OK, async (err) => {
        if (err) {
          await Tool.merge(event, data)
        } else {
          if (tempDate.cover) {
            await Tool.merge(event, data)
          } else {
            event.sender.send('merge-to_merge_result', '输出目录下已有同名文件')
          }
        }
      })
    } catch (err) {
      event.sender.send('error-message', err)
    }
  })
  ipcMain.on('split-select_file_dialog', async (event) => {
    try {
      const result = await dialog.showOpenDialog({
        title: '请选择PDF文件',
        filters: [{
          name: 'PDF File', extensions: ['pdf']
        }],
        properties: ['openFile']
      })
      if (result.filePaths.length > 0) {
        Tool.page(event, result.filePaths[0])
      } else {
        event.sender.send('split-select_file_path', null)
      }
    } catch (err) {
      event.sender.send('error-message', err)
    }
  })
  ipcMain.on('split-drap_file_drop', async (event, path) => {
    console.log(path)
    Tool.page(event, path)
  })
  ipcMain.on('split-send', async (event, data) => {
    Tool.split(event, data)
  })
}
