' Gambas module file

Private Const DB_NAME As String = "gambas3.shapes.db"

Private $hConn As Connection

Public Sub Open()
  
  If $hConn Then Return
  
  $hConn = New Connection
  $hConn.Type = "sqlite"
  $hConn.Host = Settings.DefaultDir
  $hConn.Open
  If Not $hConn.Databases.Exist(DB_NAME) Then $hConn.Databases.Add(DB_NAME, "Shapes")
  $hConn.Close()
  
  $hConn.Name = DB_NAME
  $hConn.Open()
  
  If Not $hConn.Tables.Exist("project") Then $hConn.ApplyTemplate(DB.LoadTemplate("Shapes"))
  
End

Public Sub Close()
  
  If Not $hConn Then Return
  $hConn.Close
  $hConn = Null
  
End

Private Sub GetTagFromName(sTag As String) As Integer

  Dim rTag As Result
  
  If Not sTag Then Return 0
  rTag = $hConn.Find("tag", "sname = &1", sTag)
  If Not rTag.Available Then
    rTag = $hConn.Create("tag", True)
    rTag!sname = sTag
    rTag.Update
  Endif
  Return rTag!id
  
End


Public Sub Add(hImageSelection As CImageSelection) As String
  
  Dim rShape As Result
  Dim sShape As String
  Dim sKey As String
  Dim iTag As Integer
  
  Open()
  
  iTag = GetTagFromName(hImageSelection.Tag)
  
  sShape = hImageSelection.Save(ByRef sKey)
  
  rShape = $hConn.Edit("shapes", "skey = &1", sKey)
  If rShape.Available Then
    If rShape!itag = iTag Then Return True
  Else
    rShape = $hConn.Create("shapes")
    rShape!skey = sKey
    rShape!sshape = sShape
  Endif
  rShape!itag = iTag
  Try rShape.Update()
  If Error Then 
    FMain.ShowErrorWith(("Cannot add shape."))
  Else 
    Return sKey
  Endif
  
End

Public Sub Load() As CImageSelection[]
  
  Dim aImageSelection As New CImageSelection[]
  Dim rShape As Result
  Dim aShape As Collection[]
  Dim hImageSelection As CImageSelection
  Dim rTag As Result
  Dim sTag As String
  
  Open()
  
  rShape = $hConn.Find("shapes", "itag = 0")
  GoSub ADD_SHAPES

  rTag = $hConn.Find("tag", "ORDER BY sname")
  For Each rTag
    sTag = rTag!sname
    rShape = $hConn.Find("shapes", "itag = &1", rTag!id)
    GoSub ADD_SHAPES
  Next
  
  Return aImageSelection
  
ADD_SHAPES:

  For Each rShape
    hImageSelection = New CImageSelection
    hImageSelection.Key = rShape!skey
    aShape = JSON.FromString(rShape!sshape)
    hImageSelection.Load(aShape)
    aImageSelection.Add(hImageSelection)
    hImageSelection.Tag = sTag
  Next
  Return
  
End

Public Sub Remove(sKey As String)
  
  Dim rShape As Result
  
  Open()
  
  rShape = $hConn.Find("shapes", "skey = &1", sKey)
  If Not rShape.Available Then Error.Raise("Shape does not exist")
  $hConn.Delete("shapes", "skey = &1", sKey)
  
Catch 
  
  FMain.ShowErrorWith(("Cannot remove shape."))
  
End

' Public Sub UpdateTag(sKey As String, sNewTag As String, sNewKey As String)
'   
'   Dim rShape As Result
'   Dim iTag As Integer
'   
'   Open()
'   
'   iTag = GetTagFromName(sNewTag)
' 
'   rShape = $hConn.Edit("shapes", "skey = &1", sKey)
'   rShape!itag = iTag
'   rShape!skey = sNewKey
'   rShape.Update()
'   
' Catch 
'   
'   FMain.ShowErrorWith(("Cannot move shape."))
'   
' End
