var request = require('request');

function requestPromise(option) {
	return new Promise(function (resolve, reject) {
		request(option, function (err, res, body) {
			if (err) {
				reject(err)
			} else {
				resolve(body)
			}
		})
	})
}


var getattr = function (pro) {
	this.url = `http://192.168.4.3:10088/config_server/get_project_attr/${pro}`;
	this.method = "GET"
}
var getpros = function () {
	this.url = `http://192.168.4.3:10088/config_server/get_systems_by_type_for_temp`;
	this.method = "GET"
}

let save_pod = function (body) {
	this.url = `http://192.168.28.170:3002/proconfig/server/save`;
	this.method = "POST"
	this.body = body;
	this.json = true;
}


function dosomething(ty, rs) {
	for (let i of rs) {
		console.log(i)
		let _arrt_o = new getattr(i)
		requestPromise(_arrt_o).then(res => {
			let attr = JSON.parse(res);
			let body =
				{
					port: attr.port,
					is_active: true,
					type: ty,
					git_path: attr.gitPath,

					host_name: attr.host,
					log_path: attr.logName,
					config_path: attr.configPath,
					des: attr.projectDesc,
					auth: attr.auth,
					jar_path: "",
					command1: "",
					command2: "",
					command3: "",
					command4: "",
					command5: "",
					build_command: "",
					start_command: "",
					stop_command: ""
				}
			// "port": attr.port,
			// 					"is_active": true,
			// 					"type": ty,
			// 					"port":attr.port,
			// 					"full_name": attr.projectDesc,
			// 					"git_path": attr.gitPath,
			// 					"host_name": attr.host,
			// 					"log_path": attr.logName,
			// 					"config_path": attr.configPath,
			// 					"des": attr.projectDesc,
			// 					"auth": attr.auth,
			// 					"jar_path": "",
			// 					"command1": "",
			// 					"command2": "",
			// 					"command3": "",
			// 					"command4": "",
			// 					"command5": "",
			// 					"build_command": "",
			// 					"start_command": "",
			// 					"stop_command": ""}
			if (ty == "java") {
				body.jar_path = attr.jarFilePath;
				body.command1 = attr.node_version;
				body.build_command = attr.buildCmd;
				body.start_command = attr.command2;
			} else if (ty == "ui") {
				body.build_command = attr.buildCmd;
			} else if (ty == "node") {
				body.start_command = attr.buildCmd;
				body.stop_command = attr.command2;
			} else if (ty == "python") {

			}

			let save_pod_o = new save_pod(
				body
			)
			console.log(body)
			requestPromise(save_pod_o)
		});


	}
}




function main() {
	let _o = new getpros();
	console.log("-----")
	requestPromise(_o).then(res => {
		// console.log(res)
		let rr = JSON.parse(res);
		let ks = Object.keys(rr);
		console.log(ks)
		for (let i of ks) {
			dosomething(i, rr[i])
		}
	});

}

main()
