package com.fizz.business.constants.enums;

import lombok.Getter;

import java.util.*;

/**
 * Fine-grained device enumeration / 细粒度设备枚举
 * - position: base position (m) / 基准位置（单位：m）
 * - sectionType: section (entry / process / exit) / 所属段（入口 / 工艺 / 出口）
 * - sourceType: data source (ENTRY/FURNACE/COAT/EXIT) / 数据来源
 * - paramFields: corresponding fields in AppMeasure*Message / 对应AppMeasure*Message中的字段
 */
@Getter
public enum DeviceEnum {
    // === Entry section / 入口段 ===
    POR1(0, "Uncoiler #1", 0.0, SectionType.ENTRY, SourceType.ENTRY, Arrays.asList("tensionPorBr1", "stripSpeed")),
    POR2(1, "Uncoiler #2", 0.0, SectionType.ENTRY, SourceType.ENTRY, Arrays.asList("tensionPorBr2", "stripSpeed")),
    WELDER(2, "Welder", 4.98, SectionType.ENTRY, SourceType.ENTRY, Arrays.asList("weldStatus")),
    ENL1(3, "Entry Looper #1", 19.04, SectionType.PROCESS, SourceType.ENTRY, Arrays.asList("celLength", "celCapacity", "tensionCel")),
    ENL2(4, "Entry Looper #2", 167.09, SectionType.PROCESS, SourceType.ENTRY, Arrays.asList("celLength", "celCapacity", "tensionCel")),
    ENL3(5, "Entry Looper #3", 198.19, SectionType.PROCESS, SourceType.ENTRY, Arrays.asList("celLength", "celCapacity", "tensionCel")),

    // === Process section / 工艺段 ===
    CLEAN(6, "Cleaning Section", 264.803, SectionType.PROCESS, SourceType.FURNACE, Arrays.asList("cleaningVoltage", "cleaningCurrent", "alkaliConcentration", "alkaliTemperature")),
    FUR1(7, "Annealing Furnace - Preheating", 302.837, SectionType.PROCESS, SourceType.FURNACE, Arrays.asList("phfExitStripTemp", "potTemperature", "gasConsumption")),
    FUR2(8, "Annealing Furnace - Heating", 381.057, SectionType.PROCESS, SourceType.FURNACE, Arrays.asList("rtfExitStripTemp", "zincPotPower")),
    FUR3(9, "Annealing Furnace - Cooling", 416.837, SectionType.PROCESS, SourceType.FURNACE, Arrays.asList("jcsExitStripTemp", "coolingTowerStripTemp")),
    FUR4(10, "Annealing Furnace - Equalizing", 432.16, SectionType.PROCESS, SourceType.FURNACE, Arrays.asList("scsExitStripTemp")),
    POT(11, "Zinc Pot", 442.994, SectionType.PROCESS, SourceType.COAT, Arrays.asList("scsExitStripTemp")),
    TOWER(12, "Cooling Tower", 563.594, SectionType.PROCESS, SourceType.COAT, Arrays.asList("scsExitStripTemp")),
    TM(13, "Temper Mill", 586.529, SectionType.PROCESS, SourceType.COAT, Arrays.asList("tensionBr5Tm", "stripSpeedTmExit")),
    TL(14, "Tension Leveler", 612.909, SectionType.PROCESS, SourceType.COAT, Arrays.asList("tlElongation", "tensionTlBr7")),
    COAT(15, "Post-treatment Section", 712.699, SectionType.PROCESS, SourceType.COAT, Arrays.asList(
            "avrCoatingWeightTop","stdCoatingWeightTop","maxCoatingWeightTop","minCoatingWeightTop",
            "avrCoatingWeightBottom","stdCoatingWeightBottom","maxCoatingWeightBottom","minCoatingWeightBottom",
            "airKnifePressure","airKnifeFlow","airKnifeGap","stripSpeedTmExit","tensionBr8Tm",
            "tensionTmBr9","tensionBr8Br9","tmMask","tmElongation","rollForceOperator","rollForceDrive",
            "motorTorque","bendingForce","antiCrimpingRollMesh","billyRollMesh",
            "tensionTlBr10Br11","tensionBr9toBr10Br11","tlFlag","tlElongation","levelingUnit1Mesh","levelingUnit2Mesh",
            "antiCrossBowUnitMesh","tensionBr10Br11toBr12","stripSpeedAfp","stripTempAfp"
    )),

    // === Exit section / 出口段 ===
    CXL1(16, "Exit Looper #1", 720.709, SectionType.EXIT, SourceType.EXIT, Arrays.asList("cxlLength", "cxlCapacity", "tensionCxl")),
    CXL2(17, "Exit Looper #2", 888.789, SectionType.EXIT, SourceType.EXIT, Arrays.asList("cxlLength", "cxlCapacity", "tensionCxl")),
   // INS(18, "Inspection Station", 940.561, SectionType.EXIT, SourceType.EXIT, Arrays.asList("inspectionStatus")),
    TR(18, "Recoiler", 952.819, SectionType.EXIT, SourceType.EXIT, Arrays.asList("coilLength", "speedExitSection", "tensionBr9Tr")),
    EXC(19, "Coil Car", 9999999.0, SectionType.EXIT, SourceType.EXIT, Collections.emptyList()),
    WEIGHT(20, "Weighing Saddle", 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
    }
}
