package com.ycwl.basic.repository;

import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
import com.ycwl.basic.integration.common.response.PageResponse;
import com.ycwl.basic.integration.scenic.service.ScenicIntegrationService;
import com.ycwl.basic.integration.scenic.service.ScenicConfigIntegrationService;
import com.ycwl.basic.integration.scenic.dto.config.ScenicConfigV2DTO;
import com.ycwl.basic.mapper.MpConfigMapper;
import com.ycwl.basic.mapper.MpNotifyConfigMapper;
import com.ycwl.basic.model.pc.mp.MpConfigEntity;
import com.ycwl.basic.model.pc.mp.MpNotifyConfigEntity;
import com.ycwl.basic.model.pc.mp.ScenicMpNotifyVO;
import com.ycwl.basic.model.pc.scenic.entity.ScenicEntity;
import com.ycwl.basic.model.pc.scenic.req.ScenicReqQuery;
import com.ycwl.basic.utils.JacksonUtil;
import com.ycwl.basic.integration.common.manager.ScenicConfigManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Objects;

@Component
public class ScenicRepository {
    @Autowired
    private MpConfigMapper mpConfigMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private ScenicIntegrationService scenicIntegrationService;
    @Autowired
    private ScenicConfigIntegrationService scenicConfigIntegrationService;

    public static final String SCENIC_MP_CACHE_KEY = "scenic:%s:mp";
    public static final String SCENIC_MP_NOTIFY_CACHE_KEY = "scenic:%s:mpNotify";

    public ScenicV2DTO getScenicBasic(Long id) {
        ScenicV2DTO scenicDTO = scenicIntegrationService.getScenic(id);
        return scenicDTO;
    }

    public ScenicEntity getScenic(Long id) {
        // 分别获取景区基础信息和配置信息
        ScenicV2DTO scenicBasic = scenicIntegrationService.getScenic(id);
        ScenicConfigManager configManager = getScenicConfigManager(id);

        ScenicEntity scenicEntity = convertToScenicEntity(scenicBasic, configManager);
        return scenicEntity;
    }

    public MpConfigEntity getScenicMpConfig(Long scenicId) {
        if (redisTemplate.hasKey(String.format(SCENIC_MP_CACHE_KEY, scenicId))) {
            return JacksonUtil.parseObject(redisTemplate.opsForValue().get(String.format(SCENIC_MP_CACHE_KEY, scenicId)), MpConfigEntity.class);
        }
        ScenicEntity scenic = getScenic(scenicId);
        MpConfigEntity mpConfigEntity = mpConfigMapper.selectById(scenic.getMpId());
        if (mpConfigEntity != null) {
            redisTemplate.opsForValue().set(String.format(SCENIC_MP_CACHE_KEY, scenicId), JacksonUtil.toJSONString(mpConfigEntity));
        }
        return mpConfigEntity;
    }

    public List<ScenicV2DTO> list(ScenicReqQuery scenicReqQuery) {
        try {
            // 将 ScenicReqQuery 参数转换为 zt-scenic 服务需要的参数
            Integer page = scenicReqQuery.getPageNum();
            Integer pageSize = scenicReqQuery.getPageSize();
            Integer status = null;
            if (scenicReqQuery.getStatus() != null) {
                status = Integer.valueOf(scenicReqQuery.getStatus());
            }
            String name = scenicReqQuery.getName();

            // 调用 zt-scenic 服务的 list 方法
            PageResponse<ScenicV2DTO> response = scenicIntegrationService.listScenics(page, pageSize, status, name, null);

            // 将 ScenicV2DTO 列表转换为 ScenicEntity 列表
            if (response != null && response.getList() != null) {
                return response.getList();
            }
            return new java.util.ArrayList<>();
        } catch (Exception e) {
            // 如果调用失败，返回空列表
            return new java.util.ArrayList<>();
        }
    }

    private ScenicEntity convertToScenicEntity(ScenicV2DTO dto, ScenicConfigManager configManager) {
        if (dto == null) {
            return null;
        }
        ScenicEntity entity = new ScenicEntity();
        entity.setId(Long.parseLong(dto.getId()));
        entity.setName(dto.getName());
        entity.setMpId(dto.getMpId());
        entity.setStatus(dto.getStatus().toString());
        entity.setAddress(configManager.getString("address"));
        entity.setArea(configManager.getString("area"));
        entity.setCity(configManager.getString("city"));
        entity.setProvince(configManager.getString("province"));
        entity.setLatitude(configManager.getBigDecimal("latitude"));
        entity.setLongitude(configManager.getBigDecimal("longitude"));
        entity.setRadius(configManager.getBigDecimal("radius"));
        entity.setPhone(configManager.getString("phone"));
        entity.setLogoUrl(configManager.getString("logo_url"));
        entity.setCoverUrl(configManager.getString("cover_url"));
        entity.setKfCodeUrl(configManager.getString("kf_code_url"));
        return entity;
    }

    /**
     * 获取景区配置管理器
     * 
     * @param scenicId 景区ID
     * @return ScenicConfigManager实例，如果获取失败返回null
     */
    public ScenicConfigManager getScenicConfigManager(Long scenicId) {
        try {
            List<ScenicConfigV2DTO> configList = scenicConfigIntegrationService.listConfigs(scenicId);
            return new ScenicConfigManager(Objects.requireNonNullElse(configList, Collections.emptyList()));
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 批量获取景区名称
     * @param scenicIds 景区ID列表
     * @return 景区ID到景区名称的映射
     */
    public Map<Long, String> batchGetScenicNames(List<Long> scenicIds) {
        if (scenicIds == null || scenicIds.isEmpty()) {
            return new HashMap<>();
        }

        Map<Long, String> result = new HashMap<>();
        for (Long scenicId : scenicIds) {
            try {
                ScenicV2DTO scenic = getScenicBasic(scenicId);
                if (scenic != null) {
                    result.put(scenicId, scenic.getName());
                }
            } catch (Exception e) {
                // 获取失败时使用默认值
                result.put(scenicId, "未知景区");
            }
        }
        return result;
    }

    /**
     * 批量获取景区完整信息
     * @param scenicIds 景区ID列表
     * @return 景区ID到景区实体的映射
     */
    public Map<Long, ScenicEntity> batchGetScenics(List<Long> scenicIds) {
        if (scenicIds == null || scenicIds.isEmpty()) {
            return new HashMap<>();
        }

        Map<Long, ScenicEntity> result = new HashMap<>();
        for (Long scenicId : scenicIds) {
            try {
                ScenicEntity scenic = getScenic(scenicId);
                if (scenic != null) {
                    result.put(scenicId, scenic);
                }
            } catch (Exception e) {
                // 获取失败时记录日志但不中断处理
                System.err.println("获取景区信息失败: " + scenicId + ", 错误: " + e.getMessage());
            }
        }
        return result;
    }

    /**
     * 批量获取景区基础信息DTO
     * @param scenicIds 景区ID列表
     * @return 景区ID到景区DTO的映射
     */
    public Map<Long, ScenicV2DTO> batchGetScenicBasics(List<Long> scenicIds) {
        if (scenicIds == null || scenicIds.isEmpty()) {
            return new HashMap<>();
        }

        Map<Long, ScenicV2DTO> result = new HashMap<>();
        for (Long scenicId : scenicIds) {
            try {
                ScenicV2DTO scenic = getScenicBasic(scenicId);
                if (scenic != null) {
                    result.put(scenicId, scenic);
                }
            } catch (Exception e) {
                // 获取失败时记录日志但不中断处理
                System.err.println("获取景区基础信息失败: " + scenicId + ", 错误: " + e.getMessage());
            }
        }
        return result;
    }

}
