import { NModal, NSpin } from 'naive-ui';
import { defineComponent, ref, toRef, watch } from 'vue';
import styles from './index.module.less';
import VideoModal from './video-modal';
import MusicModal from './music-modal';
import SongModal from './song-modal';
import TheEmpty from '../TheEmpty';

export default defineComponent({
  name: 'card-preview',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    item: {
      type: Object,
      default: () => ({})
    },
    size: {
      type: String,
      default: 'default'
    },
    /** 是否下载 只支持 video audio */
    isDownload: {
      type: Boolean,
      default: false
    }
  },
  emit: ['update:show'],
  setup(props, { emit }) {
    const show = toRef(props.show);
    const item = toRef(props.item);
    const pptLoading = ref(true);

    watch(
      () => props.show,
      () => {
        show.value = props.show;
      }
    );

    watch(
      () => props.item,
      () => {
        item.value = props.item;
      }
    );
    return () => (
      <>
        <NModal
          v-model:show={show.value}
          onUpdate:show={() => {
            emit('update:show', show.value);
            if (!show.value) {
              pptLoading.value = true;
            }
          }}
          preset="card"
          showIcon={false}
          class={[
            'modalTitle background',
            styles.cardPreview,
            item.value.type === 'PPT' && styles.maxCard,
            props.size === 'large' && styles.cardLarge
          ]}
          title={item.value.title}
          blockScroll={false}>
          {item.value.type === 'VIDEO' && (
            <VideoModal
              title={item.value.title}
              poster={item.value.url}
              src={item.value.content}
              isDownload={props.isDownload}
            />
          )}
          {item.value.type === 'MUSIC' && (
            <MusicModal class={styles.musicPreview} item={item.value} />
          )}
          {item.value.type === 'SONG' && (
            <SongModal item={item.value} isDownload={props.isDownload} />
          )}
          {item.value.type === 'PPT' && (
            <NSpin show={pptLoading.value}>
              <iframe
                class={styles.pptBox}
                src={`https://view.officeapps.live.com/op/embed.aspx?src=${encodeURIComponent(
                  item.value.content
                )}`}
                onLoad={() => {
                  console.log('loading end');
                  pptLoading.value = false;
                }}
                width="100%"
                height="100%"
                frameborder="1"></iframe>
            </NSpin>
          )}
          {!['VIDEO', 'MUSIC', 'SONG', 'PPT'].includes(item.value.type) && (
            <TheEmpty />
          )}
        </NModal>
      </>
    );
  }
});
