Component({
  behaviors: [require('../common/share-behavior').default],
  properties: {
    captureState: {
      type: Number,
      value: 0,
      observer: function (newVal, oldVal) {
        if (newVal !== oldVal) {
          if (newVal === 1) {
            this.capture();
            this.ShowModel();
          }
        }
      },
    },
    recordState: {
      type: Number,
      value: 0,
      observer: function (newVal, oldVal) {
        if (newVal !== oldVal) {
          if (newVal === 0) {
            this.recordEnd();
          } else {
            this.recordStart();
          }
        }
      }
    },
    captureQuality: {
      type: Number,
      value: 0.8,
    },
    captureType: {
      type: String,
      value: 'jpg',
    },
    recordFPS: {
      type: Number,
      value: 30,
    },
    recordWidth: {
      type: Number,
      value: undefined,
    },
    recordHeight: {
      type: Number,
      value: undefined,
    },
    recordBPS: {
      type: Number,
      value: 1000,
    },
    uiShowModelView:{
      type: Boolean,
      value: false,
      observer: function (newVal, oldVal) {
      
        if (newVal !== oldVal) {
          if(newVal){
            //this.ShowModel(1);
          }else{
            this.CloseModel();
          }
        }     
      },
    },   
    uiShowIndex:{
      type: Number,
      value: 0,
      observer: function (newVal, oldVal) {
        if (newVal !== oldVal) {
          this.ShowModel(newVal);
        }
      },
    }
  },
  data: {
    loaded: false,
    loadedSaoMiao:false,
    loaded: false,
    arReady: false,
    loaded2:false,
    position2:[0,0,0],
    rot:0,
    str:"1",
    longshow : false,
    binglongshow:false,
    lanjiachongshow:false,
  },

  lifetimes: {
    async attached() {
      console.log('data', this.data);
    }
  },
  methods: {
    handleReady: function ({detail}) {
      const xrScene = this.scene = detail.value;
      console.log('xr-scene', xrScene);

      this.binglong = {el:this.scene.getElementById('binglong')};

      const scene = this.scene = detail.value;
      const appHide = () => this.scene.share.recordPause();
      const appShow = () => this.scene.share.recordResume();
      wx.onAppHide(appHide);
      wx.onAppShow(appShow);
      wx.offAppHide(appHide);
      wx.offAppShow(appShow);
      
      this.triggerEvent('sceneReady', {width: scene.width, height: scene.height});

    },
    handleAssetsProgress: function({detail}) {
      this.triggerEvent('assetsProgress', detail.value);
    },
    handleAssetsLoaded: function ({detail}) {
      console.log('assets loaded', detail.value);
      this.triggerEvent('assetsLoaded', detail.value);
      this.setData({
        loaded: true
      });
     
    },handleGltfLoaded2: function() {
      
    },handleGltfLoaded: function() {
      
    },
    handleTrackerSwitch: function ({detail}) {


      const active = detail.value;
      if(active)
      {
        this.setData({
          loaded2: true
        });
      }
    },handleARTrackerState({detail}) {
      // 事件的值即为`ARTracker`实例
      const tracker = detail.value;
      // 获取当前状态和错误信息
      const {state, errorMessage} = tracker;

      const element = detail.el;


      if( state==2)
      {

        this.triggerEvent('assetsstr',"state"  + state);
        
/*
        this.triggerEvent('assetsstr',"x:"  + (180/Math.PI* element.scene.getNodeById("Marker").rotation.x) +"\ny:"  +  (180/Math.PI*element.scene.getNodeById("Marker").rotation.y)+"\nz:"  +  (180/Math.PI*element.scene.getNodeById("Marker").rotation.z));*/
      
      }


    },
    handleAnimationStop: function() {
      console.log('animation Stop');
    },
    handleGLTFLoadedbingLong:function({detail}){
      const el2 = detail.value;
      const el = detail.value.target;
      const animator = el.getComponent(xrFrameSystem.Animator);
      //animator.play("ui_motion_1");
      
      var state = true;

      const xrScene = this.scene;
      const xrFrameSystem = wx.getXrFrameSystem();
      const myModel = xrScene.getElementById('binglong');
      this.myModelAnimator = myModel.getComponent(xrFrameSystem.Animator);
      const clips = this.myModelAnimator._clips;
      this.clipName = []
       clips.forEach((v, key) => {
        if (key.indexOf('pose') == -1) {
          this.clipName.push(key)
        }
      })
      wx.showToast({
        title: this.clipName[0],
      });
      this.myModelAnimator.play.play(this.clipName[0],{ loop:5,});

      var interval = setInterval(function() {
        if(false)
        {
          clearInterval(interval);
        }
        console.log(5000 +" 毫秒更新");
        if(state)
        {
          state = false;
          this.myModelAnimator.play.play(this.clipName[1],{ loop:5,});
        }else{
          state = true;
          this.myModelAnimator.play.play(this.clipName[2],{ loop:5,});
          el2.position =(0 -2.5 -40);
        }
        
       }, 5000)

    },
    capture() {
      this.scene.share.captureToFriends({
        fileType: this.data.captureType,
        quality: this.data.captureQuality
      });
    },
    recordStart() {
      console.log('recordStart') 
      wx.updateShareMenu({
        withShareTicket: true,
        success () { }
      })
      this.scene.share.recordStart({
        fps: this.data.recordFPS,
        videoBitsPerSecond: this.data.recordBPS,
        width: this.data.recordWidth,
        height: this.data.recordHeight
      });
    },
    recordEnd() {
      console.log('recordEnd')
     
      
      //
      const tempFilePath =  this.scene.share.recordFinishToAlbum({
        fps: this.data.recordFPS,
        videoBitsPerSecond: this.data.recordBPS,
        width: this.data.recordWidth,
        height: this.data.recordHeight
      });
      wx.shareVideoMessage({
        videoPath: tempFilePath,
      });
    },
    ShowModel(modelIndex){
      
      switch(modelIndex){
        case 0:
          this.setData({longshow:true});
          this.setData({binglongshow:false});
          this.setData({lanjiachongshow:false});
          break;
        case 1:
          this.setData({longshow:false});
          this.setData({binglongshow:true});
          this.setData({lanjiachongshow:false});
          break;
        case 2:
          this.setData({longshow:false});
          this.setData({binglongshow:false});
          this.setData({lanjiachongshow:true});
          break;
      }
     
    },
    CloseModel(){
      this.setData({longshow:false});
      this.setData({binglongshow:false});
      this.setData({lanjiachongshow:false});
    }
  }
})