package com.ycwl.basic.puzzle.fill.datasource;

import com.fasterxml.jackson.databind.JsonNode;
import com.ycwl.basic.mapper.SourceMapper;
import com.ycwl.basic.model.pc.source.entity.SourceEntity;
import com.ycwl.basic.puzzle.fill.enums.DataSourceType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 设备缩略图数据源策略
 * 根据deviceIndex指定第N个设备的缩略图
 */
@Slf4j
@Component
public class DeviceThumbImageDataSourceStrategy implements DataSourceStrategy {

    @Autowired
    private SourceMapper sourceMapper;

    @Override
    public String resolve(JsonNode sourceFilter, String sortStrategy, DataSourceContext context) {
        try {
            // 默认type=2(图片)
            Integer type = 2;
            if (sourceFilter != null && sourceFilter.has("type")) {
                type = sourceFilter.get("type").asInt();
            }

            // 获取deviceIndex
            Integer deviceIndex = 0;
            if (sourceFilter != null && sourceFilter.has("deviceIndex")) {
                deviceIndex = sourceFilter.get("deviceIndex").asInt();
            }

            // 使用默认策略
            if (sortStrategy == null || sortStrategy.isEmpty()) {
                sortStrategy = "LATEST";
            }

            // 1. 检查是否有过滤后的机位列表
            Map<String, Object> extra = context.getExtra();
            if (extra != null && extra.containsKey("filteredDeviceIds")) {
                @SuppressWarnings("unchecked")
                List<Long> filteredDeviceIds = (List<Long>) extra.get("filteredDeviceIds");

                if (filteredDeviceIds != null && !filteredDeviceIds.isEmpty()) {
                    // 使用过滤后的机位列表
                    if (deviceIndex >= filteredDeviceIds.size()) {
                        log.warn("deviceIndex[{}]超出过滤后的机位列表范围, 最大索引={}",
                                deviceIndex, filteredDeviceIds.size() - 1);
                        return null;
                    }

                    Long targetDeviceId = filteredDeviceIds.get(deviceIndex);
                    log.debug("使用过滤后的机位列表, deviceIndex={}, targetDeviceId={}",
                            deviceIndex, targetDeviceId);

                    SourceEntity source = sourceMapper.getSourceByFaceAndDeviceId(
                            context.getFaceId(),
                            targetDeviceId,
                            type,
                            sortStrategy
                    );

                    if (source != null) {
                        String thumbUrl = source.getThumbUrl();
                        log.debug("解析DEVICE_THUMB_IMAGE成功(过滤模式), faceId={}, deviceId={}, type={}, thumbUrl={}",
                                context.getFaceId(), targetDeviceId, type, thumbUrl);
                        return thumbUrl;
                    }
                    return null;
                }
            }

            // 2. 降级到原有逻辑(使用deviceIndex直接查询)
            SourceEntity source = sourceMapper.getSourceByFaceAndDeviceIndex(
                    context.getFaceId(),
                    deviceIndex,
                    type,
                    sortStrategy
            );

            if (source != null) {
                String thumbUrl = source.getThumbUrl();
                log.debug("解析DEVICE_THUMB_IMAGE成功(索引模式), faceId={}, deviceIndex={}, type={}, thumbUrl={}",
                        context.getFaceId(), deviceIndex, type, thumbUrl);
                return thumbUrl;
            }
        } catch (Exception e) {
            log.error("解析DEVICE_THUMB_IMAGE异常, faceId={}", context.getFaceId(), e);
        }
        return null;
    }

    @Override
    public String getSupportedType() {
        return DataSourceType.DEVICE_THUMB_IMAGE.getCode();
    }
}
