var camCtrl;
var uiCtrl;
var buildCtrl;

var _canvas;
var _engine;
var _scene;


var id;

var pingmu;
var ignoreVisit;

window.onload = function(){
    if(!ToolMgr.isMobel()){
        let link = document.getElementById('linkcss');
        link.href = "http://exhibition.szwyg.org.cn/resources/studio_modern/res/css/style.css";
    }
    let params = ToolMgr.getParams(location.search);
    if(params && params.id){
        id = params.id;
        ignoreVisit = params.ignore_visit
        initEnv();
        _engine.runRenderLoop(function () {
            _scene.render();
            TWEEN.update();
            // console.log(_engine.getFps());
        });
    }
    else{
        alert("入参xx.html?id=1不存在");
    }
}
window.addEventListener("resize", function (ev) {
    if(_engine){
        _engine.resize();
    }
});
function initEnv() {
    _canvas = document.getElementById("renderCanvas");
    if(ToolMgr.isMobel()){
        _engine = new BABYLON.Engine(_canvas, true);
    }
    else {
        _engine = new BABYLON.Engine(_canvas, true);
    }
    _scene = new BABYLON.Scene(_engine);
     _scene.environmentTexture = new BABYLON.HDRCubeTexture("http://exhibition.szwyg.org.cn/resources/studio_modern/res/hdr/1.hdr", _scene, 128, false, true, false, true);
    _scene.environmentIntensity = 1;

    var light = new BABYLON.HemisphericLight("HemiLight", new BABYLON.Vector3(0, 1, 0), _scene);
    light.intensity = 1;
    initCtrl();
    DataConfig.getAllData(id, (datas)=> {
        //加载模型
        buildCtrl.loadBuildModel("http://exhibition.szwyg.org.cn/resources/studio_modern/res/models/", "modern.glb", (value) => {
            uiCtrl.setProgress(value + "%")
        }, () => {
            for(let i = 0; i < datas.rooms.length; i++){
                if(datas.rooms[i].number == "3"){
                    uiCtrl.createLeftMenu(datas.rooms[i].exhibits);
                    break;
                }
            }
            uiCtrl.loadFinish();
        });
    });
}
function initCtrl() {
    //相机控制
    camCtrl = new CamCtrl(_canvas, _scene);
    camCtrl.awake();
    //建筑
    buildCtrl = new BuildCtrl(_scene);
    uiCtrl = new UICtrl();
}
function playFirst(){
    uiCtrl.playNextVideo();
}
//播放视频
function playPingMu(url) {
    if (pingmu) {
        var ANote0VideoMat = pingmu.material;
        if (ANote0VideoMat.albedoTexture != null && ANote0VideoMat.albedoTexture.video != null) {
            ANote0VideoMat.albedoTexture.video.removeEventListener('ended', videoEnded, false);
            ANote0VideoMat.albedoTexture.video.pause();
        }
        let ANote0VideoVidTex = new BABYLON.VideoTexture("vidtex", url, _scene, false, true);//, false, false, BABYLON.TRILINEAR_SAMPLINGMODE, {autoPlay:true, muted:true, autoUpdateTexture:true});
        // ANote0VideoVidTex.video.autoPlay = false;
        // ANote0VideoVidTex.video.autoUpdateTexture = true;
        ANote0VideoVidTex.video.loop = false;

        // ANote0VideoVidTex.vScale = -1;
        ANote0VideoVidTex.video.play();
        ANote0VideoVidTex.video.addEventListener('play', function () { //
            console.log("播放开始");
            pingmu.scaling.y = -1;
            // pingmu.position.y = 7.4;
            ANote0VideoMat.albedoTexture = ANote0VideoVidTex;
            ANote0VideoMat.albedoColor = new BABYLON.Color3.White();
        }, false);

        ANote0VideoVidTex.video.addEventListener('ended', videoEnded, false);
    }
}

function videoEnded() {
    console.log("播放结束");
    uiCtrl.playNextVideo();
}

function showPicInfo(id){
    console.log(id);
    $("#detialWindow").attr("src", "http://182.92.75.61/view/html/tourist/details.html?id="+id);
    $("#showDetial").show();
}
