VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "ItemHyperlink"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' Parsed data hyperlink
Option Explicit

Private gid_ As Long
Private bookmark_ As String
Private system_ As TLinkSystem

Private Sub Class_Initialize()
  gid_ = HT03_INVALID_GID
  bookmark_ = ""
  system_ = PT_INVALID
End Sub

Public Function Init(iSystem As TLinkSystem, gid&, Optional sBookmark$ = "sub_0")
  gid_ = gid
  bookmark_ = sBookmark
  system_ = iSystem
End Function

Public Property Get DocumentID() As Long
  DocumentID = gid_
End Property

Public Property Get BookmarkName() As String
  BookmarkName = bookmark_
End Property

Public Property Get LinkSystem() As TLinkSystem
  LinkSystem = system_
End Property

Public Property Let DocumentID(newVal&)
  gid_ = newVal
End Property

Public Property Let BookmarkName(newVal$)
  bookmark_ = newVal
End Property

Public Property Let LinkSystem(newVal As TLinkSystem)
  system_ = newVal
End Property

Public Function FromAddress(addr$) As Boolean
  FromAddress = False
  Dim iSystem As TLinkSystem: iSystem = ExtractSystem(addr)
  If iSystem = PT_INVALID Then _
    Exit Function
  
  Dim dataSuffix$: dataSuffix = CutPrefix(addr, iSystem)
  If dataSuffix = vbNullString Then _
    Exit Function
  Dim gidStr$: gidStr = VBA.Left(dataSuffix, VBA.InStr(1, dataSuffix, ".") - 1)
  If Not IsNumeric(gidStr) Then _
    Exit Function
    
  Dim strSub$: strSub = VBA.Right(dataSuffix, VBA.Len(dataSuffix) - VBA.InStr(dataSuffix, "."))
  If Not IsNumeric(strSub) Then _
    Exit Function
  
  system_ = iSystem
  gid_ = CLng(gidStr)
  bookmark_ = "sub_" & strSub
  
  FromAddress = True
End Function

Public Function ToAddress() As String
  Dim sBookmarkID$: sBookmarkID = GetBookmarkHyperlink
  Select Case system_
    Case PT_MOBILE: ToAddress = Fmt("http://mobileonline.garant.ru/document/redirect/{1}/{2}", gid_, sBookmarkID)
    Case PT_LOCAL: ToAddress = Fmt("garantf1://{1}.{2}/", gid_, sBookmarkID)
    Case PT_INTERNET: ToAddress = Fmt("http://internet.garant.ru/document/redirect/{1}/{2}", gid_, sBookmarkID)
  End Select
End Function

Public Function ValidateSync(itLink As IteratorLinkCached)
  If itLink.DestinationID <> gid_ Then _
    Call Err.Raise(ERR_SYNC_LINK_ID, Description:=CStr(itLink.DestinationID) & "!=" & CStr(gid_))
End Function

' ===========================
Private Function CutPrefix(aLink$, iSystem As TLinkSystem) As String
  Select Case iSystem
    Case PT_MOBILE
      CutPrefix = VBA.Right(aLink, VBA.Len(aLink) - 48) ' 48 = VBA.Len(PrefixFor(PT_MOBILE))
      CutPrefix = Replace(CutPrefix, "/", ".")
    Case PT_LOCAL
      CutPrefix = VBA.Right(aLink, VBA.Len(aLink) - VBA.InStr(1, aLink, "//") - 1)
      CutPrefix = VBA.Left(CutPrefix, VBA.Len(CutPrefix) - 1)
    Case PT_INTERNET
      CutPrefix = VBA.Right(aLink, VBA.Len(aLink) - 44) ' 44 = Len(PrefixFor(PT_INTERNET))
      CutPrefix = VBA.Replace(CutPrefix, "/", ".")
  End Select
  If Not CutPrefix Like "*.*" Then _
    CutPrefix = vbNullString
End Function

Private Function PrefixFor(aType As TLinkSystem) As String
  Select Case aType
    Case PT_MOBILE: PrefixFor = "http://mobileonline.garant.ru/document/redirect/"
    Case PT_LOCAL: PrefixFor = "garantf1://"
    Case PT_INTERNET: PrefixFor = "http://internet.garant.ru/document/redirect/"
  End Select
End Function

Private Function ExtractSystem(sAddress$) As TLinkSystem
  If InStr(1, sAddress, PrefixFor(PT_MOBILE), vbTextCompare) = 1 Then
    ExtractSystem = PT_MOBILE
  ElseIf InStr(1, sAddress, PrefixFor(PT_LOCAL), vbTextCompare) = 1 Then
    ExtractSystem = PT_LOCAL
  ElseIf InStr(1, sAddress, PrefixFor(PT_INTERNET), vbTextCompare) = 1 Then
    ExtractSystem = PT_INTERNET
  Else
    ExtractSystem = PT_INVALID
  End If
End Function

Private Function GetBookmarkHyperlink() As String
  If Len(bookmark_) <= 4 Then
    GetBookmarkHyperlink = "0"
  Else
    GetBookmarkHyperlink = Right(bookmark_, Len(bookmark_) - 4)
  End If
End Function
