/*
 * 文 件 名:  MockPileAutoConfiguration
 * 版    权:  华设设计集团股份有限公司
 * 描    述:  Mock充电桩自动配置
 * 修 改 人:  lvwenbin
 * 修改时间:  2025/12/23
 * 修改内容:  添加缺失的 @Bean 注解
 */
package com.ruoyi.ems.charging.mock.config;

import com.ruoyi.ems.charging.mock.simulator.MockPileClientManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;

/**
 * Mock充电桩自动配置
 * 通过配置文件控制是否启用Mock客户端
 *
 * @author lvwenbin
 * @version [版本号, 2025/12/23]
 */
@Configuration
@ConditionalOnProperty(name = "adapter.charging-pile.mock.enabled", havingValue = "true")
public class MockPileAutoConfiguration {

    private static final Logger log = LoggerFactory.getLogger(MockPileAutoConfiguration.class);

    @Value("${adapter.charging-pile.mock.server-host:127.0.0.1}")
    private String serverHost;

    @Value("${adapter.charging-pile.mock.server-port:8234}")
    private int serverPort;

    @Value("${adapter.charging-pile.mock.heartbeat-interval:10}")
    private int heartbeatInterval;

    @Value("${adapter.charging-pile.mock.charging-report-interval:5}")
    private int chargingReportInterval;

    @Value("${adapter.charging-pile.mock.idle-report-interval:300}")
    private int idleReportInterval;

    @Value("${adapter.charging-pile.mock.charging-cycle:120}")
    private int chargingCycle;

    @Value("${adapter.charging-pile.mock.charging-duration:120}")
    private int chargingDuration;

    @Value("${adapter.charging-pile.mock.auto-charging:true}")
    private boolean autoCharging;

    @Value("${adapter.charging-pile.mock.config-type:simple}")
    private String configType;

    private MockPileClientManager clientManager;

    /**
     * 修复: 添加 @Bean 注解，使Spring能够正确管理这个Bean
     */
    @Bean
    public MockPileClientManager mockPileClientManager() {
        log.info("初始化Mock充电桩客户端管理器...");

        // 根据配置类型创建配置
        MockPileConfig config;
        if ("highway".equalsIgnoreCase(configType)) {
            config = MockPileConfig.createHighwayServiceAreaConfig();
            log.info("使用高速服务区配置: 4台主机, 30把枪");
        } else {
            config = MockPileConfig.createSimpleTestConfig();
            log.info("使用简单测试配置: 1台主机, 1把枪");
        }

        // 应用配置参数
        config.setServerHost(serverHost);
        config.setServerPort(serverPort);
        config.setHeartbeatIntervalSeconds(heartbeatInterval);
        config.setChargingReportIntervalSeconds(chargingReportInterval);
        config.setIdleReportIntervalSeconds(idleReportInterval);
        config.setChargingCycleSeconds(chargingCycle);
        config.setChargingDurationSeconds(chargingDuration);
        config.setAutoChargingEnabled(autoCharging);

        clientManager = new MockPileClientManager(config);

        return clientManager;
    }

    @PreDestroy
    public void destroy() {
        if (clientManager != null) {
            log.info("停止Mock充电桩客户端管理器...");
            clientManager.stopAll();
        }
    }
}