<template>
  <BaseSameLayer
    ref="baseSameLayer"
    :hosSameLayerArgs="hosSameLayerArgs"
    embedType="native/midea-ipc-live-view"
    :defaultWidth="300"
    :defaultHeight="300"
  ></BaseSameLayer>
</template>

<script>
import BaseSameLayer from './baseSameLayer.vue';
const weexModule = weex.requireModule('weexModule');

export default {
  data() {
    return {
      width: 0,
      height: 0,
      embedId: '',
    };
  },
  name: 'MideaIpcLiveView',
  components: {
    BaseSameLayer,
  },
  props: {
    src: {
      type: String,
      default: '',
    },
    url: {
      type: String,
      default: '',
    },
    videoResize: {
      type: Number,
      default: '',
    },
    autoplay: {
      type: Boolean,
      default: false,
    },
    videoCover: {
      type: String,
      default: '',
    },
    bottomShadow: {
      type: Object,
      default: () => {},
    },
    isSupportScale: {
      type: Boolean,
      default: false,
    },
    controls: {
      type: Boolean,
      default: false,
    },
    topShadow: {
      type: Object,
      default: () => {},
    },
    channel: {
      type: [Number, String],
      default: 0,
    },
    playStatus: {
      type: String,
      default: '',
    },
    mute: {
      type: [Number, Boolean],
      default: 1,
    },
    writeToFile: {
      type: Number,
      default: 1,
    },
    analyzeDuration: {
      type: Number,
      default: 100000,
    },
    isOnlyOnePlayer: {
      type: Boolean,
      default: true,
    },
    disallowInterceptTouchEvent: {
      type: Boolean,
      default: false,
    },
    hosUniqueProps: {
      type: Object,
      default() {
        return {};
      },
    },
    data: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  computed: {
    ref() {
      return this.embedId;
    },
    muted() {
      return this.mute === 1 || this.mute === true;
    },
    hosSameLayerArgs() {
      return {
        ...this.hosUniqueProps,
        width: this.width,
        height: this.height,
        data: this.data,
        src: this.src,
        url: this.url,
        channel: this.channel,
        playStatus: this.playStatus,
        muted: this.muted,
        mute: this.mute,
        writeToFile: this.writeToFile,
        analyzeDuration: this.analyzeDuration,
        isOnlyOnePlayer: this.isOnlyOnePlayer,
        disallowInterceptTouchEvent: this.disallowInterceptTouchEvent,
        videoResize: this.videoResize,
        autoplay: this.autoplay,
        videoCover: this.videoCover,
        bottomShadow: this.bottomShadow,
        isSupportScale: this.isSupportScale,
        controls: this.controls,
        topShadow: this.topShadow,
        hosUniqueProps: this.hosUniqueProps,
        onPlayerClick: this.onPlayerClick,
        onMoveControl: this.onMoveControl,
        onScaleControl: this.onScaleControl,
        onScaleChanged: this.onScaleChanged,
        onMoveEnd: this.onMoveEnd,
        onStatePrepared: this.onStatePrepared,
        onAutoCompletion: this.onAutoCompletion,
        onStateAutoComplete: this.onStateAutoComplete,
      };
    },
  },
  mounted() {
    this.width = this.$el.clientWidth;
    this.height = this.$el.clientHeight;
    this.embedId = this.$refs.baseSameLayer && this.$refs.baseSameLayer.embedId;
  },
  methods: {
    captureImage(params, callback) {
      delete params.refId;
      weexModule.callNative(
        'ipcLinkApi',
        {
          method: 'captureImage',
          name: this.embedId,
          params,
        },
        callback
      );
    },
    switchVideoQuality(params, callback) {
      weexModule.callNative(
        'ipcLinkApi',
        {
          method: 'switchVideoQuality',
          name: this.embedId,
          params,
        },
        callback
      );
    },
    getVideoNetworkSpeed(params, callback) {
      weexModule.callNative(
        'ipcLinkApi',
        {
          method: 'getVideoNetworkSpeed',
          name: this.embedId,
          params,
        },
        callback
      );
    },
    startRecord(params, callback) {
      weexModule.callNative(
        'ipcLinkApi',
        {
          method: 'startRecord',
          name: this.embedId,
          params,
        },
        callback
      );
    },
    stopRecord(params, callback) {
      weexModule.callNative(
        'ipcLinkApi',
        {
          method: 'stopRecord',
          name: this.embedId,
          params,
        },
        callback
      );
    },
    changeAudioStatus(params, callback) {
      weexModule.callNative(
        'ipcLinkApi',
        {
          method: 'changeAudioStatus',
          name: this.embedId,
          params,
        },
        callback
      );
    },
    onPlayerClick(res) {
      this.$emit('onPlayerClick', res);
    },
    onMoveControl(res) {
      this.$emit('onMoveControl', res);
    },
    onScaleControl(res) {
      this.$emit('onScaleControl', res);
    },
    onScaleChanged(res) {
      this.$emit('onScaleChanged', res);
    },
    onMoveEnd(res) {
      this.$emit('onMoveEnd', res);
    },
    onStatePrepared() {
      this.$emit('onStatePrepared');
    },
    onAutoCompletion() {
      this.$emit('onAutoCompletion');
    },
    onStateAutoComplete() {
      this.$emit('onStateAutoComplete');
    },
  },
};
</script>
