<template>
  <div ref="dragRef">
    <div class="camera" v-for="camera in cameraGroup.children" :key="camera.cameraGroupDetailId">
      <Thumbnail :imageUrl="camera.imageUrl" :code="camera.code" position="right">
        <div class="IconAndCameraName">
          <div class="cameraIcon">
            <WarningFilled
              class="invalidCameraIcon"
              style="color: red"
              v-if="!carouselList.includes(camera) && playingGroup?.id === cameraGroup.id"
            />
            <img
              class="cameraImg"
              src="@/assets/icons/nine-square-grid/camera.png"
              v-if="!cameraInPlay.includes(camera)"
            />
            <img class="cameraImg" src="@/assets/icons/nine-square-grid/playingCamera.png" v-else />
          </div>

          <div
            class="cameraName"
            :class="{
              playingCamera: cameraInPlay.includes(camera),
              invalidCameraName: !carouselList.includes(camera) && playingGroup?.id === cameraGroup.id,
            }"
          >
            {{ camera.name }}
          </div>
        </div>
      </Thumbnail>

      <div class="cameraOperation">
        <img
          class="cameraOperationIcon"
          src="@/assets/icons/nine-square-grid/icon-delete.png"
          @click.stop="handleDelete(cameraGroup, camera)"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, watch } from 'vue';
  import { storeToRefs } from 'pinia';
  import { useDraggable } from 'vue-draggable-plus';
  import { ElMessageBox } from 'element-plus';
  import { WarningFilled } from '@element-plus/icons-vue';
  import { updateCameraOrderApi } from '@/api/nine-square-grid';
  import { useCameraGroupList } from '@/store/modules/useCameraGroupList';
  import Thumbnail from '@/components/thumbnail/Thumbnail.vue';
  import { Camera, CameraGroupType } from '@/types/camera-group';

  const props = defineProps<{
    cameraGroup: CameraGroupType;
  }>();
  const dragRef = ref<HTMLElement | null>(null);
  const list = ref<Camera[]>([]);

  const { carouselList, playingGroup, cameraInPlay, cameraGroupList } = storeToRefs(useCameraGroupList());

  const { deleteCameraFromGroup, deleteCameraInPlaylist, restartPlay } = useCameraGroupList();

  function handleDelete(cameraGroup: CameraGroupType, camera: Camera) {
    const text = '删除后，相机数据不可恢复，是否确认删除？';
    ElMessageBox.confirm(text, '提示', {
      cancelButtonText: '取消',
      confirmButtonText: '确定',
      customClass: 'customMessageBox--warning',
      lockScroll: false,
    })
      .then(() => {
        if (cameraInPlay.value.includes(camera)) {
          deleteCameraInPlaylist(cameraGroup, camera);
        } else {
          deleteCameraFromGroup(cameraGroup, camera);
          restartPlay();
        }
      })
      .catch(() => {
        return;
      });
  }

  watch(
    () => props.cameraGroup.children,
    (children) => {
      list.value = children;
    },
    { immediate: true, deep: true },
  );

  useDraggable(dragRef, list, {
    animation: 150,
    ghostClass: 'ghost',
    onUpdate() {
      const updateIds = list.value.map((x, index) => {
        return {
          groupId: props.cameraGroup.id,
          cameraId: x.id,
          orderNum: index + 1,
        };
      });
      // 通过 store 更新分组中的相机顺序
      const groupInStore = cameraGroupList.value.find((x) => x.id === props.cameraGroup.id);
      if (groupInStore) {
        groupInStore.children = list.value;
      }
      updateCameraOrderApi(updateIds);
      restartPlay();
    },
  });
</script>

<style lang="scss" scoped>
  .camera {
    height: 32px;
    width: 100%;
    white-space: nowrap;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 72px;
    font-size: 14px;
    line-height: 32px;
    color: #333333;
    text-align: left;
    background: #f4f7ff;
    position: relative;
    &:hover {
      background-color: #cdd8ff;
    }
    &:hover .cameraOperation {
      visibility: visible;
      display: flex;
      align-items: center;
    }

    .IconAndCameraName {
      display: flex;
      align-items: center;
      width: 100%;
      .cameraIcon {
        display: flex;
        align-items: center;
        position: relative;
        .cameraImg {
          height: 24px;
          width: 24px;
          margin-right: 12px;
        }
        .invalidCameraIcon {
          height: 14px;
          color: #dd5869;
          position: absolute;
          right: 6px;
          top: 0px;
        }
      }
      .cameraName {
        width: fit-content;
        position: relative;
      }
      .invalidCameraName {
        color: #999999;
      }
    }

    .cameraOperation {
      visibility: hidden;
      margin-right: 4px;
      .cameraOperationIcon {
        height: 20px;
        width: 20px;
        margin-left: 10px;
        padding: 2px;
        z-index: 999;
        &:hover {
          background-color: rgb(197, 197, 197);
          border-radius: 4px;
          cursor: pointer;
        }
      }
    }
  }

  :deep(.thumb-nail) {
    display: flex;
    align-items: center;
    flex: 1;
    height: 100%;
  }

  :deep(.thumb-nail > div) {
    width: 100%;
    height: 100%;
  }
</style>
