import { defineComponent, ref, watch } from 'vue';
import styles from './index.module.less';
import { useUserStore } from '/src/store/modules/users';
import { vaildMusicScoreUrl } from '/src/utils/urlUtils';

export default defineComponent({
  name: 'song-modal',
  props: {
    item: {
      type: Object,
      default: () => ({})
    },
    activeStatus: {
      type: Boolean,
      default: false
    }
  },
  emits: ['setIframe'],
  setup(props, { emit }) {
    const userStore = useUserStore();
    const iframeRef = ref();
    const isLoaded = ref(false);
    let src = `${
      location.origin
    }/classroom-app/#/tempo-practice?v=${+new Date()}&platform=modal&dataJson=${
      props.item.dataJson
    }&Authorization=${userStore.getToken}&win=pc`;
    if (/(localhost)/.test(location.host)) {
      src = `http://localhost:9002/#/tempo-practice?v=${+new Date()}&platform=modal&dataJson=${
        props.item.dataJson
      }&Authorization=${userStore.getToken}&win=pc`;
    }

    watch(
      () => props.activeStatus,
      () => {
        if (!props.activeStatus) {
          iframeRef.value.contentWindow?.postMessage({ api: 'resetPlay' }, '*');
        }
      }
    );

    return () => (
      <div class={styles.musicScore}>
        <iframe
          ref={iframeRef}
          onLoad={() => {
            emit('setIframe', iframeRef.value);
            isLoaded.value = true;
          }}
          class={[styles.container, 'musicIframe']}
          frameborder="0"
          src={src}></iframe>
      </div>
    );
  }
});
