const fs = require('fs');

const args = process.argv.slice(2)[0];
const filesToModify = ['./src/manifest.json', './src/pages.json'];
filesToModify.forEach((file) => {
    fs.readFile(file, 'utf8', function (err, data) {
        if (err) throw err;
        let cleanedData = data.replace(/\/\/.*|\/\*[\s\S]*?\*\//g, '');// 使用正则表达式去除单行注释
        let state = JSON.parse(cleanedData);// 解析JSON数据

        if (file == './src/manifest.json') {
            if (args === 'zhiHuiYun') {
                state.name = '综合智慧云'
                state.appid = '__UNI__36DE3A0'
                state.description = '综合智慧云APP，是一款助力于企业数字化的应用平台，帮助企业提升办公效率，实现组织数字化和业务数字化。'
                state.versionName = "2.2.4"
                state.versionCode = 24
                state.h5.title = '综合智慧云'
            } else if (args === 'xunJian') {
                state.name = '智能巡更'
                state.appid = '__UNI__BF1A1F0'
                state.description = '智能巡更app，是一款用于监督和记录巡逻人员按照预定路线和时间进行巡逻的系统。'
                state.versionName = "2.2.0"
                state.versionCode = 20
                state.h5.title = '智能巡更'
            } else if (args === 'huiYi') {
                state.name = '智能会议'
                state.appid = '__UNI__F3963F8'
                state.description = '智能会议APP，是一款集成了现代信息技术和智能化管理功能的移动应用程序，旨在提升会议体验和管理效率。'
                state.versionName = "2.0.1"
                state.versionCode = 1
                state.h5.title = '智能会议'
            }
        } else if ('./src/pages.json') {
            state.pages.forEach((e, index) => {
                if (args === 'zhiHuiYun') {
                    if (e.style.navigationBarTitleText === "工作台") {
                        if (index != 0) {
                            state.pages.splice(0, 1, ...state.pages.splice(index, 1, state.pages[0]))
                        }
                    }
                    state.condition.list[0].path = "pages/login"
                } else if (args === 'xunJian') {
                    if (e.style.navigationBarTitleText === "工作台") {
                        if (index != 0) {
                            state.pages.splice(0, 1, ...state.pages.splice(index, 1, state.pages[0]))
                        }
                    }
                    state.condition.list[0].path = "pages/login"
                } else if (args === 'huiYi') {
                    if (e.style.navigationBarTitleText === "人脸识别") {
                        if (index != 0) {
                            state.pages.splice(0, 1, ...state.pages.splice(index, 1, state.pages[0]))
                        }
                    }
                    state.condition.list[0].path = "pages/common/face/index"
                }
            })
        }

        // 将修改后的对象转换回JSON字符串
        const updated = JSON.stringify(state, null, 4);

        // 写入新的配置到manifest.json
        fs.writeFile(file, updated, 'utf8', function (err) {
            if (err) throw err;
            console.log(file + ' updated successfully');
        });
    });
})

