// import consola from 'consola';
import { ref } from 'vue';
import { useTourStore } from '../store/modules/tour';
import { useApp } from './userApp';

const playing = ref(false);
const hasTours = ref(false);
let player;

async function createTourPlayer() {
  const app = await useApp();
  player = await app.use('TourPlayer');
  const tourStore = useTourStore();
  app.TourManager.on('loaded', (tours) => {
    // existsGuide.value = !!tours.length
    if (tours?.length) hasTours.value = true;
    tourStore.setData(tours);
  });
  // { partId, frameId }
  console.log('player', player);
  player.on('play', () => {
    playing.value = true;
  });
  player.on('pause', () => {
    playing.value = false;
  });
  // { partId, frameId, progress }
  player.on('progress', () => {
    // 画面进度
    // this.partId = partId
    // this.frameId = frameId
    // this.progress = Number(progress * 100).toFixed(5)
    // // 片段进度
    // if (this.tours.length == 1) {
    //     this.progressPart = this.progress
    // } else {
    //     if (currPartId != partId) {
    //         currPartId = partId
    //         currFrames = this.tours[partId].list.length
    //         this.progressPart = 0
    //     }
    //     this.progressPart += progress / currFrames
    // }
  });
}

export function useTourPlayer() {
  return {
    createTourPlayer,
    playing,
    hasTours,
    player,
  };
}
