import "DAC.yjs";
import "union.yjs";
import "ShanxiAuditPRoxy.yjs";
import "ShanxiDataRepo.yjs";
import "ShanxiGlobalRouter.yjs";
import "ShanxiRegistry.yjs";
//网关为Gateway
//路由器改名为Resolver，中文名为标识解析系统
//搜索引擎改名为为Registry，中文名为注册表系统
//本文接口由李智负责对接。
contract ShanxiControlProxy {
    function convertArgToJson(arg) {
        if (typeof(arg)=='string' && !arg.startsWith("{")) return arg;
        if (typeof(arg)=='string') return JSON.parse(arg);
        return arg;
    }

    function onCreate(arg) {
        if (arg==null|| typeof(arg)=='string'){
            arg = {
            "prefix":"test.ab.cd", "router":"GlobalRouter"
            };
        }
        arg = convertArgToJson(arg);
        Global.router = "GlobalRouter";
        Global.prefix = arg.prefix;
        Global.router = arg.router;
        initDAC(requester);
        org.bdware.sc.controlproxy.RepoProxy.init(arg);
    }

    function isString(arg) {
        return typeof(arg)=='string';
    }

    export function getControlProxyConfig() {
        return org.bdware.sc.controlproxy.RepoProxy.getConfig();
    }

    export function isOwner() {
        return checkPermission(requester);
    }
    @Description("参数为，{\"doId\":\"bdware.ss/Repox\"}")
    export function addRepo(arg) {
        arg = convertArgToJson(arg);
        if (arg.doId ==undefined ){
            return {
            "msg":"missing arguments repoId ", "code":1
            };
        }
        executeContract("Gateway", "addLocalRepo", arg);
        return {
        "msg":"success", "rrt":"110ms", "code":0
        };
    }
//网关需新增的接口
    //@Description("参数为，{\"doId\":\"bdware.ss/Repox\"}")
    //export function pingRepo(arg) {
        //arg = convertArgToJson(arg);
        //if (arg.doId ==undefined ){
        //    return {
        //    "msg":"missing arguments repoId ", "code":1
        //    };
        //}
        // var ret = executeContract("Gateway", "pingRepo", arg.doId);
        // ret = ret.result;
        // if (ret.rrt > 0)
        //     return {
        //     "msg":"success", "rrt":ret.rrt, "code":0, "repoInfo":JSON.parse(ret.repoInfo)
        //     }; else return {
        // "msg":"repo is unconnected", "code":1
        // };
        //return org.bdware.sc.controlproxy.RepoProxy.pingRepo(arg.doId);
    //}

    @Description("shanxi/TestLocal shanxi/Repository")
    export function testPingRepo(arg) {
        return org.bdware.sc.controlproxy.RepoProxy.pingRepo(arg);
    }

    @Description("shanxi/TestLocal shanxi/Repository")
    export function testRepoStatistics(arg) {
        return org.bdware.sc.controlproxy.RepoProxy.getRepoStatistics(arg);
    }
//网关的接口
    //@Description("无参数")
    //export function getRepoList(arg) {
    //use configed  arg.routerId;
    //    ret = executeContract(Global.router, "listRepo", "");
        //ret=executeContract("Gateway","listLocalRepo","");
        //var list = ret.result
    //    return ret.result;
    //}
//网关的接口
    @Description("参数为 {\"doId\":\"bdware.ss/Repox\"}")
    export function deleteRepo(arg) {
        arg = convertArgToJson(arg);
        if (arg.doId != null && arg.doId.indexOf(Global.prefix) != -1){
            executeContract("Gateway", "deleteLocalRepo", arg.doId);
            return {
            "status":"success", "code":0
            };
        }
        return {
        "msg":"can not found repo", "code":1
        };
    }
//网关的接口 需新增
    //@Description("参数为 [{\"doId\":\"bdware.ss/Repox\"},{\"doId\":\"bdware.ss/Repoy\"}]")
    //export function deleteRepoList(arg) {
    //    arg = convertArgToJson(arg);
    //    var ret = [];
    //    for (var i = 0;
    //    i<arg.length;
    //    i++){
    //        ret.push(deleteRepo(arg[i]));
    //    }
    //    return ret;
    //}
//网关的接口
    @Description("参数为 {\"doId\":..., \"address\":..., \"owner\":..., }")
    export function updateRepo(arg) {
        arg = convertArgToJson(arg);
        if (arg.doId!=null){
            executeContract("Gateway", "addLocalRepo", arg);
            return {
            "msg":"success", "code":0
            };
        }
        return {
        "msg":"missing arguments", "code":1
        };
    }

    export function getLevelTopology(arg) {
        return org.bdware.sc.controlproxy.TopologyCollector.getLevelTopology(arg/1.0);
    }

    export function getAllTopology(arg) {
       if (arg==null|| arg.length==0) {
           return org.bdware.sc.controlproxy.TopologyCollector.getLevelTopology(2);
       } else return org.bdware.sc.controlproxy.TopologyCollector.getLevelTopology(arg/1.0);
    }
//总体拓扑图
//@范博
    export function setTopologyLevel(arg) {
        Global.level = arg/1.0;
        return Global;
    }
    export function getAllTopology2() {
        if (Global.level==null)
            Global.level = 0;
        return getAllTopology(Global.level);
    // var nodeList = [];
    // nodeList.push({
    // "name":"小店区仓库", "doId":"bdware.ss.ab/Repo1", "type":"Repo", "previousNode":["bdware.ss/Gateway1", "bdware.ss/Gateway2", "bdware.ss.ab/Repo2", "bdware.ss.zz/SearchEngine"]
    // }
    // );
    // nodeList.push({
    // "name":"小山区仓库", "doId":"bdware.ss.ab/Repo2", "type":"Repo", "previousNode":["bdware.ss/Gateway1", "bdware.ss/Gateway2", "bdware.ss.ab/SearchEngine"]
    // }
    // );
    // nodeList.push({
    // "name":"太原内部共享网关", "doId":"bdware.ss/Gateway1", "type":"Gateway", "previousNode":["bdware/Gateway1", "bdware/Gateway2"]
    // }
    // );
    // nodeList.push({
    // "name":"太原对外开放网关", "doId":"bdware.ss/Gateway2", "type":"Gateway", "previousNode":["bdware/Gateway1"]
    // }
    // );
    // nodeList.push({
    // "name":"山西内部共享网关", "doId":"bdware/Gateway1", "type":"Gateway"
    // }
    // );
    // nodeList.push({
    // "name":"山西内部共享网关2", "doId":"bdware/Gateway2", "type":"Gateway"
    // }
    // );
    // nodeList.push({
    // "name":"xxx搜索引擎", "doId":"bdware.ss/SearchEngine", "type":"SearchEngine", "previousNode":["bdware.ss.cc/SearchEngine"]
    // }
    // );
    // nodeList.push({
    // "name":"山西内部共享搜索引擎2", "doId":"bdware.ss.ab/SearchEngine", "type":"SearchEngine", "previousNode":["bdware.ss/SearchEngine"]
    // }
    // );
    // nodeList.push({
    // "name":"yyy搜索引擎", "doId":"bdware.ss.cc/SearchEngine", "type":"SearchEngine"
    // }
    // );
    // nodeList.push({
    // "name":"zzz搜索引擎", "doId":"bdware.ss.zz/SearchEngine", "type":"SearchEngine", "previousNode":["bdware.ss.cc/SearchEngine"]
    // }
    // );
    // nodeList.push({
    // "name":"贵州省域路由器", "doId":"bdware.ss.ab", "type":"Router", "previousNode":["bdware"]
    // }
    // );
    // nodeList.push({
    // "name":"山西省域路由器", "doId":"bdware.ss", "type":"Router", "previousNode":["bdware"]
    // }
    // );
    // nodeList.push({
    // "name":"数瑞测试网", "doId":"bdware", "type":"Router"
    // }
    // );
    // return nodeList;
    }
}
