package com.ycwl.basic.repository;

import com.ycwl.basic.utils.JacksonUtil;
import com.ycwl.basic.model.pc.renderWorker.entity.RenderWorkerEntity;
import com.ycwl.basic.model.task.req.ClientStatusReqVo;
import com.ycwl.basic.integration.render.dto.worker.RenderWorkerV2DTO;
import com.ycwl.basic.integration.render.service.RenderWorkerIntegrationService;
import com.ycwl.basic.integration.render.service.RenderWorkerConfigIntegrationService;
import com.ycwl.basic.integration.render.dto.config.RenderWorkerConfigV2DTO;
import com.ycwl.basic.integration.common.manager.RenderWorkerConfigManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RenderWorkerRepository {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    public static final String RENDER_WORKER_STATUS_CACHE_KEY = "render_worker:host_status:%s";

    @Autowired
    private RenderWorkerIntegrationService renderWorkerIntegrationService;
    
    @Autowired
    private RenderWorkerConfigIntegrationService renderWorkerConfigIntegrationService;

    public RenderWorkerEntity getWorkerByAccessKey(String accessKey) {
        RenderWorkerV2DTO workerDTO = renderWorkerIntegrationService.getWorkerByKey(accessKey);
        if (workerDTO == null) {
            return null;
        }
        RenderWorkerEntity renderWorker = convertToEntity(workerDTO);
        return renderWorker;
    }

    public RenderWorkerEntity getWorker(Long id) {
        RenderWorkerV2DTO workerDTO = renderWorkerIntegrationService.getWorker(id);
        if (workerDTO == null) {
            return null;
        }
        RenderWorkerEntity renderWorker = convertToEntity(workerDTO);
        return renderWorker;
    }

    public void setWorkerHostStatus(Long id, ClientStatusReqVo status) {
        String key = String.format(RENDER_WORKER_STATUS_CACHE_KEY, id);
        if (status == null) {
            return;
        }
        status.setUpdateAt(new Date());
        redisTemplate.opsForValue().set(key, JacksonUtil.toJSONString(status), 1, TimeUnit.HOURS);
    }

    public ClientStatusReqVo getWorkerHostStatus(Long id) {
        String key = String.format(RENDER_WORKER_STATUS_CACHE_KEY, id);
        if (redisTemplate.hasKey(key)) {
            String status = redisTemplate.opsForValue().get(key);
            return JacksonUtil.parseObject(status, ClientStatusReqVo.class);
        }
        return null;
    }

    private RenderWorkerEntity convertToEntity(RenderWorkerV2DTO dto) {
        if (dto == null) {
            return null;
        }
        RenderWorkerEntity entity = new RenderWorkerEntity();
        entity.setId(dto.getId());
        entity.setName(dto.getName());
        entity.setAccessKey(dto.getKey());
        entity.setStatus(dto.getIsActive());
        entity.setCreateAt(dto.getCreateTime());
        entity.setUpdateAt(dto.getUpdateTime());
        return entity;
    }

    /**
     * 获取渲染工作器配置管理器
     * 
     * @param workerId 工作器ID
     * @return RenderWorkerConfigManager实例，如果获取失败返回null
     */
    public RenderWorkerConfigManager getWorkerConfigManager(Long workerId) {
        try {
            List<RenderWorkerConfigV2DTO> configList = renderWorkerConfigIntegrationService.getWorkerConfigs(workerId);
            return new RenderWorkerConfigManager(configList);
        } catch (Exception e) {
            log.error("获取渲染工作器配置管理器失败, workerId: {}", workerId, e);
            return null;
        }
    }

}
