var cproc = require('child_process');
var execSync = cproc.execSync;
var project_name, project_attr, projects = {},
    java_projects = [],
    node_projects = [],
    python_projects = [],
    ui_projects = [];
var param = {
    type: "",
    name: "",
    attr: ""
}

projects = String(execSync(`curl -s http://127.0.0.1:10088/config_server/get_project_config_from_db`))
projects = JSON.parse(projects)
// console.log(typeof(projects))
// console.log(projects)

for (var index in projects) {
    // if (projects[index]['project_name'] == 'xyqb') {
    //     console.log(index)
    //     console.log(projects[index])
    // }
    switch (projects[index]['type']) {
        case "java":
            java_projects.push(projects[index]);
            break;
        case "node":
            node_projects.push(projects[index]);
            break;
        case "ui":
            ui_projects.push(projects[index]);
            break;
        case "python":
            python_projects.push(projects[index]);
            break;
    }
}
// console.log(111)
// // --java
// new pClass().init(1,true,"java", "7042", "xyqb-user2", "head_group", "xyqb-user2.log", "application.properties", "--worker 1 --currentWorker 1 -t -ts", "-Denv=dev -Didc=default -Dtech.localhost= -Dtest=true", "用户注册登录-V2-server", "", "mvn clean package -Ptest -Dmaven.test.skip=true", "");

var temp = "";

process.argv.forEach(function(val, index, array) {
    if (index >= 2) {
        if (index % 2 == 0) {
            temp = val.replace(/\-/, "");
            param[temp] = "";
        } else {
            param[temp] = val;
        }
    }
});

function getListAttr(projects, attr) {
    var list = [];
    for (var i = 0; i < projects.length; i++) {
        // if (projects[i].is_active) {
        list.push(projects[i][attr])
        // }
    }
    return list.join(" ");
}
var configFunction = function() {};
configFunction.prototype = {
    // getProjectAttr: function(pName, pAttr) {
    // 	if (typeof projects[pName] == "undefined") {
    // 		console.log("111")
    // 	} else {
    // 		console.log("222")

    // 			var attrs = pAttr.split("/");
    // 			if (attrs.length == 0) {
    // 				console.log("")
    // 				return
    // 			}
    // 			var list = [];
    // 			for (var j = 0; j < attrs.length; j++) {
    // 				if(projects[pName][attrs[j]]){
    // 					console.log(projects)
    // 					list.push(projects[pName][attrs[j]].split(" ").join("^"));
    // 				}else{
    // 					list.push(" ");
    // 				}
    // 			}
    // 			console.log(list.join("|"))
    // 	}
    // },
    getProjectAttr: function(pName, pAttr) {
        for (var index in projects) {
            if (projects[index]['project_name'] == pName) {
                var attrs = pAttr.split("/");
                if (attrs.length == 0) {
                    console.log("")
                    return
                }
                var list = [];
                for (var j = 0; j < attrs.length; j++) {
                    if (projects[index][attrs[j]]) {
                        list.push(projects[index][attrs[j]].split(" ").join("^"));
                    } else {
                        list.push(" ");
                    }
                }
                console.log(list.join("|"))
            } else {}
        }
    },
    // getProjectsAttrs: function(projects, attr, attr2) {
    // 	var attrs = attr.split("/");
    // 	if (attrs.length == 0) {
    // 		return
    // 	}
    // 	var list = [];
    // 	//attr2 专有参数，用来表示是否要取带有对外域名的服务
    // 	if(attr2 == "true"){
    // 		for (var i = 0; i < projects.length; i++) {
    // 			if(projects[i].url != ""){
    // 				var li = [projects[i]["name"]];
    // 				for (var j = 0; j < attrs.length; j++) {
    // 					li.push(projects[i][attrs[j]]);
    // 				}
    // 				// li.push(projects[i].url);
    // 				list.push(li.join("|"));
    // 			}
    // 		}
    // 	}else{
    // 		for (var i = 0; i < projects.length; i++) {
    // 			// if (projects[i].is_active){
    // 				var li = [projects[i]["name"]];
    // 				for (var j = 0; j < attrs.length; j++) {
    // 					li.push(projects[i][attrs[j]]);
    // 				}
    // 				list.push(li.join("|"));
    // 			// }
    // 		}
    // 	}
    // 	return list.join(" ");
    // },
    getProjectsAttrs: function(projects, attr, attr2) {
        var attrs = attr.split("/");
        if (attrs.length == 0) {
            return
        }
        var list = [];
        //attr2 专有参数，用来表示是否要取带有对外域名的服务
        if (attr2 == "true") {
            for (var i = 0; i < projects.length; i++) {
                if (projects[i].url != "") {
                    var li = [projects[i]["project_name"]];
                    for (var j = 0; j < attrs.length; j++) {
                        li.push(projects[i][attrs[j]]);
                    }
                    // li.push(projects[i].url);
                    list.push(li.join("|"));
                }
            }
        } else {
            for (var i = 0; i < projects.length; i++) {
                // if (projects[i].is_active){
                var li = [projects[i]["project_name"]];
                for (var j = 0; j < attrs.length; j++) {
                    li.push(projects[i][attrs[j]]);
                }
                list.push(li.join("|"));
                // }
            }
        }
        return list.join(" ");
    },
    getProjectsAttr: function(type, attr) {
        switch (type) {
            case "java":
                console.log(getListAttr(java_projects, attr));
                break;
            case "ui":
                console.log(getListAttr(ui_projects, attr));
                break;
            case "python":
                console.log(getListAttr(python_projects, attr));
                break;
            case "node":
                console.log(getListAttr(node_projects, attr));
                break;
        }
    }
}

//tower add
module.exports = {
    java: java_projects,
    node: node_projects,
    python: python_projects,
    ui: ui_projects
}

var F = new configFunction();

if (param["type"] == "") {
    F.getProjectAttr(param["name"], param["attr"]);
} else if (param["type"] == "multi") {
    var str = "";
    str += F.getProjectsAttrs(java_projects, param["attr"], param["attr2"]);
    str += " " + F.getProjectsAttrs(ui_projects, param["attr"], param["attr2"]);
    str += " " + F.getProjectsAttrs(python_projects, param["attr"], param["attr2"]);
    str += " " + F.getProjectsAttrs(node_projects, param["attr"], param["attr2"]);
    console.log(str)
} else if (param["type"] == "hosts") {
    console.log(JSON.stringify(hosts));
} else {
    F.getProjectsAttr(param["type"], param["attr"]);
}