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

import cn.hutool.json.JSONUtil;
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.List;
import java.util.Map;

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


@Slf4j
@Component
public class FunctionAccessBatchStatusRule implements DeviceStatusRule {
    @Override
    public String getCommandType() {
        return "FunctionAccessBatchStatusRule";
    }

    @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.getBatchId());
        log.info("FunctionAccessBatchStatusRule:{}",functionAccess);
        if(StringUtils.isBlank(functionAccess)){
            status.put("functionAccess", FunctionAccessStatus.OK.getCode());
            return status;
        }
        List<String> cachedDeviceImeiList = JSONUtil.toList(functionAccess, String.class);
        if(cachedDeviceImeiList.isEmpty()){
            status.put("functionAccess", FunctionAccessStatus.OK.getCode());
            return status;
        }
        for (String key : cachedDeviceImeiList) {
            String item = RedisUtils.getCacheObject(FUNCTION_ACCESS_KEY + key);
            if("ACTIVE".equals(item)){
                status.put("functionAccess", FunctionAccessStatus.ACTIVE.getCode());
                break;
            }else {
                status.put("functionAccess", FunctionAccessStatus.OK.getCode());
            }
//            if (StringUtils.isBlank(item)) {
//                String timeOut = RedisUtils.getCacheObject(FUNCTION_ACCESS_TIMEOUT_KEY + dto.getDeviceImei());
//                if ("TIMEOUT".equals(timeOut)) {
//                    status.put("functionAccess", FunctionAccessStatus.TIMEOUT.getCode());
//                    break;
//                } else {
//                    status.put("functionAccess", FunctionAccessStatus.OK.getCode());
//                }
//            } else {
//                if (!FunctionAccessStatus.OK.getCode().equals(item)) {
//                    status.put("functionAccess", FunctionAccessStatus.FAILED.getCode());
//                    break;
//                } else {
//                    status.put("functionAccess", FunctionAccessStatus.OK.getCode());
//                }
//            }
        }
        return status;
    }
}