/*
 * 文 件 名:  MockPileConfig
 * 版    权:  华设设计集团股份有限公司
 * 描    述:  Mock充电桩配置
 * 修 改 人:  lvwenbin
 * 修改时间:  2025/12/23
 * 修改内容:  修正桩编码为14位BCD码格式，与数据库attr_value保持一致
 */
package com.ruoyi.ems.charging.mock.config;

import java.util.ArrayList;
import java.util.List;

/**
 * Mock充电桩配置
 * 定义模拟的充电桩主机和充电枪配置
 * 编码规则 (符合PDF协议规范):
 *   桩编码(pileCode): 7字节BCD码 = 14位数字
 *   枪号(gunNo): 1字节BCD码 = 2位数字 (01-08)
 * 桩编码设计: 321283 + AA + BBBB + 01
 *   321283: 泰兴市行政区划码前6位
 *   AA: 服务区(01=北区, 02=南区)
 *   BBBB: 主机序号(0001, 0002)
 *   01: 校验位
 *
 * @author lvwenbin
 * @version [版本号, 2025/12/23]
 */
public class MockPileConfig {

    /**
     * 桩编码常量 - 与数据库保持一致
     */
    public static final String PILE_CODE_NORTH_HOST_01 = "32128301000101";
    public static final String PILE_CODE_NORTH_HOST_02 = "32128301000201";
    public static final String PILE_CODE_SOUTH_HOST_01 = "32128302000101";
    public static final String PILE_CODE_SOUTH_HOST_02 = "32128302000201";

    /**
     * 服务器地址
     */
    private String serverHost = "127.0.0.1";

    /**
     * 服务器端口
     */
    private int serverPort = 8234;

    /**
     * 充电桩主机配置列表
     */
    private List<PileHostConfig> pileHosts = new ArrayList<>();

    /**
     * 心跳间隔(秒)
     */
    private int heartbeatIntervalSeconds = 10;

    /**
     * 实时数据上报间隔 - 空闲状态(秒)
     */
    private int idleReportIntervalSeconds = 300;

    /**
     * 实时数据上报间隔 - 充电状态(秒)
     */
    private int chargingReportIntervalSeconds = 5;

    /**
     * 模拟充电周期(秒) - 多久触发一次充电开始
     */
    private int chargingCycleSeconds = 120;

    /**
     * 模拟充电时长(秒)
     */
    private int chargingDurationSeconds = 120;

    /**
     * 是否启用自动充电模拟
     */
    private boolean autoChargingEnabled = true;

    /**
     * 重连间隔(秒)
     */
    private int reconnectIntervalSeconds = 5;

    /**
     * 创建默认的高速服务区配置
     * 南北两侧,每侧2台充电主机
     * 主机1: 1台600kW水冷枪 + 6台250kW风冷枪 = 7枪
     * 主机2: 1台600kW水冷枪 + 7台250kW风冷枪 = 8枪
     *
     * 桩编码使用14位BCD码格式，与数据库adm_ems_obj_attr_value表保持一致
     */
    public static MockPileConfig createHighwayServiceAreaConfig() {
        MockPileConfig config = new MockPileConfig();

        // 北区-主机1: 7枪 (1*600kW + 6*250kW)
        // 对应数据库: W2-B-CHARGING-HOST-01, pileCode=32128301000101
        PileHostConfig northHost1 = new PileHostConfig();
        northHost1.setPileCode(PILE_CODE_NORTH_HOST_01);  // 32128301000101
        northHost1.setGunCount(7);
        northHost1.setDescription("北区-主机1");
        // 枪号使用2位格式: 01-07
        northHost1.addGun(new GunConfig("01", 600, GunConfig.GunType.WATER_COOLED));
        for (int i = 2; i <= 7; i++) {
            northHost1.addGun(new GunConfig(String.format("%02d", i), 250, GunConfig.GunType.AIR_COOLED));
        }
        config.getPileHosts().add(northHost1);

        // 北区-主机2: 8枪 (1*600kW + 7*250kW)
        // 对应数据库: W2-B-CHARGING-HOST-02, pileCode=32128301000201
        PileHostConfig northHost2 = new PileHostConfig();
        northHost2.setPileCode(PILE_CODE_NORTH_HOST_02);  // 32128301000201
        northHost2.setGunCount(8);
        northHost2.setDescription("北区-主机2");
        // 枪号使用2位格式: 01-08
        northHost2.addGun(new GunConfig("01", 600, GunConfig.GunType.WATER_COOLED));
        for (int i = 2; i <= 8; i++) {
            northHost2.addGun(new GunConfig(String.format("%02d", i), 250, GunConfig.GunType.AIR_COOLED));
        }
        config.getPileHosts().add(northHost2);

        // 南区-主机1: 7枪 (1*600kW + 6*250kW)
        // 对应数据库: W2-N-CHARGING-HOST-01, pileCode=32128302000101
        PileHostConfig southHost1 = new PileHostConfig();
        southHost1.setPileCode(PILE_CODE_SOUTH_HOST_01);  // 32128302000101
        southHost1.setGunCount(7);
        southHost1.setDescription("南区-主机1");
        // 枪号使用2位格式: 01-07
        southHost1.addGun(new GunConfig("01", 600, GunConfig.GunType.WATER_COOLED));
        for (int i = 2; i <= 7; i++) {
            southHost1.addGun(new GunConfig(String.format("%02d", i), 250, GunConfig.GunType.AIR_COOLED));
        }
        config.getPileHosts().add(southHost1);

        // 南区-主机2: 8枪 (1*600kW + 7*250kW)
        // 对应数据库: W2-N-CHARGING-HOST-02, pileCode=32128302000201
        PileHostConfig southHost2 = new PileHostConfig();
        southHost2.setPileCode(PILE_CODE_SOUTH_HOST_02);  // 32128302000201
        southHost2.setGunCount(8);
        southHost2.setDescription("南区-主机2");
        // 枪号使用2位格式: 01-08
        southHost2.addGun(new GunConfig("01", 600, GunConfig.GunType.WATER_COOLED));
        for (int i = 2; i <= 8; i++) {
            southHost2.addGun(new GunConfig(String.format("%02d", i), 250, GunConfig.GunType.AIR_COOLED));
        }
        config.getPileHosts().add(southHost2);

        return config;
    }

    /**
     * 创建简单的测试配置(单主机单枪)
     * 使用北区主机1的桩编码
     */
    public static MockPileConfig createSimpleTestConfig() {
        MockPileConfig config = new MockPileConfig();

        PileHostConfig host = new PileHostConfig();
        host.setPileCode(PILE_CODE_NORTH_HOST_01);  // 32128301000101
        host.setGunCount(1);
        host.setDescription("测试主机");
        host.addGun(new GunConfig("01", 120, GunConfig.GunType.AIR_COOLED));
        config.getPileHosts().add(host);

        return config;
    }

    /**
     * 格式化枪号为2位BCD格式
     * @param gunNo 枪号 (1-99)
     * @return 2位格式枪号 (01-99)
     */
    public static String formatGunNo(int gunNo) {
        return String.format("%02d", gunNo);
    }

    /**
     * 格式化枪号为2位BCD格式
     * @param gunNo 枪号字符串
     * @return 2位格式枪号
     */
    public static String formatGunNo(String gunNo) {
        try {
            return String.format("%02d", Integer.parseInt(gunNo));
        } catch (NumberFormatException e) {
            return gunNo;
        }
    }

    // Getters and Setters
    public String getServerHost() {
        return serverHost;
    }

    public void setServerHost(String serverHost) {
        this.serverHost = serverHost;
    }

    public int getServerPort() {
        return serverPort;
    }

    public void setServerPort(int serverPort) {
        this.serverPort = serverPort;
    }

    public List<PileHostConfig> getPileHosts() {
        return pileHosts;
    }

    public void setPileHosts(List<PileHostConfig> pileHosts) {
        this.pileHosts = pileHosts;
    }

    public int getHeartbeatIntervalSeconds() {
        return heartbeatIntervalSeconds;
    }

    public void setHeartbeatIntervalSeconds(int heartbeatIntervalSeconds) {
        this.heartbeatIntervalSeconds = heartbeatIntervalSeconds;
    }

    public int getIdleReportIntervalSeconds() {
        return idleReportIntervalSeconds;
    }

    public void setIdleReportIntervalSeconds(int idleReportIntervalSeconds) {
        this.idleReportIntervalSeconds = idleReportIntervalSeconds;
    }

    public int getChargingReportIntervalSeconds() {
        return chargingReportIntervalSeconds;
    }

    public void setChargingReportIntervalSeconds(int chargingReportIntervalSeconds) {
        this.chargingReportIntervalSeconds = chargingReportIntervalSeconds;
    }

    public int getChargingCycleSeconds() {
        return chargingCycleSeconds;
    }

    public void setChargingCycleSeconds(int chargingCycleSeconds) {
        this.chargingCycleSeconds = chargingCycleSeconds;
    }

    public int getChargingDurationSeconds() {
        return chargingDurationSeconds;
    }

    public void setChargingDurationSeconds(int chargingDurationSeconds) {
        this.chargingDurationSeconds = chargingDurationSeconds;
    }

    public boolean isAutoChargingEnabled() {
        return autoChargingEnabled;
    }

    public void setAutoChargingEnabled(boolean autoChargingEnabled) {
        this.autoChargingEnabled = autoChargingEnabled;
    }

    public int getReconnectIntervalSeconds() {
        return reconnectIntervalSeconds;
    }

    public void setReconnectIntervalSeconds(int reconnectIntervalSeconds) {
        this.reconnectIntervalSeconds = reconnectIntervalSeconds;
    }

    /**
     * 充电桩主机配置
     */
    public static class PileHostConfig {
        /**
         * 桩编号 (BCD码 7字节, 14位数字)
         */
        private String pileCode;

        /**
         * 枪数量
         */
        private int gunCount;

        /**
         * 描述
         */
        private String description;

        /**
         * 桩类型: 0-直流, 1-交流
         */
        private byte pileType = 0;

        /**
         * 软件版本
         */
        private String softwareVersion = "V1.0.0";

        /**
         * 协议版本 (乘10)
         */
        private int protocolVersion = 20;

        /**
         * 网络类型: 0-SIM, 1-LAN, 2-WAN
         */
        private byte networkType = 1;

        /**
         * 枪配置列表
         */
        private List<GunConfig> guns = new ArrayList<>();

        public void addGun(GunConfig gun) {
            guns.add(gun);
        }

        // Getters and Setters
        public String getPileCode() {
            return pileCode;
        }

        public void setPileCode(String pileCode) {
            this.pileCode = pileCode;
        }

        public int getGunCount() {
            return gunCount;
        }

        public void setGunCount(int gunCount) {
            this.gunCount = gunCount;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public byte getPileType() {
            return pileType;
        }

        public void setPileType(byte pileType) {
            this.pileType = pileType;
        }

        public String getSoftwareVersion() {
            return softwareVersion;
        }

        public void setSoftwareVersion(String softwareVersion) {
            this.softwareVersion = softwareVersion;
        }

        public int getProtocolVersion() {
            return protocolVersion;
        }

        public void setProtocolVersion(int protocolVersion) {
            this.protocolVersion = protocolVersion;
        }

        public byte getNetworkType() {
            return networkType;
        }

        public void setNetworkType(byte networkType) {
            this.networkType = networkType;
        }

        public List<GunConfig> getGuns() {
            return guns;
        }

        public void setGuns(List<GunConfig> guns) {
            this.guns = guns;
        }
    }

    /**
     * 充电枪配置
     */
    public static class GunConfig {
        /**
         * 枪号 (2位BCD格式: 01-99)
         */
        private String gunNo;

        /**
         * 额定功率 (kW)
         */
        private int ratedPower;

        /**
         * 枪类型
         */
        private GunType gunType;

        public GunConfig() {
        }

        public GunConfig(String gunNo, int ratedPower, GunType gunType) {
            this.gunNo = gunNo;
            this.ratedPower = ratedPower;
            this.gunType = gunType;
        }

        public enum GunType {
            /**
             * 水冷枪
             */
            WATER_COOLED,
            /**
             * 风冷枪
             */
            AIR_COOLED
        }

        // Getters and Setters
        public String getGunNo() {
            return gunNo;
        }

        public void setGunNo(String gunNo) {
            this.gunNo = gunNo;
        }

        public int getRatedPower() {
            return ratedPower;
        }

        public void setRatedPower(int ratedPower) {
            this.ratedPower = ratedPower;
        }

        public GunType getGunType() {
            return gunType;
        }

        public void setGunType(GunType gunType) {
            this.gunType = gunType;
        }
    }
}