import { AfterViewInit, Component, ElementRef, OnDestroy, OnInit, ViewChild, } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { MyService } from 'src/app/my/my.service';

@Component({
  selector: 'ydlife-video',
  templateUrl: './video.component.html',
  styleUrls: ['./video.component.scss']
})
export class VideoComponent implements OnInit, AfterViewInit, OnDestroy {
  @ViewChild('video') video: ElementRef;
  videoSrc: string;
  pdfPath: any;
  permissions: any;
  lifeCustomerInfo: any;
  timer: any;
  timer2:any;
  videoPlaybacks: Array<any>;
  originTime: number = 0;
  constructor(private activatedRoute: ActivatedRoute, private myService: MyService) { }

  ngOnInit() {
    this.videoSrc = sessionStorage.getItem('videoPath');
    this.permissions = JSON.parse(sessionStorage.getItem('permissions'));
    this.pdfPath = this.activatedRoute.snapshot.queryParams['path'];
    this.lifeCustomerInfo = JSON.parse(localStorage.getItem('lifeCustomerInfo'));
    this.timer = setInterval(() => {
      this.saveVideoPlayback()
    }, 1000 * 20)
    this.queryVideoPlayback()
  }

  ngAfterViewInit(): void {
    this.video.nativeElement.addEventListener('pause', ()=> { //暂停开始执行的函数
      console.log('暂停播放')
      this.saveVideoPlayback();
    });
        // 禁止拖动进度条
    this.timer2 = setInterval(()=> {
      const currentTime = this.video.nativeElement.currentTime;
      const originTime = Number(sessionStorage.getItem('curTime')) ;
      if (currentTime - originTime > 1) {
        this.video.nativeElement.currentTime = sessionStorage.getItem('curTime')
        this.video.nativeElement.pause();
        sessionStorage.setItem('curTime',currentTime)
      } 
    }, 500)
    // >>>>>>>>>>>>>禁止拖动进度条结束<<<<<<<<<<<<<<
    const attr = this.video.nativeElement.getAttribute('controlslist')
    if (this.permissions.isDownload == '2') {
      this.video.nativeElement.setAttribute('controlslist', attr + ' nodownload')
    } else {
      this.video.nativeElement.setAttribute('controlslist', attr)
    }
    if (this.permissions.isControlPlayback == '2') {
      this.video.nativeElement.setAttribute('controlslist', attr + ' noplaybackrate')
    } else {
      this.video.nativeElement.setAttribute('controlslist', attr)
    }
  }
  ngOnDestroy(): void {
    clearInterval(this.timer)
    clearInterval(this.timer2)
    this.saveVideoPlayback();
  }
  menuPrevent() {
    return false;
  }
  saveVideoPlayback() {
    // playbackStatus		视频播放状态(1:未播放完;2:已播放完)	【必填】
    const param = {
      id: '',
      customerId: this.lifeCustomerInfo.customerId,
      practitionerId: this.lifeCustomerInfo.practitionerId,
      fileId: '',
      totalTime: this.video.nativeElement.duration,
      viewTime: this.video.nativeElement.currentTime,
      playbackStatus: this.video.nativeElement.currentTime >= this.video.nativeElement.duration ? 2 : 1,
    }
    sessionStorage.setItem('curTime',this.video.nativeElement.currentTime);
    this.myService.saveVideoPlayback(param).subscribe(res => {
      console.log(res);
    })
  }

  queryVideoPlayback() {
    const param = {
      id: '',
      customerId: this.lifeCustomerInfo.customerId,
      practitionerId: this.lifeCustomerInfo.practitionerId,
      fileId: '',
    }
    this.myService.queryVideoPlayback(param).subscribe(res => {
      if (res['success']) {
        this.videoPlaybacks = res['data']['videoPlaybacks'];
        // 设置开始播放时间为上次离开时间
        this.video.nativeElement.currentTime = this.videoPlaybacks.length > 0 ? this.videoPlaybacks[0]['viewTime'] : 0;
      } else {
        this.video.nativeElement.currentTime = 0;
      }
      console.log(res);
    })
  }

}
