<template>
  <div :style="imgStyle">
    <img style="width: 100%; height: 100%; object-fit: contain" :src="url" v-if="url" />
    <div class="image-slot" v-if="!url">
      <i class="el-icon-video-camera"></i>
    </div>
  </div>
</template>

<script>
import Dayjs from 'dayjs'

export default {
  name: 'ImageVideo',
  components: {},
  data() {
    return {
      url: '',
      lastUrl: '',
      ws: null,
      zoom: false,
      addWidth: 0,
      addHeight: 0,
      startTime: null,
      speed: 5,
      presetIndex: 1,
      datePickerOptions: {
        disabledDate: (currData) => currData > new Date(),
      },
      onPlayBack: false,
    }
  },
  props: {
    visible: false,
    cameraData: {
      type: Object,
      required: true,
    },
    size: {
      type: Object,
      default: () => {
        return { height: 420, width: 680 }
      },
    },
  },
  computed: {
    dialogStyle() {
      if (this.zoom) {
        let clientHeight = document.documentElement.clientHeight
        return {
          height: clientHeight - 174 + this.addHeight + 'px',
          width:
            ((clientHeight - 234) / (this.size.height - 60)) * (this.size.width - 40) +
            40 +
            this.addWidth +
            'px',
        }
      } else {
        return {
          height: this.size.height + this.addHeight + 'px',
          width: this.size.width + this.addWidth + 'px',
        }
      }
    },
    imgStyle() {
      return {
        position: 'absolute',
        left: '0px',
        top: '0px',
        right: this.addWidth + 'px',
        bottom: this.addHeight + 'px',
        'object-fit': 'contain',
      }
    },
  },
  watch: {
    cameraData(val) {
      if (val.id) {
        //console.log(val)
        if (val.canPlayBack) this.addHeight = 1
        else this.addHeight = 0
        if (val.ptzEnable) this.addWidth = 1
        else this.addWidth = 0
        if (this.ws && this.ws.readyState == 1) {
          this.ws.send(JSON.stringify({ id: val.id, canControl: val.ptzEnable }))
        } else {
          this.init(JSON.stringify({ id: val.id, canControl: val.ptzEnable }))
        }
        setTimeout(() => {
          this.url = ''
          if (this.lastUrl) URL.revokeObjectURL(this.lastUrl)
          this.lastUrl = ''
        }, 500)
      }
    },
  },
  mounted() {},
  created() {
    //this.init()
  },
  methods: {
    init(str) {
      this.ws = new WebSocket(
        'ws://' + window.location.host + process.env.VUE_APP_BASE_API + '/VideoOverWebSocket'
      )
      let _this = this
      this.ws.onmessage = function (evt) {
        if (typeof evt.data === 'string') {
          console.log(JSON.parse(evt.data).Msg)
        } else {
          let currUrl = URL.createObjectURL(evt.data)
          if (_this.lastUrl) URL.revokeObjectURL(_this.lastUrl)
          _this.lastUrl = currUrl
          _this.url = currUrl
        }
      }
      if (str) {
        setTimeout(() => {
          this.ws.send(str)
        }, 500)
      }
    },
    zoomHandler() {
      this.zoom = this.zoom ? false : true
    },
    closeHandler() {
      if (this.ws && this.ws.readyState == 1) {
        this.ws.send(JSON.stringify({ id: '0' }))
      }
      setTimeout(() => {
        this.ws.close()
        if (this.url) URL.revokeObjectURL(this.url)
        this.url = ''
        this.lastUrl = ''
        this.zoom = false
        this.$emit('update:cameraData', {})
        this.$emit('update:visible', false)
      }, 500)
    },
    playBackHandle() {
      this.onPlayBack = true
      if (this.ws && this.ws.readyState == 1) {
        let time = Dayjs(this.startTime).format('YYYY-MM-DD HH:mm:ss')
        this.ws.send(JSON.stringify({ id: this.cameraData.id, startTime: time }))
      } else {
        this.init(JSON.stringify({ id: this.cameraData.id, startTime: time }))
      }
      setTimeout(() => {
        this.url = ''
        if (this.lastUrl) URL.revokeObjectURL(this.lastUrl)
        this.lastUrl = ''
      }, 500)
    },
    realTimekHandle() {
      this.onPlayBack = false
      this.startTime = null
      if (this.ws && this.ws.readyState == 1) {
        this.ws.send(
          JSON.stringify({ id: this.cameraData.id, canControl: this.cameraData.ptzEnable })
        )
      } else {
        this.init(JSON.stringify({ id: this.cameraData.id, canControl: this.cameraData.ptzEnable }))
      }
      setTimeout(() => {
        this.url = ''
        if (this.lastUrl) URL.revokeObjectURL(this.lastUrl)
        this.lastUrl = ''
      }, 500)
    },
    ptzCommHandle(comm, stop) {
      if (this.ws && this.ws.readyState == 1) {
        this.ws.send(
          JSON.stringify({
            id: this.cameraData.id,
            canControl: true,
            ptzCommand: comm,
            ptzStop: stop,
            ptzSpeed: this.speed,
          })
        )
      } else {
        this.init(
          JSON.stringify({
            id: this.cameraData.id,
            canControl: true,
            ptzCommand: comm,
            ptzStop: stop,
            ptzSpeed: this.speed,
          })
        )
      }
    },
    ptzPresetHandle(comm) {
      if (this.ws && this.ws.readyState == 1) {
        this.ws.send(
          JSON.stringify({
            id: this.cameraData.id,
            canControl: true,
            ptzPreset: comm,
            ptzPresetIndex: this.presetIndex,
          })
        )
      } else {
        this.init(
          JSON.stringify({
            id: this.cameraData.id,
            canControl: true,
            ptzPreset: comm,
            ptzPresetIndex: this.presetIndex,
          })
        )
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.dialog-container {
  position: absolute;
  right: 20px;
  bottom: 20px;
  z-index: 5;
  color: white;
  background-color: #10316b;
  // background-color: rgba(48, 65, 86, 0.8);
  box-sizing: border-box;
  border-radius: 20px;
}
.dialog-header {
  position: absolute;
  left: 20px;
  top: 0px;
  right: 20px;
  height: 40px;
  font-size: 20px;
  padding-top: 10px;
  float: left;
}
.close-icon {
  margin-left: 10px;
  float: right;
}
.close-icon:hover {
  color: #368ae8;
  // color: rgb(28, 188, 156);
}
.zoom-icon {
  float: right;
}
.zoom-icon:hover {
  color: #368ae8;
  // color: rgb(28, 188, 156);
}
.dialog-body {
  position: absolute;
  left: 20px;
  top: 40px;
  right: 20px;
  bottom: 20px;
}
.ptz-panel {
  position: absolute;
  top: 0px;
  right: 0px;
  bottom: 0px;
  width: 150px;
}
.playback-panel {
  position: absolute;
  left: 0px;
  right: 0px;
  bottom: 0px;
  height: 50px;
  padding-top: 15px;
}
::v-deep .image-slot {
  width: 100%;
  height: 100%;
  background-color: #063e92;
  color: #909399;
  font-size: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
