package com.sz.admin.monitor.service.impl;

import cn.hutool.core.lang.UUID;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.sz.admin.monitor.mapper.CameraMapper;
import com.sz.admin.monitor.mapper.CameraSnapshotMapper;
import com.sz.admin.monitor.pojo.po.Camera;
import com.sz.admin.monitor.pojo.po.CameraSnapshot;
import com.sz.admin.monitor.pojo.vo.camerasnapshot.CameraSnapshotVO;
import com.sz.admin.monitor.sdk.ManageNVR;
import com.sz.admin.monitor.service.CameraService;
import com.sz.admin.monitor.service.CameraSnapshotService;
import com.sz.admin.monitor.utils.ImageNameUtils;
import com.sz.core.util.BeanCopyUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * ClassName: CameraSnapshotServiceImpl
 * Package: com.sz.admin.monitor.service.impl
 * Description:
 */
@Service
public class CameraSnapshotServiceImpl extends ServiceImpl<CameraSnapshotMapper, CameraSnapshot> implements CameraSnapshotService {
    @Resource
    private ManageNVR manageNVR;
    @Resource
    private CameraSnapshotMapper cameraSnapshotMapper;
    @Resource
    private CameraService cameraService;

    @Override
    public CameraSnapshotVO captureAndSave(Long id, int triggerType) {
        // 生成一个随机的文件名称
        String fileName = ImageNameUtils.generateFileName("CAPTURE", id);
        String path = manageNVR.capturePic(id.intValue(), fileName);
        CameraSnapshot snapshot = new CameraSnapshot();
        snapshot.setImagePath(path);
        snapshot.setCameraId(id);
        snapshot.setCheckType(triggerType);
        // 先删除，后插入
        cameraSnapshotMapper.insert(snapshot);
        return BeanCopyUtils.copy(snapshot, CameraSnapshotVO.class);
    }

    @Override
    @Transactional
    public CameraSnapshotVO captureAndSave(Long id, int triggerType, int presetIndex) {
        // 生成一个随机的文件名称
        String fileName = ImageNameUtils.generateFileName("CAPTURE", id);
        String path = manageNVR.capturePic(id.intValue(), fileName);
        CameraSnapshot snapshot = new CameraSnapshot();
        snapshot.setImagePath(path);
        snapshot.setCameraId(id);
        snapshot.setPresetIndex(presetIndex);
        snapshot.setCheckType(triggerType);
        // 先删除，后插入
        deleteSnapshotByChannelIdAndPresetIndex(id, presetIndex);
        cameraSnapshotMapper.insert(snapshot);
        return BeanCopyUtils.copy(snapshot, CameraSnapshotVO.class);
    }

    @Override
    public CameraSnapshotVO selectByCameraIdAndPresetIndex(Long id, int presetIndex) {
        QueryWrapper wrapper = QueryWrapper.create().select().from(CameraSnapshot.class)
            .where(CameraSnapshot::getCameraId).eq(id)
            .and(CameraSnapshot::getPresetIndex).eq(presetIndex);
        CameraSnapshot cameraSnapshot = cameraSnapshotMapper.selectOneByQuery(wrapper);
        return BeanCopyUtils.copy(cameraSnapshot, CameraSnapshotVO.class);
    }

    @Override
    public List<CameraSnapshotVO> getExistSnapshotList(Long id) {
        QueryWrapper wrapper = QueryWrapper.create().select().from(CameraSnapshot.class)
            .where(CameraSnapshot::getCameraId).eq(id);
        cameraSnapshotMapper.selectListByQuery(wrapper);
        return BeanCopyUtils.copyList(cameraSnapshotMapper.selectListByQuery(wrapper), CameraSnapshotVO.class);
    }



    private void deleteSnapshotByChannelIdAndPresetIndex(Long id, int presetIndex) {
        QueryWrapper wrapper = QueryWrapper.create().select().from(CameraSnapshot.class)
                .where(CameraSnapshot::getCameraId).eq(id)
                .and(CameraSnapshot::getPresetIndex).eq(presetIndex);
        CameraSnapshot cameraSnapshot = cameraSnapshotMapper.selectOneByQuery(wrapper);
        if (cameraSnapshot != null) {
            cameraSnapshotMapper.delete(cameraSnapshot);
        }

    }
}
