Attribute VB_Name = "Module_Validate"
' 360LM BTL Add-in — Validation
Option Explicit

Private Const VALID_TYPES As String = _
    "Glow Sign Board,Backlit Board,Flex Banner,ACP Board," & _
    "One Way Vision,Window Decal,Floor Graphic,Translit," & _
    "Fabric Board,Lollypop,Clip on Board,Vinyl Print,Inshop Branding"

Public Function RunValidation() As String
    Dim issues As String: issues = ""
    Dim prs As Presentation: Set prs = ActivePresentation

    ' Check cover
    Dim brand As String: brand = Module_Main.GetCoverField("lm360_brand")
    Dim campaign As String: campaign = Module_Main.GetCoverField("lm360_campaign")
    If brand = "" Or brand Like "*[*]*" Then
        issues = issues & "• Cover: Brand name not set" & vbNewLine
    End If
    If campaign = "" Or campaign Like "*[*]*" Then
        issues = issues & "• Cover: Campaign name not set" & vbNewLine
    End If

    ' Check each counter slide
    Dim sl As slide
    For Each sl In prs.Slides
        If Module_Main.IsCounterSlide(sl) Then
            Dim cno As String: cno = Module_Main.GetCounterNo(sl)
            Dim prefix As String: prefix = "Counter #" & cno & ": "

            Dim cname As String
            cname = Module_Main.GetShapeText(sl, "lm360_counter_" & cno)
            If cname = "" Or cname Like "*[*]*" Or cname Like "*Counter #*" Then
                issues = issues & "• " & prefix & "Counter name missing" & vbNewLine
            End If

            Dim cityState As String
            cityState = Module_Main.GetShapeText(sl, "lm360_city_state_" & cno)
            If cityState = "" Or cityState Like "*[*]*" Then
                issues = issues & "• " & prefix & "City/State missing" & vbNewLine
            End If

            ' Check items
            Dim ri As Integer: ri = 0
            Dim hasItems As Boolean: hasItems = False
            Do
                Dim itype As String
                itype = Module_Main.GetShapeText(sl, "lm360_" & cno & "_" & ri & "_type")
                If itype = "" And ri > 0 Then Exit Do
                If itype <> "" Then
                    hasItems = True
                    Dim sz As String
                    sz = Module_Main.GetShapeText(sl, "lm360_" & cno & "_" & ri & "_size")
                    If sz = "" Then
                        issues = issues & "• " & prefix & "Item " & (ri + 1) & _
                                 " (" & itype & "): size missing" & vbNewLine
                    End If
                    If Not IsValidType(itype) Then
                        issues = issues & "• " & prefix & "Item " & (ri + 1) & _
                                 ": unrecognised type '" & itype & "'" & vbNewLine
                    End If
                End If
                ri = ri + 1
                If ri > 20 Then Exit Do
            Loop
            If Not hasItems Then
                issues = issues & "• " & prefix & "No branding items entered" & vbNewLine
            End If
        End If
    Next sl

    RunValidation = issues
End Function

Private Function IsValidType(t As String) As Boolean
    Dim types() As String: types = Split(VALID_TYPES, ",")
    Dim i As Integer
    For i = 0 To UBound(types)
        If LCase(Trim(types(i))) = LCase(Trim(t)) Then
            IsValidType = True
            Exit Function
        End If
    Next i
    IsValidType = False
End Function
