'=============================================================================
' GaugeClassifier - Core business logic class for pressure gauge classification
' Handles string parsing and classification based on priority decision funnel
'=============================================================================
Option Explicit

' Main classification entry point
' Input: modelString - Full model code string (e.g., "PYTH-100.A0.531|BP-088.2312|FBP-251040")
' Output: Classification name in Chinese
Public Function Classify(modelString As String) As String
    Dim head As String
    Dim flange As String
    Dim mainModel As String

    ' Handle empty or invalid input
    If Trim(modelString) = "" Then
        Classify = "其他"
        Exit Function
    End If

    ' Extract components
    head = GetHead(modelString)
    flange = GetFlange(modelString)
    mainModel = GetMainModel(head)

    ' Classification priority funnel (highest priority first)

    ' 1. 喷涂产品 (Spray-coated) - Highest Priority
    ' Check: Flange section ends with 'P' OR head contains 'F6AP' or 'F6BP'
    If IsSprayProduct(flange, head) Then
        Classify = "喷涂产品"
        Exit Function
    End If

    ' 2. 卫生型隔膜表 (Sanitary diaphragm)
    ' Check: Head contains 'F6A' or 'F6B' (excluding spray variants already caught)
    If IsSanitary(head) Then
        Classify = "卫生型隔膜表"
        Exit Function
    End If

    ' 3-8: Main model prefix based classification
    Select Case True
        ' 3. 隔膜表 (Diaphragm)
        Case mainModel Like "P*"
            Classify = "隔膜表"

        ' 4. 差压表 (Differential pressure)
        Case mainModel Like "YC*"
            Classify = "差压表"

        ' 5. 膜盒压力表 (Capsule)
        Case mainModel Like "YE*"
            Classify = "膜盒压力表"

        ' 6. 精密压力表 (Precision)
        Case mainModel Like "YB*"
            Classify = "精密压力表"

        ' 7. 电接点压力表 (Electric contact)
        Case mainModel Like "YX*"
            Classify = "电接点压力表"

        ' 8. 常规表 (Regular)
        Case mainModel Like "Y*"
            Classify = "常规表"

        ' 9. 其他 (Other) - Default
        Case Else
            Classify = "其他"
    End Select
End Function

'=============================================================================
' SPRAY PRODUCT DETECTION
'=============================================================================
Private Function IsSprayProduct(flange As String, head As String) As Boolean
    ' Check 1: Flange starts with F and 3rd character is P (e.g., FBP, FCP)
    If flange <> "" And Len(flange) >= 3 Then
        If Left(flange, 1) = "F" And Mid(flange, 3, 1) = "P" Then
            IsSprayProduct = True
            Exit Function
        End If
    End If

    ' Check 2: Head contains 'F6AP' or 'F6BP'
    If InStr(head, "F6AP") > 0 Or InStr(head, "F6BP") > 0 Then
        IsSprayProduct = True
        Exit Function
    End If

    IsSprayProduct = False
End Function

'=============================================================================
' SANITARY DIAPHRAGM DETECTION
'=============================================================================
Private Function IsSanitary(head As String) As Boolean
    ' Contains 'F6A' or 'F6B' (but not spray variants)
    ' Note: Spray variants (F6AP, F6BP) are already caught by IsSprayProduct
    If InStr(head, "F6A") > 0 Or InStr(head, "F6B") > 0 Then
        IsSanitary = True
    Else
        IsSanitary = False
    End If
End Function

'=============================================================================
' STRING PARSING METHODS
'=============================================================================

' Extract head section (everything before first '|')
' Example: "PYTH-100.A0.531|BP-088.2312|FBP-251040" -> "PYTH-100.A0.531"
Public Function GetHead(fullString As String) As String
    Dim pipePos As Long

    pipePos = InStr(fullString, "|")

    If pipePos > 0 Then
        GetHead = Left(fullString, pipePos - 1)
    Else
        ' No pipe found, entire string is head
        GetHead = fullString
    End If
End Function

' Extract flange section (last section if starts with 'F')
' Example: "PYTH-100.A0.531|BP-088.2312|FBP-251040" -> "FBP-251040"
' Example: "PYTH-100.A0.531|BP-088.2312" -> ""
Public Function GetFlange(fullString As String) As String
    Dim sections() As String
    Dim lastSection As String

    sections = Split(fullString, "|")

    If UBound(sections) >= 0 Then
        lastSection = Trim(sections(UBound(sections)))

        ' Check if last section starts with 'F' (flange indicator)
        If Left(lastSection, 1) = "F" Then
            GetFlange = lastSection
        Else
            GetFlange = ""
        End If
    Else
        GetFlange = ""
    End If
End Function

' Extract main model prefix from head (first word before '-')
' Example: "PYTH-100.A0.531.M201.M06" -> "PYTH"
' Example: "Y-040.Z0.200.M104.M07" -> "Y"
Public Function GetMainModel(headString As String) As String
    Dim dashPos As Long
    Dim model As String

    ' Find first dash
    dashPos = InStr(headString, "-")

    If dashPos > 0 Then
        model = Left(headString, dashPos - 1)
    Else
        ' No dash found, use entire head
        model = headString
    End If

    GetMainModel = model
End Function
