//var oneWs = new SockJS("https://"+window.location.hostname+":7443/oneTopic.do");
var oneWs = new SockJS("http://"+window.location.host+"/oneTopic.do");
//var oneWs = new SockJS("http://"+window.location.hostname+":8443/oneTopic.do");

var _CvibNodeArr = [];
var _CvibNode;


oneWs.onopen = function (message) {
    wsOpen(message);
}
oneWs.onmessage = function (message) {
    wsOnMessage(message);
}
oneWs.close = function (message) {
    wsClose(message);
}
oneWs.onerror = function (message) {
    wsError(message);
    console.log("oneTopic.error: " + message)
}

/*
  웹소켓 접속시 nodeid 를 보낸다.
 */
function doClose() {
    console.log("oneTopic.close")
    oneWs.close();
}

function wsOpen(message) {
    console.log("oneTopic.send: " +  nodeId)
    _Level = 1;
    oneWs.send(nodeId);
};

function wsOnMessage(message) {

    console.log("oneTopic.recv")

    var recv = JSON.parse(message.data);

    recv.sort(dirCodeSort);
    console.log(recv);

    var nodeId;
    var date;
    var errCenter;
    var errCont;
    var errScu;
    var oprBlink;
    var oprInd;
    var oprManual;
    var oprTrans;
    var oprTurnoff;
    var dataCount;
    var counter;

    var light = []			// 신호등정보 [직진,좌,보행]
    var dirAdd = [];			//연등지
    var timeFlag = [];			//시간정보신뢰성
    var walker = [];			//보행자
    var unprotected = [];			//비보호 상태
    var stts = [];			//신호등상태
    var dispTm = [];						//표출시간
    var remainTm = [];						//잔여시간
    var dirCode = []; 						//방향코드


    var cvibSttsArr = [];

    for (var i = 10; i <= 80; i = i + 10) {
        cvibSttsArr[i] = '9_9_9_9';
    } // 직진_좌회전_보행_유턴


    for (var ii = 0; ii < recv.length; ii++) {
        if (ii == 0) {
            nodeId = recv[ii].nodeId;
            date = recv[ii].date;
            dataCount = recv[ii].dataCount;
            counter = recv[ii].counter;
            (recv[ii].errCenter == 0) ? errCenter = '정상' : errCenter = '이상';
            (recv[ii].errCont == 0) ? errCont = '정상' : errCont = '이상';
            (recv[ii].errScu == 0) ? errScu = '정상' : errScu = '이상';

            (recv[ii].oprBlink == 0) ? oprBlink = 'OFF' : oprBlink = 'ON';
            (recv[ii].oprTrans == 0) ? oprTrans = 'OFF' : oprTrans = 'ON';
            (recv[ii].oprInd == 0) ? oprInd = 'OFF' : oprInd = 'ON';
            (recv[ii].oprManual == 0) ? oprManual = 'OFF' : oprManual = 'ON';
            (recv[ii].oprTurnoff == 0) ? oprTurnoff = 'OFF' : oprTurnoff = 'ON';

            continue;
        }

        //( recv[ii].light == 2 && recv[ii].stts == 3 && recv[ii].unprotected == 1 ) ? stts.push(1) : stts.push(recv[ii].stts);
        stts.push(recv[ii].stts)
        light.push(recv[ii].light);
        dirAdd.push(recv[ii].dirAdd);
        timeFlag.push(recv[ii].timeFlag);
        walker.push(recv[ii].walker);
        unprotected.push(recv[ii].unprotected);
        dispTm.push(recv[ii].dispTm);
        remainTm.push(recv[ii].remainTm);
        dirCode.push(recv[ii].dirCode);

//        console.log(cvibSttsArr[recv[ii].dirCode]);
        cvibSttsArr[recv[ii].dirCode] = setCvibSttsFnc(recv[ii].unprotected, recv[ii].light, recv[ii].dirCode, recv[ii].stts, cvibSttsArr[recv[ii].dirCode]);
//        console.log(cvibSttsArr);


    }
    // if(_CvibNodeArr.length > 0){
    //     _CvibNode.emptyCvibCircleImg();
    //     _CvibNode.emptyCvibInfoImg();
    // }

    _CvibNode = new CvibInfo(nodeId, nodeName, nodeLat, nodeLng, nodeAddr1,
        nodeAddr2, nodeAddr3, date, errCenter, errCont, errScu, oprBlink, oprInd, oprManual, oprTrans, oprTurnoff, dataCount, counter,
        cvibSttsArr[10], cvibSttsArr[20], cvibSttsArr[30], cvibSttsArr[40], cvibSttsArr[50], cvibSttsArr[60], cvibSttsArr[70], cvibSttsArr[80],
        light, dirAdd, timeFlag, walker, unprotected, stts, dispTm, remainTm, dirCode);

//    console.log(_CvibNode);
    // if (_Level == 0 || _Level == 1 && map != null || map != undefined) {
        _CvibNode.emptyCvibCircleImg();
        _CvibNode.emptyCvibInfoImg();
        _CvibNode.setCircle();
        _CvibNode.setCvibInfo();

    // }
    if (cvibDetailInfoTimeId == null) {
        showCvibDetailInfo();
    }
    if (cvibPopTimeId == null && map == null || map == undefined) {
        drawCvibInfo();
    } else {
        clearTimeout(cvibPopTimeId);
    }
}

function wsClose(message) {
    console.log("oneTopic.close: " + message);
};

function wsError(message) {
    console.log("oneTopic.error" + message);
};

function setCvibSttsFnc(unprotected, light, dirCode, stts, cvibStts) {
    if (light == '1') {
        var temp = cvibStts.substring(1);
        cvibStts = stts + temp;

    } else if (light == '2') {
        var tempS = cvibStts.substring(0, 2);
        var tempE = cvibStts.substring(3);
        //stts =(unprotected == 0) ? stts : 1;
        cvibStts = tempS + stts + tempE;
    } else if (light == '3') {

        var tempS = cvibStts.substring(0, 4);
        var tempE = cvibStts.substring(5);
        cvibStts = tempS + stts + tempE;
    } else if (light == '7') {
        var temp = cvibStts.substring(0, 6);
        cvibStts = temp + stts;
    }
    return cvibStts;

}

function dirCodeSort(a, b) {
    return a.dirCode - b.dirCode;
}