package com.dji.sample.manage.service.impl;

import com.dji.sample.component.mqtt.model.EventsReceiver;
import com.dji.sample.component.redis.RedisConst;
import com.dji.sample.component.redis.RedisOpsUtils;
import com.dji.sdk.common.AMap;
import com.dji.sample.manage.model.dto.DeviceDTO;
import com.dji.sample.manage.service.ICapacityCameraService;
import com.dji.sample.manage.service.IDeviceRedisService;
import com.dji.sample.map.model.dto.FlightTaskDTO;
import com.dji.sample.map.model.dto.FlightTrackDTO;
import com.dji.sample.map.model.entity.FlightTrackEntity;
import com.dji.sdk.cloudapi.firmware.OtaProgress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author sean
 * @version 1.4
 * @date 2023/3/21
 */
@Service
public class DeviceRedisServiceImpl implements IDeviceRedisService {

    @Autowired
    private ICapacityCameraService capacityCameraService;

    @Override
    public Boolean checkDeviceOnline(String sn) {
        String key = RedisConst.DEVICE_ONLINE_PREFIX + sn;
        return RedisOpsUtils.checkExist(key) && RedisOpsUtils.getExpire(key) > 0;
    }

    @Override
    public Optional<DeviceDTO> getDeviceOnline(String sn) {
        return Optional.ofNullable((DeviceDTO) RedisOpsUtils.get(RedisConst.DEVICE_ONLINE_PREFIX + sn));
    }

    @Override
    public void setDeviceOnline(DeviceDTO device) {
        RedisOpsUtils.setWithExpire(RedisConst.DEVICE_ONLINE_PREFIX + device.getDeviceSn(), device, RedisConst.DEVICE_ALIVE_SECOND);
    }

    @Override
    public Boolean delDeviceOnline(String sn) {
        return RedisOpsUtils.del(RedisConst.DEVICE_ONLINE_PREFIX + sn);
    }

    @Override
    public void setDeviceOsd(String sn, Object data) {
        RedisOpsUtils.setWithExpire(RedisConst.OSD_PREFIX + sn, data, RedisConst.DEVICE_ALIVE_SECOND);
    }

    @Override
    public <T> Optional<T> getDeviceOsd(String sn, Class<T> clazz) {
        return Optional.ofNullable(clazz.cast(RedisOpsUtils.get(RedisConst.OSD_PREFIX + sn)));
    }

    @Override
    public Optional<Object> getDeviceOsd(String sn) {
        return Optional.ofNullable(RedisOpsUtils.get(RedisConst.OSD_PREFIX + sn));
    }

    @Override
    public Boolean delDeviceOsd(String sn) {
        return RedisOpsUtils.del(RedisConst.OSD_PREFIX + sn);
    }

    @Override
    public void setFirmwareUpgrading(String sn, EventsReceiver<OtaProgress> events) {
        RedisOpsUtils.setWithExpire(RedisConst.FIRMWARE_UPGRADING_PREFIX + sn, events, RedisConst.DEVICE_ALIVE_SECOND * 20);
    }

    @Override
    public Optional<EventsReceiver<OtaProgress>> getFirmwareUpgradingProgress(String sn) {
        return Optional.ofNullable((EventsReceiver<OtaProgress>) RedisOpsUtils.get(RedisConst.FIRMWARE_UPGRADING_PREFIX + sn));
    }

    @Override
    public Boolean delFirmwareUpgrading(String sn) {
        return RedisOpsUtils.del(RedisConst.FIRMWARE_UPGRADING_PREFIX + sn);
    }

    @Override
    public void addEndHmsKeys(String sn, String... keys) {
        RedisOpsUtils.listRPush(RedisConst.HMS_PREFIX + sn, keys);
    }

    @Override
    public Set<String> getAllHmsKeys(String sn) {
        return RedisOpsUtils.listGetAll(RedisConst.HMS_PREFIX + sn).stream()
                .map(String::valueOf).collect(Collectors.toSet());
    }

    @Override
    public Boolean delHmsKeysBySn(String sn) {
        return RedisOpsUtils.del(RedisConst.HMS_PREFIX + sn);
    }

    @Override
    public void gatewayOffline(String gatewaySn) {
        delDeviceOnline(gatewaySn);
        delHmsKeysBySn(gatewaySn);
        capacityCameraService.deleteCapacityCameraByDeviceSn(gatewaySn);
    }

    @Override
    public void subDeviceOffline(String deviceSn) {
        delDeviceOnline(deviceSn);
        delDeviceOsd(deviceSn);
        delHmsKeysBySn(deviceSn);
        capacityCameraService.deleteCapacityCameraByDeviceSn(deviceSn);
    }

    @Override
    public void setDroneFlightTask(String deviceSn, FlightTaskDTO flightTaskDTO) {
        RedisOpsUtils.setWithExpire(RedisConst.DRONE_FLIGHT_TASK + deviceSn, flightTaskDTO, RedisConst.DRONE_HOME_ALIVE_SECOND);
    }

    @Override
    public Optional<FlightTaskDTO> getDroneFlightTask(String sn) {
        return Optional.ofNullable((FlightTaskDTO) RedisOpsUtils.get(RedisConst.DRONE_FLIGHT_TASK + sn));
    }

    @Override
    public Boolean delDroneFlightTask(String sn) {
        return RedisOpsUtils.del(RedisConst.DRONE_FLIGHT_TASK + sn);
    }

    @Override
    public void setDroneFlightTaskLock(String deviceSn) {
        RedisOpsUtils.setWithExpire(RedisConst.DRONE_FLIGHT_TASK_LOCK + deviceSn, deviceSn, RedisConst.DEVICE_ALIVE_SECOND);
    }

    @Override
    public Optional<String> getDroneFlightTaskLock(String sn) {
        return Optional.ofNullable((String) RedisOpsUtils.get(RedisConst.DRONE_FLIGHT_TASK_LOCK + sn));
    }

    @Override
    public Boolean delDroneFlightTaskLock(String sn) {
        return RedisOpsUtils.del(RedisConst.DRONE_FLIGHT_TASK_LOCK + sn);
    }


    @Override
    public void setLastDroneTrack(String deviceSn, FlightTrackEntity flightTrackEntity) {
        RedisOpsUtils.setWithExpire(RedisConst.DRONE_LAST_TRACK + deviceSn, flightTrackEntity, RedisConst.DEVICE_ALIVE_SECOND);
    }

    @Override
    public Optional<FlightTrackEntity> getLastDroneTrack(String sn) {
        return Optional.ofNullable((FlightTrackEntity) RedisOpsUtils.get(RedisConst.DRONE_LAST_TRACK + sn));
    }

    @Override
    public Boolean delLastDroneTrack(String sn) {
        return RedisOpsUtils.del(RedisConst.DRONE_LAST_TRACK + sn);
    }

    @Override
    public void setDroneHomeChange(String deviceSn, AMap homePoint) {
        RedisOpsUtils.setWithExpire(RedisConst.DRONE_HOME + deviceSn, homePoint, RedisConst.DRONE_HOME_ALIVE_SECOND);
    }

    @Override
    public Optional<AMap> getDroneHomeChange(String sn) {
        return Optional.ofNullable((AMap) RedisOpsUtils.get(RedisConst.DRONE_HOME + sn));
    }

    @Override
    public Boolean delDroneHomeChange(String sn) {
        return RedisOpsUtils.del(RedisConst.DRONE_HOME + sn);
    }

    @Override
    public void setDroneFlyTrack(String deviceSn, List<FlightTrackDTO> points) {
        RedisOpsUtils.setWithExpire(RedisConst.DRONE_FLY_TRACK + deviceSn, points, RedisConst.DRONE_HOME_ALIVE_SECOND);
    }

    @Override
    public Optional<List<FlightTrackDTO>> getDroneFlyTrack(String sn) {
        return Optional.ofNullable((List<FlightTrackDTO>) RedisOpsUtils.get(RedisConst.DRONE_FLY_TRACK + sn));
    }

    @Override
    public Boolean delDroneFlyTrack(String sn) {
        return RedisOpsUtils.del(RedisConst.DRONE_FLY_TRACK + sn);
    }

//    @Override
//    public void setDeviceStatus(String deviceSn, DeviceModelEnum deviceStatus) {
//        RedisOpsUtils.setWithExpire(RedisConst.DEVICE_STATUS + deviceSn, deviceStatus, RedisConst.DRONE_HOME_ALIVE_SECOND);
//    }
//
//    @Override
//    public Optional<DeviceStatusEnum> getDeviceStatus(String sn) {
//        return Optional.ofNullable((DeviceStatusEnum) RedisOpsUtils.get(RedisConst.DEVICE_STATUS + sn));
//    }
//
//    @Override
//    public Boolean delDeviceStatus(String sn) {
//        return RedisOpsUtils.del(RedisConst.DEVICE_STATUS + sn);
//    }


}
