const fs = require('fs');

const path = './swagger.txt';

// 截取两个字符串之间的子字符串,返回所有
function subStringMulti(text, begin, end) {
	let regex;
	if (end == '\\n') regex = new RegExp(begin + '(.+)', 'g');
	else regex = new RegExp(begin + '([\\s\\S]+?)' + end, 'g');
	try {
		let result;
		const blocks = [];
		while ((result = regex.exec(text)) != null) {
			blocks.push(result[1].trim());
		}
		return blocks;
		// return text.match(regex);
	} catch (err) {
		return null;
	}
}

fs.readFile(path, (err, data) => {
	if (err) {
		console.log('文件不存在！');
	} else {
		const oldValue = data.toString();
		let newValue;
		if (oldValue.indexOf('optional') === -1) {
			newValue =
				// 'interface HttpRespone {code: number;msg: string;page: number;pagesize: number;total: number;}' +
				'export interface Name' +
				oldValue
					.replace(/\*/g, '')
					.replace(/number/g, ':number;\n//')
					.replace(/0/g, 'number;\n//')
					.replace(/integer/g, ':number;\n//')
					.replace(/string/g, ':string;\n//')
					.replace(/boolean/g, ':boolean;\n//')
					.replace(/"/g, '')
					.replace(/"/g, '')
					.replace(/\n\/\/\n/g, '//')
					.replace(/\n(\n)*( )*(\n)*\n/g, '\n')
					.trim() +
				'\n';
		} else {
			const before = oldValue.split('{')[0].replace('message', 'export interface ') + '{';
			const between = subStringMulti(oldValue, '{', '}')[0];
			const semicolonAry = between.split(';');
			const newSemicolonAry = [];
			semicolonAry.forEach((item) => {
				if (item.indexOf('optional uint') !== -1) {
					item = item.replace(item.split('=')[1], 'Number').replace('=', '?:');
				} else if (item.indexOf('optional string') !== -1) {
					item = item.replace(item.split('=')[1], 'String').replace('=', '?:');
				} else if (item.indexOf('optional double') !== -1) {
					item = item.replace(item.split('=')[1], 'Number').replace('=', '?:');
				} else if (item.indexOf('optional int32') !== -1) {
					item = item.replace(item.split('=')[1], 'Number').replace('=', '?:');
				} else if (item.indexOf('required int32') !== -1) {
					item = item.replace(item.split('=')[1], 'Number').replace('=', ':');
				} else if (item.indexOf('required uint') !== -1) {
					item = item.replace(item.split('=')[1], 'Number').replace('=', ':');
				} else if (item.indexOf('required string') !== -1) {
					item = item.replace(item.split('=')[1], 'String').replace('=', ':');
				} else if (item.indexOf('required double') !== -1) {
					item = item.replace(item.split('=')[1], 'Number').replace('=', ':');
				}
				newSemicolonAry.push(item + ';');
			});
			const changeValue = newSemicolonAry.join(' ');
			newValue =
				before +
				changeValue
					.replace(/optional MessageHead Header = 1;/g, '')
					.replace(/required/g, 'optional')
					.replace(/optional int32/g, '')
					.replace(/optional uint32/g, '')
					.replace(/optional uint64/g, '')
					.replace(/optional string/g, '')
					.replace(/optional double/g, '')
					.replace(/=/g, ':')
					.trim() +
				'\n }';
		}

		console.log(newValue);
		fs.unlink('./swagger.ts', (err) => {
			!err && console.log('文件删除成功！');
		});
		fs.writeFile('./swagger.ts', newValue, { flag: 'a' }, (err) => {
			!err && console.log('写入文件成功！');
		});
	}
});
