Attribute VB_Name = "Module_Main"
' 360LM BTL Add-in — Main Module
' Ribbon callbacks + utility functions
Option Explicit

Public gRibbon As IRibbonUI

' ── Ribbon lifecycle ──────────────────────────────────────────────────────────
Public Sub RibbonOnLoad(ribbon As IRibbonUI)
    Set gRibbon = ribbon
End Sub

' ── Ribbon callbacks ──────────────────────────────────────────────────────────
Public Sub EditCover(control As IRibbonControl)
    ' Navigate to cover slide (slide 2) and open cover form
    If ActivePresentation.Slides.Count < 2 Then
        MsgBox "Cover slide not found. Is this a 360LM BTL deck?", vbExclamation
        Exit Sub
    End If
    ActiveWindow.View.GotoSlide 2
    frmCover.Show
End Sub

Public Sub NewCounter(control As IRibbonControl)
    frmNewCounter.Show
End Sub

Public Sub ValidateAll(control As IRibbonControl)
    Dim issues As String
    issues = Module_Validate.RunValidation()
    If issues = "" Then
        MsgBox "All slides passed validation.", vbInformation, "360LM Validate"
    Else
        MsgBox "Issues found:" & vbNewLine & vbNewLine & issues, vbExclamation, "360LM Validate"
    End If
End Sub

Public Sub ExportJSON(control As IRibbonControl)
    Dim path As String
    path = Module_JSON.ExportToFile()
    If path <> "" Then
        MsgBox "JSON exported to:" & vbNewLine & path, vbInformation, "360LM Export"
    End If
End Sub

Public Sub EmailTo360LM(control As IRibbonControl)
    Dim jsonPath As String
    jsonPath = Module_JSON.ExportToFile()
    If jsonPath = "" Then Exit Sub

    ' Save PPTX to temp location
    Dim pptxPath As String
    pptxPath = Environ("TEMP") & "\360LM_BTL_" & Format(Now, "YYYYMMDD_HHMMSS") & ".pptx"
    ActivePresentation.SaveCopyAs pptxPath

    ' Compose Outlook email
    On Error Resume Next
    Dim ol As Object
    Set ol = CreateObject("Outlook.Application")
    If ol Is Nothing Then
        MsgBox "Outlook not found. Please attach:" & vbNewLine & _
               pptxPath & vbNewLine & jsonPath & vbNewLine & _
               "and email to 360degreemktg@gmail.com", vbInformation
        Exit Sub
    End If
    On Error GoTo 0

    Dim mail As Object
    Set mail = ol.CreateItem(0)
    mail.To = "360degreemktg@gmail.com"
    mail.Subject = "BTL Brief — " & GetCoverField("lm360_brand") & _
                   " — " & GetCoverField("lm360_campaign")
    mail.Body = "Please find the BTL installation brief attached." & vbNewLine & _
                "Brand: " & GetCoverField("lm360_brand") & vbNewLine & _
                "Campaign: " & GetCoverField("lm360_campaign") & vbNewLine & _
                "Date: " & GetCoverField("lm360_date")
    mail.Attachments.Add pptxPath
    mail.Attachments.Add jsonPath
    mail.Display   ' show for review before send
End Sub

' ── Utilities ─────────────────────────────────────────────────────────────────
Public Function GetCoverField(tagName As String) As String
    ' Read a named shape from the cover slide (slide index 2)
    GetCoverField = ""
    If ActivePresentation.Slides.Count < 2 Then Exit Function
    Dim slide As slide
    Set slide = ActivePresentation.Slides(2)
    Dim shp As Shape
    For Each shp In slide.Shapes
        If shp.Name = tagName Then
            If shp.HasTextFrame Then
                GetCoverField = Trim(shp.TextFrame.TextRange.Text)
            End If
            Exit Function
        End If
    Next shp
End Function

Public Function GetShapeText(slide As slide, tagName As String) As String
    GetShapeText = ""
    Dim shp As Shape
    For Each shp In slide.Shapes
        If shp.Name = tagName Then
            If shp.HasTextFrame Then
                GetShapeText = Trim(shp.TextFrame.TextRange.Text)
            End If
            Exit Function
        End If
    Next shp
End Function

Public Function IsCounterSlide(slide As slide) As Boolean
    ' A counter slide has a shape named lm360_counter_N
    Dim shp As Shape
    For Each shp In slide.Shapes
        If shp.Name Like "lm360_counter_*" Then
            IsCounterSlide = True
            Exit Function
        End If
    Next shp
    IsCounterSlide = False
End Function

Public Function GetCounterNo(slide As slide) As String
    Dim shp As Shape
    For Each shp In slide.Shapes
        If shp.Name Like "lm360_counter_*" Then
            GetCounterNo = Mid(shp.Name, Len("lm360_counter_") + 1)
            Exit Function
        End If
    Next shp
    GetCounterNo = ""
End Function
