import { Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { ExamVideoStdScoreComponent } from '../exam-video-std-score/exam-video-std-score.component';
declare var aliPlayerUtil :any;

@Component({
  selector: 'app-exam-video-page',
  templateUrl: './exam-video-page.component.html',
  styleUrls: ['./exam-video-page.component.scss']
})
export class ExamVideoPageComponent extends FramePageComponent implements OnInit {
  agentArray = [];
  ly_agent_id;
  tpl: any;
  bsModalWin: any;
  aliPlayer: any;
  curStep = '';
  repeat = true;
  VideoTotal: any;

  constructor(private modalService: BsModalService) {
    super(modalService);
  }

  ngOnInit() {
    // 获取所有考点
    AjaxService.requestArray('exam/checkin/init/agent.htm', {}, (array) => {
      this.agentArray = array;
      if (this.agentArray.length === 1) {
        this.ly_agent_id = this.agentArray[0].ly_agent_id;
        this.pager.pagerParams["ly_agent_id"] = this.ly_agent_id;
        this.pager.reload();
      }
    });
  }

  showStdDetail(std: any) {
    this.showComponentModal(ExamVideoStdScoreComponent, {
      'ResponseEntity': std,
      'arrayUrl': 'exam/video/dist/std/score.htm',
      'arrayParam': {
        room_id : std.ly_room_id,
        std_id : std.std_id
      }
    });
  }

  startVideo(tpl, video_file) {
    this.tpl = tpl;
    this.nextGroupVideo(true, video_file);
  }

  nextGroupVideo(firstLoad, video_file) {
    if (firstLoad) {
      this.initVideoPlayer();
    }
    aliPlayerUtil.changeSource(this.aliPlayer, video_file);
  }

  initVideoPlayer(url?) {
    let _this = this;
    this.bsModalWin = this.modalService.show(this.tpl, { class: 'modal-9p' , backdrop: 'static', keyboard: false});
    this.aliPlayer = aliPlayerUtil.init('aliPlayer', url);
    // 技术播放自动下一个
    this.aliPlayer.on("ended", function() {
      debugger;
    });
  }

  changeVideoSource(url){
    aliPlayerUtil.changeSource(this.aliPlayer,url);
  }

  hideModal(){
    if(this.bsModalWin) this.bsModalWin.hide();
  }

  pauseVideo(){
    aliPlayerUtil.pause(this.aliPlayer);
  }

  playVideo(){
    aliPlayerUtil.play(this.aliPlayer);
  }
}

