package com.usky.sas.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.sas.domain.SasDevice;
import com.usky.sas.domain.SasMapDevice;
import com.usky.sas.domain.SasMaps;
import com.usky.sas.mapper.SasDeviceMapper;
import com.usky.sas.mapper.SasMapDeviceMapper;
import com.usky.sas.mapper.SasMapsMapper;
import com.usky.sas.service.SasMapNavigateService;
import com.usky.sas.service.vo.MapDeviceDetailResponse;
import com.usky.sas.service.vo.MapNavigateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SasMapNavigateServiceImpl implements SasMapNavigateService {

    @Autowired
    private SasMapsMapper sasMapsMapper;

    @Autowired
    private SasMapDeviceMapper sasMapDeviceMapper;

    @Autowired
    private SasDeviceMapper sasDeviceMapper;

    @Override
    public MapNavigateResponse navigate(String mapId, Double x, Double y) {
        SasMaps map = sasMapsMapper.selectById(mapId);
        if (map == null) {
            return null;
        }
        LambdaQueryWrapper<SasMapDevice> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SasMapDevice::getMapId, mapId);
        List<SasMapDevice> mapDevices = sasMapDeviceMapper.selectList(wrapper);

        MapNavigateResponse resp = new MapNavigateResponse();
        MapNavigateResponse.MapInfo mapInfo = new MapNavigateResponse.MapInfo();
        mapInfo.setId(map.getId());
        mapInfo.setName(map.getName());
        // 这里仅返回图片ID，真实 URL 可由前端拼接
        mapInfo.setBackImgUrl(map.getBackImgId());
        resp.setMap(mapInfo);

        resp.setDevices(mapDevices.stream().map(d -> {
            MapNavigateResponse.MapDeviceInfo md = new MapNavigateResponse.MapDeviceInfo();
            md.setId(d.getId());
            md.setDeviceId(d.getDeviceId());
            md.setDeviceName(d.getText());
            md.setDeviceType(d.getType());
            md.setX(d.getX() != null ? d.getX().doubleValue() : null);
            md.setY(d.getY() != null ? d.getY().doubleValue() : null);
            SasDevice device = sasDeviceMapper.selectById(d.getDeviceId());
            md.setStatus(isOnline(device) ? "online" : "offline");
            return md;
        }).collect(Collectors.toList()));

        return resp;
    }

    @Override
    public MapDeviceDetailResponse deviceDetail(String deviceId) {
        SasDevice device = sasDeviceMapper.selectById(deviceId);
        if (device == null) {
            return null;
        }
        MapDeviceDetailResponse resp = new MapDeviceDetailResponse();
        resp.setId(device.getId());
        resp.setDeviceId(device.getDeviceId());
        resp.setName(device.getDeviceId());
        resp.setType("camera");
        resp.setIpAddr(device.getIpAddr());
        resp.setStatus(isOnline(device) ? "online" : "offline");
        if (device.getIpAddr() != null) {
            resp.setLiveUrl("rtsp://" + device.getIpAddr() + ":554/stream");
        }
        resp.setPlaybackUrl("http://192.168.10.151/playback");
        resp.setAddress(device.getAddress());
        return resp;
    }

    private boolean isOnline(SasDevice d) {
        if (d == null || d.getTriggerTime() == null) {
            return false;
        }
        long seconds = ChronoUnit.SECONDS.between(d.getTriggerTime(), LocalDateTime.now());
        return seconds <= 60;
    }
}

