'#Reference {A3B5C45B-F842-44fc-A5E5-50CED69BA86E}#2.0#0#C:\Program Files\Mindjet\MindManager 6\Mm5WebExport.dll#Mm5webexport 2.0 Type Library
'#Uses "GlobalUtilities.mmbas"

Option Explicit

Sub Main

    Dim addin As Mm5WebExportAddIn
    Dim webex As WebExport
    Dim aPage As Mm5WebExportLib.Page

    Set addin = WebExportHelper.WebExportAddIn
    Set webex = addin.ActiveWebExport

    Dim count As Long
    Dim i As Long
    Dim template As String
    Dim topicString As String
    Dim bodyString As String
    Dim TheUnit As Long
    Dim DoExport As Boolean
    Dim boxCounterTextMarkers As Integer

    Dim oneIconsCollection As Icons
    Dim oneIcon As Icon
    Dim oneTopic As Topic
    Dim oneTextLabels As TextLabels
    Dim exportTextMarkersBodyString As String

    On Error GoTo ReportError
    
    webex.Trace("Entering BoxTextMarkers.mmbas",2)

    Set oneTopic = webex.ActiveTopic
    Set oneTextLabels = oneTopic.TextLabels
    
    template = webex.ReadFile("BoxTextMarkers.htm")

    If    webex.GetParameter("Strings.Show.TextMarkers") = False _
       Or DoExportTextMarkersSection(oneTextLabels) = False Then

       ' Don't export task info section
       bodyString = ""

    Else
       ' Process all commands and parameters
       ProcessString webex, template 

       ' Our merged string to write back
       bodyString = template 

       ' Increment box counter number
       boxCounterTextMarkers = webex.GetParameter("CustomParameter.BoxCounterTextMarkers")
       boxCounterTextMarkers = boxCounterTextMarkers + 1
       webex.SetParameter("CustomParameter.BoxCounterTextMarkers", boxCounterTextMarkers)
    End If

    ' Write out the result
    webex.PushString(bodyString)

    webex.Trace("Exiting Page_box_textmarkers.mmbas",2)

    Exit Sub
 
ReportError:
    ProcessError(webex)
    Exit Sub
 
End Sub

Function DoExportTextMarkersSection(oneTextLabels As TextLabels)

    DoExportTextMarkersSection = False

    ' Allow task to have priority and percentage complete info
    ' defined without exporting the task planning section

    If oneTextLabels.Count > 0 Then
        DoExportTextMarkersSection = True
    End If
    

End Function

Private Function ExportTextMarkers(ByRef webex As WebExport, ByRef oneTextLabels As TextLabels) As String

    Dim i As Long
    Dim textMarkerCount As Long
    Dim template As String
    Dim topicString As String
    Dim bodyString As String

    'Make a copy of list To Compare With
    Dim textMarkersToCompare As Variant
    textMarkerCount = oneTextLabels.Count
    ReDim textMarkersToCompare(1 To textMarkerCount) As String

    For i = 1 To textMarkerCount
        Dim oneTextLabel As TextLabel
        Set oneTextLabel = oneTextLabels.Item(i)

		Dim textMarkerName As String
		textMarkerName = oneTextLabel.Name
		textMarkersToCompare(i) = textMarkerName
    Next


    'Create And add Text marker strings To export
    For i = 1 To textMarkerCount
		Dim oneTextMarker As TextLabel
        Set oneTextMarker = oneTextLabels.Item(i)

		Dim textMarker As String
		textMarker = oneTextMarker.Name

		If textMarker <> "" Then
            Dim textMarkerNameToAdd As String
            Dim textMarkerGroupName As String
            CreateTextMarker webex, textMarker, textMarkersToCompare, textMarkerNameToAdd, textMarkerGroupName
	    	If textMarkerNameToAdd <> "" Then
	            template = webex.ReadFile( "SnipTextMarkers.htm" )
	            webex.SetParameter("CustomParameter.CodeLabel" , textMarkerGroupName)
	            webex.SetParameter("CustomParameter.CodeValue" , textMarkerNameToAdd)

	            topicString = template

	            ' Process all commands and parameters
	            ProcessString webex, topicString

	            ' Store our processed review comments
	            bodyString = bodyString + topicString
            End If
        End If
    Next

	MsgBox(bodyString)

    ExportTextMarkers = bodyString
End Function

Private Sub CreateTextMarker( _
    ByRef webex As WebExport, _
    ByVal textMarkerSource As String, _
    ByRef varrTextMarkers As Variant, _
    ByRef textMarkerName As String, _
    ByRef textMarkerGroupName As String)

    Dim checkForAppendedTextMarkers As Boolean
    Dim strTextMarker As String
    Dim i As Integer

    checkForAppendedTextMarkers = False

    'Only want To export Text markers For 1 group Name, no duplicates
    For i = 1 To UBound(varrTextMarkers)
        If textMarkerSource = varrTextMarkers(i) Then
            textMarkerGroupName = GetTextMarkerGroupName(webex, textMarkerSource)
            textMarkerName = textMarkerSource
            checkForAppendedTextMarkers = True
        End If
	Next

	'Check If current group Name has more associated Text markers
    If checkForAppendedTextMarkers = True Then
        For i = 1 To UBound(varrTextMarkers)
            If varrTextMarkers(i) <> "" Then
                Dim textMarkerGroupNameCompare As String
                textMarkerGroupNameCompare = GetTextMarkerGroupName(webex, textMarkerName)
                If textMarkerGroupNameCompare = textMarkerGroupName And textMarkerSource <> varrTextMarkers(i) Then
                    'Append Text marker Name And remove it from the comparing list
                    textMarkerName = textMarkerName & ", " & varrTextMarkers(i)
                    varrTextMarkers(i) = ""
                End If
            End If
        Next
    End If
End Sub

'Get the specified Text marker's group name
Private Function GetTextMarkerGroupName(ByRef webex As WebExport, ByVal textMarker As String) As String

    Dim textMarkerGroupName As String
    Dim mapMarkerGroupsCount As Long
    Dim i As Long

    Dim spMapMarkerGroups As MapMarkerGroups
    Set spMapMarkerGroups = webex.Document.MapMarkerGroups
    mapMarkerGroupsCount = spMapMarkerGroups.Count
    For i = 1 To mapMarkerGroupsCount
	Dim spMapMarkerGroup As MapMarkerGroup
	Set spMapMarkerGroup = spMapMarkerGroups.Item(i)

        If spMapMarkerGroup.Type = mmMapMarkerGroupTypeTextLabel Then
            Dim mapMarkerGroupCount As Long
            Dim j As Long
            mapMarkerGroupCount = spMapMarkerGroup.Count
            For j = 1 To mapMarkerGroupCount
                Dim spMapMarker As MapMarker
                Set spMapMarker = spMapMarkerGroup.Item(j)

                If spMapMarker.Label = textMarker Then
                    textMarkerGroupName = spMapMarkerGroup.Name
                End If
            Next
	End If
    Next

    If textMarkerGroupName = "" Then
        textMarkerGroupName = "Dynamic text markers"
    End If

    GetTextMarkerGroupName = textMarkerGroupName
End Function
