Attribute VB_Name = "Main"
Option Explicit

Public Const PRODUCT_VERSION = "1.3.0"
Public Const PRODUCT_NAME = "Concept-NPA"

Public g_VersionTimer As Long

Public Sub StartVersionCheck()
  g_VersionTimer = SetTimer(0, 0, CP_VERSION_MSG_DELAY, AddressOf OnVersionCheck)
End Sub

Public Function OnVersionCheck(ByVal nHwnd As Long, ByVal uMsg As Long, ByVal nEvent As Long, ByVal nTime As Long)
  Call KillTimer(0, g_VersionTimer)
  Call VersionValidate(PRODUCT_NAME, PRODUCT_VERSION)
End Function

Public Sub RunClearSchema()
  If Not UserInteraction.AskQuestion(QM_CLEAR_ALL) Then _
    Exit Sub
  
  Call GlobalUndo.BeginScope("Очистка схемы")
  Call VsoClearPage(ThisDocument.Application.ActivePage)
  Call GlobalUndo.EndScope
  
  Call VsoShowWholePage(ThisDocument.Application.ActivePage)
End Sub

Public Sub RunCreateSchema()
  Dim mainPage As Visio.Page: Set mainPage = ThisDocument.Application.ActivePage
  If mainPage.Shapes.Count <> 0 Then _
    If Not UserInteraction.AskQuestion(QM_CLEAR_ALL) Then _
      Exit Sub
  
  Dim xlAppl As New API_XLWrapper
  Call xlAppl.CreateApplication(bIsVisible:=False)

  Dim iUserInput As ItemImportOptions: Set iUserInput = PromptImportOptions(xlAppl)
  If iUserInput Is Nothing Then _
    GoTo SAFE_EXIT

  Dim vsoUI As New API_VsoWrapper: Call vsoUI.SetDocument(ThisDocument)
  Call vsoUI.PauseUI
  Call GlobalUndo.BeginScope("Импорт из Excel")
  
  Call VsoClearPage(mainPage)
  Dim importer As New ImportManager: Call importer.Init(xlAppl.Document, mainPage, iUserInput)
  Call importer.GenerateSchema
    
  Call GlobalUndo.EndScope
  Call vsoUI.ResumeUI
  DoEvents
  
  Call mainPage.ResizeToFitContents
  Call VsoShowWholePage(mainPage)
  
  Call UserInteraction.ShowMessage(IM_CREATION_COMPLETE, importer.nLaws_, importer.nLinks_)
  
SAFE_EXIT:
  Call xlAppl.ReleaseDocument
  Call xlAppl.ReleaseApplication
End Sub

Public Sub RunHelp()
  Dim wordWrap As New API_WordWrapper
  Dim helpDoc As Word.Document: Set helpDoc = wordWrap.OpenDocument(HELP_FILE_PATH, bReadOnly:=True)
  If helpDoc Is Nothing Then _
    Exit Sub
    
  With helpDoc
    .Application.ActiveWindow.View.ReadingLayout = False
    Call .ActiveWindow.ScrollIntoView(helpDoc.Bookmarks(HELP_WORD_BOOKMARK).Range)
    Call .Application.Activate
  End With
End Sub

Public Sub RunMoveDown()
  Dim sel As Visio.Selection: Set sel = ThisDocument.Application.ActiveWindow.Selection
  If sel.Count <> 0 Then
    Call sel.Move(0#, -(SHAPE_HEIGHT + VERTICAL_GAP), visMillimeters)
  End If
End Sub

Public Sub RunMoveUp()
  Dim sel As Visio.Selection: Set sel = ThisDocument.Application.ActiveWindow.Selection
  If sel.Count <> 0 Then
    Call sel.Move(0#, SHAPE_HEIGHT + VERTICAL_GAP, visMillimeters)
  End If
End Sub

Public Sub RunGotoLinkBegin()
Attribute RunGotoLinkBegin.VB_ProcData.VB_Invoke_Func = "Q"
  Call CC_GotoLinkBegin
End Sub

Public Sub RunGotoLinkEnd()
Attribute RunGotoLinkEnd.VB_ProcData.VB_Invoke_Func = "E"
  Call CC_GotoLinkEnd
End Sub

Public Sub RunMoveLeft()
  Dim sel As Visio.Selection: Set sel = ThisDocument.Application.ActiveWindow.Selection
  If sel.Count <> 0 Then
    Call sel.Move(-(SHAPE_WIDTH + HORIZONTAL_GAP), 0#, visMillimeters)
  End If
End Sub

Public Sub RunMoveRight()
  Dim sel As Visio.Selection: Set sel = ThisDocument.Application.ActiveWindow.Selection
  If sel.Count <> 0 Then
    Call sel.Move(SHAPE_WIDTH + HORIZONTAL_GAP, 0#, visMillimeters)
  End If
End Sub
