package com.fuyuanshen.web.service.device.status;

import com.fuyuanshen.app.domain.dto.AppRealTimeStatusDto;
import com.fuyuanshen.common.core.utils.StringUtils;
import com.fuyuanshen.common.redis.utils.RedisUtils;
import com.fuyuanshen.global.mqtt.listener.domain.FunctionAccessStatus;
import com.fuyuanshen.web.service.device.status.base.DeviceStatusRule;
import com.fuyuanshen.web.service.device.status.constants.DeviceTypeConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import static com.fuyuanshen.common.core.constant.GlobalConstants.FUNCTION_ACCESS_KEY;
import static com.fuyuanshen.common.core.constant.GlobalConstants.FUNCTION_ACCESS_TIMEOUT_KEY;

// 上传开机图片
@Slf4j
@Component
public class FunctionAccessStatusRule implements DeviceStatusRule {
    @Override
    public String getCommandType() {
        return "FunctionAccessStatusRule";
    }

    @Override
    public boolean supports(String deviceType) {
        return true; // 适用于所有设备类型
    }
    
    @Override
    public Map<String, Object> getStatus(AppRealTimeStatusDto dto) {
        Map<String, Object> status = new HashMap<>();
        String functionAccess = RedisUtils.getCacheObject(
                FUNCTION_ACCESS_KEY + dto.getDeviceImei());
        log.info("FunctionAccessStatusRule:{}",functionAccess);
        if(StringUtils.isBlank(functionAccess)){
            String timeOut = RedisUtils.getCacheObject(FUNCTION_ACCESS_TIMEOUT_KEY + dto.getDeviceImei());
            if("TIMEOUT".equals(timeOut)){
                status.put("functionAccess", FunctionAccessStatus.TIMEOUT.getCode());
                RedisUtils.deleteObject(FUNCTION_ACCESS_TIMEOUT_KEY + dto.getDeviceImei());
            }else{
                status.put("functionAccess", FunctionAccessStatus.OK.getCode());
            }
        }else{
            status.put("functionAccess", functionAccess);
        }
        return status;
    }
}