const {app, Menu, shell, dialog} = require('electron')
const {stat, readdir} = require("fs/promises");
const isMac = process.platform === 'darwin'
exports.menuRebuild = (mainWindow) => {
    return template = [
        // { role: 'appMenu' }
        ...(isMac
            ? [{
                label: app.name,
                submenu: [
                    {role: 'about'},
                    {type: 'separator'},
                    {role: 'services'},
                    {type: 'separator'},
                    {role: 'hide'},
                    {role: 'hideOthers'},
                    {role: 'unhide'},
                    {type: 'separator'},
                    {role: 'quit'}
                ]
            }]
            : []),
        // { role: 'fileMenu' }
        {
            label: '文件',
            submenu: [
                {
                    label: '打开目录',
                    click: async () => {
                        const {dialog} = require('electron')
                        dialog.showOpenDialog({
                            properties: ['openDirectory']
                        }).then(async result => {
                            // 不取消就发送目录
                            if (!result.canceled) {
                                console.log('result.filePaths', result.filePaths)
                                const {readdir, stat} = require('fs/promises')
                                try {
                                    const files = await readdir(result.filePaths[0]);
                                    const fileStateList = []
                                    const fileChildList = []
                                    fileStateList.push({
                                        "fileName": result.filePaths[0],
                                        "filePath": result.filePaths[0],
                                        "dirFlag": true,
                                        "childList": fileChildList
                                    })
                                    for (let i = 0; i < files.length; i++) {
                                        const state = await stat(result.filePaths[0] + '/' + files[i]);
                                        if (state.isDirectory()
                                            ||files[i].endsWith(".md")
                                            ||files[i].endsWith(".html")
                                            ||files[i].endsWith(".lexical")){
                                            fileChildList.push({
                                                'fileName': files[i],
                                                "filePath": result.filePaths[0]+ '/' +files[i],
                                                'dirFlag': state.isDirectory()
                                            })
                                        }
                                    }
                                    mainWindow.webContents.send('openDirectory', fileStateList)
                                } catch (err) {
                                    console.error(err);
                                }
                            }
                        }).catch(err => {
                            console.log(err)
                        })
                    }
                },
                {
                    label: '保存文件',
                    accelerator: 'Ctrl+S',
                    click: () => { // console.log('Electron rocks!')
                    }
                },
                {
                    label: '全部应用',
                    click: () => {
                        mainWindow.webContents.send('redirectUrl', '/GateWay')
                    }
                },
                isMac ? {role: 'close'} : {role: 'quit'},
            ]
        },
        // { role: 'editMenu' }
        {
            label: 'Edit',
            submenu: [
                {role: 'undo'},
                {role: 'redo'},
                {type: 'separator'},// 分割线
                {role: 'cut'},
                {role: 'copy'},
                {role: 'paste'},
                ...(isMac
                    ? [
                        {role: 'pasteAndMatchStyle'},
                        {role: 'delete'},
                        {role: 'selectAll'},
                        {type: 'separator'},
                        {
                            label: 'Speech',
                            submenu: [
                                {role: 'startSpeaking'},
                                {role: 'stopSpeaking'}
                            ]
                        }
                    ]
                    : [
                        {role: 'delete'},
                        {type: 'separator'},
                        {role: 'selectAll'}
                    ])
            ]
        },
        // { role: 'viewMenu' }
        {
            label: 'View',
            submenu: [
                {label: '界面布局'},
                {role: 'reload'},
                {role: 'forceReload'},
                {role: 'toggleDevTools'},
                {type: 'separator'},
                {role: 'resetZoom'},
                {role: 'zoomIn'},
                {role: 'zoomOut'},
                {type: 'separator'},
                {role: 'togglefullscreen'}
            ]
        },
        // { role: 'windowMenu' }
        {
            label: 'Window',
            submenu: [
                {role: 'minimize'},
                {role: 'zoom'},
                ...(isMac
                    ? [
                        {type: 'separator'},
                        {role: 'front'},
                        {type: 'separator'},
                        {role: 'window'}
                    ]
                    : [
                        {role: 'close'}
                    ])
            ]
        },
        {
            role: 'help',
            submenu: [
                {
                    label: 'Learn More',
                    click: async () => {
                        const {shell} = require('electron')
                        await shell.openExternal('http://www.huaruyu.com')
                    }
                }
            ]
        }
    ];
}