/*
 * @Author: llzq
 * @Date: 2022-05-23 07:47:53
 * @Email: liuzhiqiang1@jd.com
 * @LastEditors: llzq
 * @LastEditTime: 2022-05-25 20:26:48
 * @Description:  头部注释
 */

const path = require('path')
const fs = require('fs-extra')
const handlebars = require('handlebars')
const bakedInHelpers = require('./baked-in-helpers')
Object.keys(bakedInHelpers).forEach(h => handlebars.registerHelper(h, bakedInHelpers[h]))
const apiIndexHbs = path.resolve(__dirname, '../templates/api/index.hbs')
const apiMethodHbs = path.resolve(__dirname, '../templates/api/method.hbs')
const apiConfigIndexHbs = path.resolve(__dirname, '../templates/api/request-config/index.hbs')
const apiConfigHbs = path.resolve(__dirname, '../templates/api/request-config/config.hbs')

module.exports = function (req, res, next) {
    // console.log(req.url)
    if (req.url === '/getFiles') {
        const { fdir } = require('fdir')
        let files = []
        try {
            files = new fdir()
                .crawlWithOptions('domain/admin-b2b/pc/modules/promotion/api', {
                    resolvePaths: true,
                    includeDirs: false,
                    maxDepth: 0
                })
                .sync()
        } catch (error) {}
        const list = Array.from(new Set(files)).filter(it => it !== 'index.ts')
        const data = list.map(it => ({
            groupName: it.split('.')[0],
            filename: it,
            methods: 0
        }))
        res.write(JSON.stringify(data))
        res.end()
    } else if (req.url.startsWith('/add')) {
        const url = require('url')
        const data = url.parse(req.url, true)
        const fileConfig = data.query
        const { fileName } = fileConfig
        fs.ensureDirSync(path.resolve(__dirname, '../domain/admin-b2b/pc/modules/promotion/api'))
        fs.ensureDirSync(
            path.resolve(__dirname, '../domain/admin-b2b/pc/modules/promotion/api/request-config')
        )
        indexPath = path.resolve(__dirname, '../domain/admin-b2b/pc/modules/promotion/api/index.ts')
        methodPath = path.resolve(
            __dirname,
            `../domain/admin-b2b/pc/modules/promotion/api/${fileName}.ts`
        )
        configIndexPath = path.resolve(
            __dirname,
            '../domain/admin-b2b/pc/modules/promotion/api/request-config/index.ts'
        )
        configPath = path.resolve(
            __dirname,
            `../domain/admin-b2b/pc/modules/promotion/api/request-config/${fileName}.ts`
        )
        if (
            fs.existsSync(
                path.resolve(
                    __dirname,
                    `../domain/admin-b2b/pc/modules/promotion/api/${fileName}.ts`
                )
            )
        ) {
            res.write(JSON.stringify({ code: -1 }))
            res.end()
        } else {
            
            const strIndex = handlebars.compile(fs.readFileSync(apiIndexHbs, 'utf8'))(fileConfig)
            const strMethod = handlebars.compile(fs.readFileSync(apiMethodHbs, 'utf8'))(fileConfig)
            const strConfigIndex = handlebars.compile(fs.readFileSync(apiConfigIndexHbs, 'utf8'))(
                fileConfig
            )
            const strConfig = handlebars.compile(fs.readFileSync(apiConfigHbs, 'utf8'))(fileConfig)
            fs.writeFileSync(indexPath, strIndex, 'utf8')
            fs.writeFileSync(methodPath, strMethod, 'utf8')
            fs.writeFileSync(configIndexPath, strConfigIndex, 'utf8')
            fs.writeFileSync(configPath, strConfig, 'utf8')
            res.write(JSON.stringify(fileConfig))
            res.end()
        }
    } else if (req.url === '/append') {
        const url = require('url')
        const data = url.parse(req.url, true)
        const fileConfig = data.query
        const { fileName } = fileConfig
        fs.ensureDirSync(path.resolve(__dirname, '../domain/admin-b2b/pc/modules/promotion/api'))
        fs.ensureDirSync(
            path.resolve(__dirname, '../domain/admin-b2b/pc/modules/promotion/api/request-config')
        )
        indexPath = path.resolve(__dirname, '../domain/admin-b2b/pc/modules/promotion/api/index.ts')
        methodPath = path.resolve(
            __dirname,
            `../domain/admin-b2b/pc/modules/promotion/api/${fileName}.ts`
        )
        configIndexPath = path.resolve(
            __dirname,
            '../domain/admin-b2b/pc/modules/promotion/api/request-config/index.ts'
        )
        configPath = path.resolve(
            __dirname,
            `../domain/admin-b2b/pc/modules/promotion/api/request-config/${fileName}.ts`
        )
        if (
            fs.existsSync(
                path.resolve(
                    __dirname,
                    `../domain/admin-b2b/pc/modules/promotion/api/${fileName}.ts`
                )
            )
        ) {
            res.write(JSON.stringify({ code: -1 }))
            res.end()
        } else {
            const strIndex = handlebars.compile(fs.readFileSync(apiIndexHbs, 'utf8'))(fileConfig)
            const strMethod = handlebars.compile(fs.readFileSync(apiMethodHbs, 'utf8'))(fileConfig)
            const strConfigIndex = handlebars.compile(fs.readFileSync(apiConfigIndexHbs, 'utf8'))(
                fileConfig
            )
            const strConfig = handlebars.compile(fs.readFileSync(apiConfigHbs, 'utf8'))(fileConfig)
            // fs.writeFileSync(indexPath, strIndex, 'utf8')
            // fs.writeFileSync(methodPath, strMethod, 'utf8')
            // fs.writeFileSync(configIndexPath, strConfigIndex, 'utf8')
            // fs.writeFileSync(configPath, strConfig, 'utf8')
            // res.write(JSON.stringify(fileConfig))
            res.end()
        }
        res.write(JSON.stringify({ a: 1 }))
        res.end()
    } else {
        res.end()
    }
}
