Attribute VB_Name = "Module_SlideBuilder"
' 360LM BTL Add-in — Slide Builder
' Programmatically creates a standardised counter slide from form data
Option Explicit

' Colours (RGB long)
Private Const NAVY   As Long = 2113060    ' #1F3864
Private Const ORANGE As Long = 27391      ' #FF6B00  (BGR for VBA = 0x006BFF → no, RGB)
Private Const WHITE  As Long = 16777215
Private Const LIGHT  As Long = 15921906   ' #F2F2F2
Private Const DARK   As Long = 2171169    ' #212121

' Helper: BGR encoding VBA uses = B*65536 + G*256 + R
Private Function Rgb2Vba(r As Integer, g As Integer, b As Integer) As Long
    Rgb2Vba = RGB(r, g, b)
End Function

Public Sub BuildCounterSlide(prs As Presentation, cno As Integer, _
    brand As String, cname As String, city As String, state As String, _
    items() As String, itemCount As Integer)

    Dim sl As slide
    ' Insert after last counter slide
    Dim insertAt As Integer: insertAt = prs.Slides.Count
    Dim s As slide
    For Each s In prs.Slides
        If Module_Main.IsCounterSlide(s) Then insertAt = s.SlideIndex
    Next s
    Set sl = prs.Slides.AddSlide(insertAt + 1, prs.SlideMasters(1).Layouts(7))

    ' Clear default placeholders
    Dim shp As Shape
    For Each shp In sl.Shapes
        shp.Delete
    Next shp

    ' Background = white
    sl.Background.Fill.Solid
    sl.Background.Fill.ForeColor.RGB = RGB(255, 255, 255)

    ' ── Header bar ──────────────────────────────────────────────────────────
    AddRect sl, 0, 0, PtToEmu(960), PtToEmu(79), NAVY, , "hdr_bg"
    AddText sl, "Counter #" & cno, _
        PtToEmu(18), PtToEmu(4), PtToEmu(144), PtToEmu(28), 10, _
        True, RGB(255, 107, 0), msoAlignLeft, "lbl_cno"
    AddText sl, cname, _
        PtToEmu(18), PtToEmu(30), PtToEmu(634), PtToEmu(45), 22, _
        True, RGB(255, 255, 255), msoAlignLeft, "lm360_counter_" & cno
    AddText sl, city & "  |  " & state, _
        PtToEmu(663), PtToEmu(6), PtToEmu(281), PtToEmu(27), 11, _
        False, RGB(242, 242, 242), msoAlignRight, "lm360_city_state_" & cno
    ' Brand pill
    AddRect sl, PtToEmu(663), PtToEmu(36), PtToEmu(94), PtToEmu(28), _
        RGB(255, 107, 0), , "lm360_brand_tag_" & cno
    AddText sl, brand, _
        PtToEmu(663), PtToEmu(36), PtToEmu(94), PtToEmu(28), 12, _
        True, RGB(255, 255, 255), msoAlignCenter, "brand_tag_txt"

    ' ── Items table ─────────────────────────────────────────────────────────
    Dim colW(5) As Single  ' in points
    colW(0) = 122: colW(1) = 108: colW(2) = 108: colW(3) = 36: colW(4) = 158: colW(5) = 130
    Dim colH(5) As String
    colH(0) = "Item Type": colH(1) = "Material": colH(2) = "Size (W×H)"
    colH(3) = "Qty":       colH(4) = "Remarks – Prod": colH(5) = "Remarks – Install"
    Dim colK(5) As String
    colK(0) = "type": colK(1) = "material": colK(2) = "size"
    colK(3) = "qty":  colK(4) = "rem_prod": colK(5) = "rem_inst"

    Dim HDR_H As Single: HDR_H = 27
    Dim ROW_H As Single: ROW_H = 32
    Dim TABLE_TOP As Single: TABLE_TOP = 85
    Dim TABLE_LEFT As Single: TABLE_LEFT = 18

    ' Header row background
    AddRect sl, PtToEmu(TABLE_LEFT), PtToEmu(TABLE_TOP), PtToEmu(662), PtToEmu(HDR_H), NAVY, , "hdr_row"
    Dim x As Single: x = TABLE_LEFT
    Dim ci As Integer
    For ci = 0 To 5
        AddText sl, colH(ci), _
            PtToEmu(x + 2), PtToEmu(TABLE_TOP + 3), PtToEmu(colW(ci) - 4), PtToEmu(HDR_H - 4), 8, _
            True, RGB(255, 255, 255), msoAlignCenter, "hdr_" & ci
        x = x + colW(ci)
    Next ci

    ' Data rows
    Dim ri As Integer
    For ri = 0 To itemCount - 1
        Dim rowTop As Single: rowTop = TABLE_TOP + HDR_H + ri * ROW_H
        Dim rowBg As Long
        rowBg = IIf(ri Mod 2 = 0, RGB(242, 242, 242), RGB(255, 255, 255))
        AddRect sl, PtToEmu(TABLE_LEFT), PtToEmu(rowTop), PtToEmu(662), PtToEmu(ROW_H), rowBg, , "row_bg_" & ri
        x = TABLE_LEFT
        For ci = 0 To 5
            Dim val As String: val = items(ri, ci)
            ' items(ri,0)=type, (1)=size, (2)=qty, (3)=remP, (4)=remI  note: material not in form currently
            ' remap: form cols are type/size/qty/remP/remI → we insert material as blank
            Select Case ci
                Case 0: val = items(ri, 0)   ' type
                Case 1: val = ""              ' material (blank — client adds if needed)
                Case 2: val = items(ri, 1)   ' size
                Case 3: val = items(ri, 2)   ' qty
                Case 4: val = items(ri, 3)   ' rem_prod
                Case 5: val = items(ri, 4)   ' rem_inst
            End Select
            AddText sl, val, _
                PtToEmu(x + 3), PtToEmu(rowTop + 5), PtToEmu(colW(ci) - 6), PtToEmu(ROW_H - 6), 8, _
                False, RGB(33, 33, 33), msoAlignLeft, "lm360_" & cno & "_" & ri & "_" & colK(ci)
            x = x + colW(ci)
        Next ci
    Next ri

    ' Orange left stripe
    Dim stripeH As Single: stripeH = HDR_H + itemCount * ROW_H
    AddRect sl, PtToEmu(TABLE_LEFT), PtToEmu(TABLE_TOP), PtToEmu(5), PtToEmu(stripeH), RGB(255, 107, 0), , "orange_stripe"

    ' ── Creative placeholder ─────────────────────────────────────────────────
    Dim cpLeft As Single: cpLeft = 692
    AddRect sl, PtToEmu(cpLeft), PtToEmu(85), PtToEmu(258), PtToEmu(450), _
        RGB(242, 242, 242), RGB(255, 107, 0), "lm360_creative_" & cno
    AddText sl, Chr(128248) & "  Drag creative" & vbNewLine & "or image here", _
        PtToEmu(cpLeft + 20), PtToEmu(250), PtToEmu(218), PtToEmu(80), 11, _
        False, RGB(170, 170, 170), msoAlignCenter, "creative_hint"

    ' ── Footer ──────────────────────────────────────────────────────────────
    AddRect sl, 0, PtToEmu(519), PtToEmu(960), PtToEmu(21), NAVY, , "footer_bg"
    AddText sl, "360LM BTL  •  Counter #" & cno & "  •  " & cname & "  •  " & brand, _
        PtToEmu(8), PtToEmu(520), PtToEmu(944), PtToEmu(19), 8, _
        False, RGB(200, 200, 200), msoAlignCenter, "footer_txt"

    ' ── Embedded JSON (hidden, off-slide right) ──────────────────────────────
    Dim jsonData As String
    jsonData = BuildItemJSON(cno, cname, city, state, brand, items, itemCount)
    AddText sl, jsonData, PtToEmu(1000), 0, PtToEmu(400), PtToEmu(20), 1, _
        False, RGB(255, 255, 255), msoAlignLeft, "lm360_json_" & cno

    ' Navigate to new slide
    ActiveWindow.View.GotoSlide sl.SlideIndex
End Sub

Private Function BuildItemJSON(cno As Integer, cname As String, city As String, _
    state As String, brand As String, items() As String, n As Integer) As String
    Dim arr As String: arr = ""
    Dim i As Integer
    For i = 0 To n - 1
        If arr <> "" Then arr = arr & ","
        arr = arr & "{""type"":""" & items(i, 0) & """," & _
              """size"":""" & items(i, 1) & """," & _
              """qty"":" & IIf(IsNumeric(items(i, 2)), items(i, 2), 1) & "," & _
              """rem_prod"":""" & items(i, 3) & """," & _
              """rem_inst"":""" & items(i, 4) & """}"
    Next i
    BuildItemJSON = "{""lm360_schema"":""v1"",""counter_no"":" & cno & _
        ",""counter_name"":""" & cname & """" & _
        ",""city"":""" & city & """,""state"":""" & state & """" & _
        ",""brand"":""" & brand & """,""items"":[" & arr & "]}"
End Function

Private Sub AddRect(sl As slide, l As Long, t As Long, w As Long, h As Long, _
    fillRGB As Long, Optional lineRGB As Long = -1, Optional nm As String = "")
    Dim shp As Shape
    Set shp = sl.Shapes.AddShape(msoShapeRectangle, l, t, w, h)
    shp.Fill.Solid
    shp.Fill.ForeColor.RGB = fillRGB
    If lineRGB = -1 Then
        shp.Line.Visible = msoFalse
    Else
        shp.Line.Visible = msoTrue
        shp.Line.ForeColor.RGB = lineRGB
        shp.Line.Weight = 1.5
    End If
    If nm <> "" Then shp.Name = nm
End Sub

Private Sub AddText(sl As slide, txt As String, l As Long, t As Long, w As Long, h As Long, _
    ptSize As Single, bold As Boolean, rgb As Long, align As Integer, nm As String)
    Dim tb As Shape
    Set tb = sl.Shapes.AddTextbox(msoTextOrientationHorizontal, l, t, w, h)
    With tb.TextFrame
        .WordWrap = msoTrue
        .MarginLeft = 0: .MarginTop = 0: .MarginRight = 0: .MarginBottom = 0
        .TextRange.Text = txt
        .TextRange.Font.Size = ptSize
        .TextRange.Font.Bold = bold
        .TextRange.Font.Color.RGB = rgb
        .TextRange.ParagraphFormat.Alignment = align
    End With
    If nm <> "" Then tb.Name = nm
End Sub

Private Function PtToEmu(pt As Single) As Long
    ' 1 point = 12700 EMU
    PtToEmu = CLng(pt * 12700)
End Function
