var Vcss2d_show = 1 ;  // 0 不显示 1 圆点    2显示
var T_sn_list = []  //订阅 SN
$(window).load(function () {
    $("#v_modes_2").hide();
    $("#v_modes_1").show();
})


function f_v3d_loadProject(id) {
    iot3d.loadProject(id,true,function () {
        // iot3d.BackgroundScene()
        iot3d.GetRenderer().setClearColor(0x000000,0)
        f_louceng()
        setTimeout(function(){

            f_v_v3d()

        },3000);

    })  // 加载项目    ProjectID:项目ID,  load_is:是否立即渲染
    // 鼠标在 模型内双击
    iot3d.Model_onDblclick(function (Model) {
        console.log("onDblclick_Model!!!:", Model)
        f_DeviceonDblclick(Model.uuid)
        // // 触发后 聚焦显示 设备名称
        // setTimeout(function(){
        //     f_v_v3d()
        //     // 延时后 正常显示圆点
        //     setTimeout(function(){
        //         Vcss2d_show = 1
        //         f_v_v3d()
        //     },10000);
        // },2000);
    })
}

f_louceng_show_x = ""
function f_louceng_show(x){

    if(x == null){
        x = f_louceng_show_x
    }else {
        f_louceng_show_x = x
    }

    if (x.length == 0 ){
        c_rlist = iot3d.GetScene().children
        for (var C_key in c_rlist) {
            c_rlist[C_key].visible = true
            $(".css_2d_"+c_rlist[C_key].uuid).show();
        }
    }else {
        // 隐藏其他层
        c_rlist = iot3d.GetScene().children
        for (var C_key in c_rlist) {

            if (c_rlist[C_key].name.includes("层") || c_rlist[C_key].name.includes("楼") || c_rlist[C_key].name.includes("库")) {
                c_rlist[C_key].visible = false
                $(".css_2d_"+c_rlist[C_key].uuid).hide();
            }
        }
        iot3d.GetModelByUuid(x).visible = true
        $(".css_2d_"+x).show();
    }

}

// 渲染楼层
function f_louceng() {
    c_rlist = iot3d.GetScene().children
    for (var C_key in c_rlist) {
        if (c_rlist[C_key].name.includes("层") || c_rlist[C_key].name.includes("楼") || c_rlist[C_key].name.includes("库")) {
            $("#div_louceng").append("<div class=\"louceng\" onclick=\"f_louceng_show(\'"+c_rlist[C_key].uuid+"\')\">\n" +
                "                    <img src=\"static/images/层.png\" style=\"width: 50px;height: 50px;margin-top: -16px\">\n" +
                "                    <p style=\"font-size: .2rem;color: #46b8ff;padding-bottom: .2rem;\">"+c_rlist[C_key].name+"</p>\n" +
                "                </div>")
        }


    }
}

function f_v_v3d() {

    for (var C_key in C_json.Device.DeviceList) {
        Device_r = C_json.Device.DeviceList[C_key]

        if(Device_r.T_3dview.length > 0){
            // pubSub.publish(data[i].T_3dview, data[i]);
            // console.log("c:",Device_r.T_3dview)
            Group_Object_Control(Device_r.T_3dview, Device_r)

        }
    }

    f_louceng_show(null)

}
// 修改设备阅览方式
function f_Vcss2d(x) {
    Vcss2d_show = x;
    f_v_v3d();// 刷新页面

    if(x == 1){
        $("#v_modes_2").hide();
        $("#v_modes_1").show();
    }
    if(x == 2){
        $("#v_modes_1").hide();
        $("#v_modes_2").show();
    }

    f_louceng_show('')
}
var xtime = 0
var Object_Control_Time =new Map();
function Group_Object_Control(uuid,json_s) {
    if(iot3d == null) return;

    let Ob = iot3d.GetModelByUuid(uuid)
    if (Ob == undefined) return

    console.log("uuid:",Ob)
    let css2d = Ob.children[1]
    let dt = Ob.children[2]
    let dh = Ob.children[3]
    // console.log("dt：",dt)
    let dled = Ob.children[4].children[8].children[0]
    // console.log("dled:",dled)
    if(Object_Control_Time.has(uuid)){
        // 关闭定时器
        clearInterval(Object_Control_Time.get(uuid));
        Object_Control_Time.delete(uuid);
    }

    let LED_color = f_panduan(json_s)

    // if(f_panduan(json_s) != 1){
    //     LED_color = 2
    // }

    // 定时器 -控制灯
    let LED_f = true
    Object_Control_Time.set(uuid,setInterval(function () {
        if(LED_f){
            LED_f = false
        }else {
            LED_f = true
        }

        // 模型 LED灯
        if (LED_f){
            switch (LED_color) {
                case 1:  // 绿灯  正常
                    dled.material.color.r = 0
                    dled.material.color.g = 1
                    dled.material.color.b = 0
                    break
                case 2:  // 红灯  产品
                    dled.material.color.r = 1
                    dled.material.color.g = 0
                    dled.material.color.b = 0
                    break
                case 0:  // 黄灯  掉线
                	dled.material.color.r = (1/255 * 227)
                	dled.material.color.g = (1/255 * 227)
                	dled.material.color.b = (1/255 * 227)
                	break
            }
        }else {
            // LED   白色灯
            dled.material.color.r = 1
            dled.material.color.g = 1
            dled.material.color.b = 1
        }


        // css2d
        switch (Vcss2d_show) {
            case 0:
                css2d.visible  = false
                break;
            case 1:
                if (LED_f){
                    css2d.visible  = true
                }else {
                    css2d.visible  = false
                }
                break;
            case 2:
                css2d.visible  = true
                break;
        }


        iot3d.Render() // 刷新
    }, 300));
    // CSS2D
    css2d.visible  = true

    parent_m =f_search_parent(Ob)

    switch (Vcss2d_show) {
        case 0:
        case 1:
            switch (LED_color) {
                case 1:  // 绿灯  正常
                    css2d.Up_Html("<div class='panel_y css_2d_"+parent_m.uuid+"' onclick=\"f_DeviceonDblclick(\'"+ json_s.T_3dview +"\')\"  style='box-shadow: 0px 0px 10px #10ef00;background-color: #10ef00;'></div>")
                    break
                case 2:  // 红灯  产品
                    css2d.Up_Html("<div class='panel_y css_2d_"+parent_m.uuid+"' onclick=\"f_DeviceonDblclick(\'"+ json_s.T_3dview +"\')\" style='box-shadow: 0px 0px 10px #ef0000;background-color: #ef0000;'></div>")
                    break
                case 0:  // 红灯  产品
                    css2d.Up_Html("<div class='panel_y css_2d_"+parent_m.uuid+"' onclick=\"f_DeviceonDblclick(\'"+ json_s.T_3dview +"\')\" style='box-shadow: 0px 0px 10px #e3e3e3;background-color: rgba(0,0,0,0.26);'></div>")
                    break
            }
            break;
        case 2:
            switch (LED_color) {
                case 1:  // 绿灯  正常
                    css2d.Up_Html("<div class='panel_s css_2d_"+parent_m.uuid+"' onclick=\"f_DeviceonDblclick(\'"+ json_s.T_3dview +"\')\"   style='background-color: #09ff18;'><div><span id='t'>"+json_s.T_name+"</span></div> <div>温度:<span id='t'>"+json_s.T_DeviceSensorData.T_t.toFixed(1)+"</span></div><div>湿度:<span id='h'>"+json_s.T_DeviceSensorData.T_rh.toFixed(1)+"</span></div></div>")
                    break
                case 2:  // 红灯  产品
                    css2d.Up_Html("<div class='panel_s css_2d_"+parent_m.uuid+"' onclick=\"f_DeviceonDblclick(\'"+ json_s.T_3dview +"\')\"   style='background-color: #ef0000;'><div><span id='t'>"+json_s.T_name+"</span></div><div>温度:<span id='t'>"+json_s.T_DeviceSensorData.T_t.toFixed(1)+"</span></div><div>湿度:<span id='h'>"+json_s.T_DeviceSensorData.T_rh.toFixed(1)+"</span></div></div>")
                    break
                case 0:  // 红灯  产品
                    css2d.Up_Html("<div class='panel_s css_2d_"+parent_m.uuid+"' onclick=\"f_DeviceonDblclick(\'"+ json_s.T_3dview +"\')\"   style='background-color: #e3e3e3;'><div><span id='t'>"+json_s.T_name+"</span></div><div>温度:<span id='t'>"+json_s.T_DeviceSensorData.T_t.toFixed(1)+"</span></div><div>湿度:<span id='h'>"+json_s.T_DeviceSensorData.T_rh.toFixed(1)+"</span></div></div>")
                    break
            }
            break;
    }
    // console.log("json_s:",dt,json_s.T_t,json_s.T_rh)
    dt.Up_Text(""+json_s.T_DeviceSensorData.T_t.toFixed(1))
    dh.Up_Text(""+json_s.T_DeviceSensorData.T_rh.toFixed(1))

}

// 聚焦设备
function f_DeviceonDblclick(uuid) {

    Patrol_mode_is = true
    // f_Patrol_mode()
    Display_mode_is = 0
    $("#Display_mode").html("关闭透视")
    if(uuid == undefined) alert("设备没有绑定模型");
    if(uuid.length > 0){
        // 由于 可能开启了 巡逻，这里需要加延时，不然会抽搐
        setTimeout(function(){
            iot3d.startFocus(iot3d.GetModelByUuid(uuid),2)
            // // 触发后 聚焦显示 设备名称
            // setTimeout(function(){
            //     Vcss2d_show = 2
            //     f_v_v3d()
            //     // 延时后 正常显示圆点
            //     setTimeout(function(){
            //         Vcss2d_show = 1
            //         f_v_v3d()
            //     },10000);
            // },2000);

        },500);

    }
}
function dateChangeFormat(format, date) {
    date = new Date(date);
    const dataItem = {
        'Y+': date.getFullYear().toString(),
        'm+': (date.getMonth() + 1).toString(),
        'd+': date.getDate().toString(),
        'H+': date.getHours().toString(),
        'M+': date.getMinutes().toString(),
        'S+': date.getSeconds().toString(),
    };
    Object.keys(dataItem).forEach((item) => {
        const ret = new RegExp(`(${item})`).exec(format);
        if (ret) {
            format = format.replace(ret[1], ret[1].length === 1 ? dataItem[item] : dataItem[item].padStart(ret[1].length, '0'));
        }
    });
    return format;
}
// 时间比较
function dateChangeFormat_xiangcha( date) {
    // console.log("===========")
    a_t = (new Date(dateChangeFormat('YYYY-mm-dd HH:MM:SS', date)).getTime()/1000)
    b_t = ((new Date()) / 1000)

    // console.log("a_t:",a_t,"b_t:",b_t,">",b_t - a_t)
    return  parseInt(b_t - a_t) ;
}


// 判断状态   返回 0离线  1正常 2超标
function f_panduan(Ds) {

    // console.log("Ds:",Ds)
    // 初始化状态
    src_img = "记录.png"

    // if(Ds.T_sn == "YD062289057646669" || Ds.T_sn == "KF868131063451706" ){
    //     var myDate = new Date();
    //     timexx= myDate.getFullYear() + "-" + (myDate.getMonth()+1) + "-" + myDate.getDate()
    //     if(!timeX.isDuringDate(timexx+' 05:00', timexx+' 17:00')){
    //         src_img = "离线.png"
    //         break;
    //     }
    //
    // }
    if(Ds.T_DeviceSensorParameter.T_free == 1){
        // 传感器 超标
        console.log("闲置状态 ",Ds.T_id);
        return 0
    }
    // 是否 离线
    // if(Ds.T_online > 1)  {
    //     // 传感器超时
    //     // src_img = "离线-线.png"
    //     // if(Ds.T_sn.indexOf("YD") != -1){
    //     // 	src_img = "离线.png"
    //     // }
    //     return 0
    // }
    if(Ds.T_online != 1 && Ds.T_online_s != 1 ){
        console.log("离线:",Ds.T_online , Ds.T_online_s)
        return 0
    }
    if(Ds.T_online != 1 && Ds.T_online_s != 1 ){
        console.log("离线:",Ds.T_online , Ds.T_online_s)
        return 0
    }
    // 传感器正常
    // if(Ds.T_sn.indexOf("YD") != -1){
    // 	if(Ds.T_monitor == 0 ){
    // 		src_img = "停止.png"
    // 	}
    // 	return 0
    // }
    // console.log("超标1：");
    // 是否 超时
    let outtime = 60 * 40
    if( dateChangeFormat_xiangcha(Ds.T_DeviceSensorData.T_time) > outtime)  {
        // 传感器超时
        // src_img = "离线-线.png"
        // if(Ds.T_sn.indexOf("YD") != -1){
        // 	src_img = "离线.png"
        // }
        console.log("超时:",Ds.T_DeviceSensorData.T_time,dateChangeFormat_xiangcha(Ds.T_DeviceSensorData.T_time),Ds)
        return 0
    }
    // console.log("超标1：",Ds.T_t , Ds.T_Tupper,Ds.T_t >=  Ds.T_Tupper);

    // 是否 超标
    if (( Ds.T_DeviceSensorParameter.T_Tlower > Ds.T_DeviceSensorData.T_t) || ( Ds.T_DeviceSensorData.T_t > Ds.T_DeviceSensorParameter.T_Tupper)){

        console.log("温度超标", C_b.T_DeviceSensorParameter.T_Tlower , C_b.T_DeviceSensorData.T_t , C_b.T_DeviceSensorData.T_t , C_b.T_DeviceSensorParameter.T_Tupper)
        return 2
    }

    if (( Ds.T_DeviceSensorParameter.T_RHlower > Ds.T_DeviceSensorData.T_rh) || ( Ds.T_DeviceSensorData.T_rh > Ds.T_DeviceSensorParameter.T_RHupper)){

        console.log("湿度超标", C_b.T_DeviceSensorParameter.T_RHlower , C_b.T_DeviceSensorData.T_rh , C_b.T_DeviceSensorData.T_rh , C_b.T_DeviceSensorParameter.T_RHupper)
        return 2
    }


    // console.log("f_panduan:",Ds,caobiao)
    // if(Ds.T_id == 1){
    // 	console.log("Ds.T_id == 1：",Ds, caobiao);
    // }
    // console.log("超标2：",caobiao);
    // if(Ds.T_DeviceSensorParameter.T_free == 0){
    //     // 传感器 超标
    //     // console.log("超标3：",Ds.T_id);
    //     return 2
    // }

    return 1
}


function f_search_parent(x) {
    if(x.parent.name == "Scene"){
        return x
    }
    return f_search_parent(x.parent)
}