const project = require('../../project')
const { TPL_HOMEPAGE, TPL_SOURCE_GIT } = require('../../constants')

const OLD_NAME = 'VVRNBaseModule'

const updateResources = async projectName => {
    const list = [ // [ <file path>, <action array> ]
        ['package.json', [
            { replace: 'VVRNBusinessModule', to: projectName },
        ]],

        // 配置生成podSpec
        [`${OLD_NAME}.podspec`, [
            { replace: OLD_NAME, to: projectName },
            { replace: 'http://homepage', to: TPL_HOMEPAGE.replace('{name}', projectName) },
            { replace: 'git@:url//git', to: TPL_SOURCE_GIT.replace('{name}', projectName) },
            { replace: `s.dependency '${projectName}'`, to: `s.dependency 'VVRNBaseModule'` },
            { moveTo: `${projectName}.podspec` },
        ]],

        // 更新podFile文件
        ['ios/Podfile', [
            { replace: OLD_NAME, to: projectName },
        ]],

        // 更新OC源代码
        [`Release_ios/Classes/VVRNBusinessViewController.h`, [
            { replace: 'VVRNBusinessViewController', to: `${projectName}ViewController` },
            { moveTo: `Release_ios/Classes/${projectName}ViewController.h` },
        ]],
        [`Release_ios/Classes/VVRNBusinessViewController.m`, [
            { replace: 'VVRNBusinessViewController', to: `${projectName}ViewController` },
            { replace: 'VVRNBusinessModule', to: projectName },
            { moveTo: `Release_ios/Classes/${projectName}ViewController.m` },
        ]],

        // 更新iOS push.sh 脚本
        ['Release_ios/push.sh', [
            { replace: 'VVRNBaseModule', to: projectName },
        ]],
    ]

    for (let i = 0; i < list.length; i++) {
        const [source, actions] = list[i]
        await project.fs.batch(source, actions)
    }
}

module.exports = { updateResources }