package com.fizz.business.constants.enums;

import lombok.Getter;

import java.util.*;

/**
 * 细粒度设备枚举
 * - position: 基准位置（单位：m）
 * - sectionType: 所属段（入口 / 工艺 / 出口）
 * - sourceType: 数据来源（ENTRY/FURNACE/COAT/EXIT）
 * - paramFields: 对应AppMeasure*Message中的字段
 */
@Getter
public enum DeviceEnum {
    // === 入口段 ===
    POR1(0, "1#开卷机", 0.0, SectionType.ENTRY, SourceType.ENTRY, Arrays.asList("tensionPorBr1", "stripSpeed")),
    POR2(1, "2#开卷机", 0.0, SectionType.ENTRY, SourceType.ENTRY, Arrays.asList("tensionPorBr2", "stripSpeed")),
    WELDER(2, "焊机", 4.98, SectionType.ENTRY, SourceType.ENTRY, Arrays.asList("weldStatus")),
    ENL1(3, "入口活套1", 19.04, SectionType.PROCESS, SourceType.ENTRY, Arrays.asList("celLength", "celCapacity", "tensionCel")),
    ENL2(4, "入口活套2", 167.09, SectionType.PROCESS, SourceType.ENTRY, Arrays.asList("celLength", "celCapacity", "tensionCel")),
    ENL3(5, "入口活套3", 198.19, SectionType.PROCESS, SourceType.ENTRY, Arrays.asList("celLength", "celCapacity", "tensionCel")),

    // === 工艺段 ===
    CLEAN(6, "清洗段", 264.803, SectionType.PROCESS, SourceType.FURNACE, Arrays.asList("cleaningVoltage", "cleaningCurrent", "alkaliConcentration", "alkaliTemperature")),
    FUR1(7, "退火炉-预热段", 302.837, SectionType.PROCESS, SourceType.FURNACE, Arrays.asList("phfExitStripTemp", "potTemperature", "gasConsumption")),
    FUR2(8, "退火炉-加热段", 381.057, SectionType.PROCESS, SourceType.FURNACE, Arrays.asList("rtfExitStripTemp", "zincPotPower")),
    FUR3(9, "退火炉-冷却段", 416.837, SectionType.PROCESS, SourceType.FURNACE, Arrays.asList("jcsExitStripTemp", "coolingTowerStripTemp")),
    FUR4(10, "退火炉-均衡段", 432.16, SectionType.PROCESS, SourceType.FURNACE, Arrays.asList("scsExitStripTemp")),
    POT(11, "锌锅", 442.994, SectionType.PROCESS, SourceType.COAT, Arrays.asList("scsExitStripTemp")),
    TOWER(12, "冷却塔", 563.594, SectionType.PROCESS, SourceType.COAT, Arrays.asList("scsExitStripTemp")),
    TM(13, "光整机", 586.529, SectionType.PROCESS, SourceType.COAT, Arrays.asList("tensionBr5Tm", "stripSpeedTmExit")),
    TL(14, "拉矫机", 612.909, SectionType.PROCESS, SourceType.COAT, Arrays.asList("tlElongation", "tensionTlBr7")),
    COAT(15, "后处理段", 712.699, SectionType.PROCESS, SourceType.COAT, Arrays.asList(
            "avrCoatingWeightTop","stdCoatingWeightTop","maxCoatingWeightTop","minCoatingWeightTop",
            "avrCoatingWeightBottom","stdCoatingWeightBottom","maxCoatingWeightBottom","minCoatingWeightBottom",
            "airKnifePressure","airKnifeFlow","airKnifeGap","stripSpeedTmExit","tensionBr5Tm",
            "tensionTmBr6","tensionBr5Br6","tmMask","tmElongation","rollForceOperator","rollForceDrive",
            "motorTorque","bendingForce","antiCrimpingRollMesh","billyRollMesh",
            "tensionTlBr7","tensionBr6Br7","tlFlag","tlElongation","levelingUnit1Mesh","levelingUnit2Mesh",
            "antiCrossBowUnitMesh","tensionBr7Br8","stripSpeedAfp","stripTempAfp"
    )),

    // === 出口段 ===
    CXL1(16, "出口活套1", 720.709, SectionType.EXIT, SourceType.EXIT, Arrays.asList("cxlLength", "cxlCapacity", "tensionCxl")),
    CXL2(17, "出口活套2", 888.789, SectionType.EXIT, SourceType.EXIT, Arrays.asList("cxlLength", "cxlCapacity", "tensionCxl")),
   // INS(18, "检查站", 940.561, SectionType.EXIT, SourceType.EXIT, Arrays.asList("inspectionStatus")),
    TR(19, "卷取机", 952.819, SectionType.EXIT, SourceType.EXIT, Arrays.asList("coilLength", "speedExitSection", "tensionBr9Tr")),
    EXC(20, "卸卷小车", 9999999.0, SectionType.EXIT, SourceType.EXIT, Collections.emptyList()),
    WEIGHT(21, "称重鞍座", 9999999.0, SectionType.EXIT, SourceType.EXIT, Collections.emptyList());

    private final int idx;
    private final String desc;
    private final double basePosition;
    private final SectionType sectionType;
    private final SourceType sourceType;
    private final List<String> paramFields;

    private static final Map<String, DeviceEnum> NAME_MAP = new HashMap<>();

    static {
        for (DeviceEnum d : values()) NAME_MAP.put(d.name(), d);
    }

    DeviceEnum(int idx, String desc, double basePosition, SectionType sectionType, SourceType sourceType, List<String> paramFields) {
        this.idx = idx;
        this.desc = desc;
        this.basePosition = basePosition;
        this.sectionType = sectionType;
        this.sourceType = sourceType;
        this.paramFields = paramFields;
    }

    public static DeviceEnum fromName(String name) {
        return NAME_MAP.get(name);
    }

    public boolean isEntrySection() {
        return sectionType == SectionType.ENTRY;
    }

    public boolean isProcessSection() {
        return sectionType == SectionType.PROCESS;
    }

    public boolean isExitSection() {
        return sectionType == SectionType.EXIT;
    }

    /**
     * 段类型：用于区分入口 / 工艺 / 出口
     */
    public enum SectionType {
        ENTRY, PROCESS, EXIT
    }

    /**
     * 来源类型：用于确定从哪个 AppMeasureXXXMessage 中取值
     */
    public enum SourceType {
        ENTRY, FURNACE, COAT, EXIT
    }
}
