Module Database
    Dim Data_Source As String
    Dim Data_Connection As New System.Data.SQLite.SQLiteConnection()

    Sub Initiallize_DataBase()
        Data_Source = My.Application.Info.DirectoryPath + "\Rin.sqlite"
        Dim conn_str As New System.Data.SQLite.SQLiteConnectionStringBuilder()
        conn_str.DataSource = Data_Source
        Data_Connection.ConnectionString = conn_str.ToString
        Data_Connection.Open()
    End Sub

#Region "Library"
    Sub Del_Library_Item(ByVal Keyword As String)
        Dim Data_Command As New System.Data.SQLite.SQLiteCommand(Data_Connection)
        Data_Command.CommandText = "DELETE FROM Library WHERE Keyword='" + Keyword + "'"
        Data_Command.ExecuteNonQuery()
    End Sub

    Sub Add_Library_Item(ByVal Param As String)
        Dim key, ans As String
        key = Param.Substring(0, InStr(Param, " ") - 1)
        ans = Param.Substring(key.Length).Trim
        Add_Library_Item(key, ans)
    End Sub

    Sub Add_Library_Item(ByVal Keyword As String, ByVal Answer As String)
        Dim Data_Command As New System.Data.SQLite.SQLiteCommand(Data_Connection)
        Data_Command.CommandText = "DELETE FROM Library WHERE Keyword='" + Keyword + "'"
        Data_Command.ExecuteNonQuery()

        Data_Command = New System.Data.SQLite.SQLiteCommand(Data_Connection)
        Data_Command.CommandText = "INSERT INTO Library VALUES('" + Keyword + "','" + Answer + "')"
        Data_Command.ExecuteNonQuery()
    End Sub

    Function Find_Library_Item(ByVal Keyword As String) As String
        Dim Data_Command As New System.Data.SQLite.SQLiteCommand(Data_Connection)
        Data_Command.CommandText = "SELECT * FROM Library WHERE (Keyword='" + Keyword + "')"
        With Data_Command.ExecuteReader()
            While .Read
                Return .Item("Answer")
            End While
        End With
        Return Nothing
    End Function

    Function Is_Lib_Required(ByVal Message_Str As String) As Boolean
        If Message_Str.EndsWith("是?") Or Message_Str.EndsWith("是？") Then Return True
        Message_Str = Message_Str.Trim().Replace("?", "")
        If Message_Str.StartsWith("什么是") Then Return True
        If Message_Str.StartsWith("啥是") Then Return True
        Message_Str = Message_Str.TrimEnd({"w"c, "呢"c, "呀"c, "啊"c, "?"c, "？"c})
        If Message_Str.EndsWith("是啥") Then Return True
        If Message_Str.EndsWith("是什么") Then Return True
        If Message_Str.EndsWith("是谁") Then Return True
        Return False
    End Function

    Function Get_Keyword(ByVal MessageStr As String) As String
        Return MessageStr.Replace("是?", "").Replace("是？", "").Trim({"?"c, "？"c, " "c}).Replace("什么是", "").Replace("啥是", "").Replace("是啥", "").Replace("是什么", "").Replace("是谁", "").TrimEnd({"w"c, "呢"c, "呀"c, "啊"c, "?"c, "？"c}).Trim
    End Function
#End Region

#Region "Reply"
    Function Find_Reply_Items(ByVal Keyword As String) As String()
        Dim Data_Command As New System.Data.SQLite.SQLiteCommand(Data_Connection)
        Dim Result As New List(Of String)
        Data_Command.CommandText = "SELECT * FROM Reply WHERE (Keyword='" + Keyword + "')"
        With Data_Command.ExecuteReader()
            While .Read
                Result.Add(.Item("Answer"))
            End While
        End With
        Return Result.ToArray
    End Function

    Sub Add_Reply_Item(ByVal Param As String)
        Dim key, ans As String
        key = Param.Substring(0, InStr(Param, " ") - 1)
        ans = Param.Substring(key.Length).Trim
        Add_Reply_Item(key, ans)
    End Sub

    Sub Add_Reply_Item(ByVal Keyword As String, ByVal Answer As String)
        Dim Data_Command As New System.Data.SQLite.SQLiteCommand(Data_Connection)
        Data_Command.CommandText = "INSERT INTO Reply VALUES('" + Keyword + "','" + Answer + "')"
        Data_Command.ExecuteNonQuery()
    End Sub

    Sub Del_Reply_Item(ByVal Keyword As String)
        Dim Data_Command As New System.Data.SQLite.SQLiteCommand(Data_Connection)
        Data_Command.CommandText = "DELETE FROM Reply WHERE Keyword='" + Keyword + "'"
        Data_Command.ExecuteNonQuery()
    End Sub

    Sub Del_Reply_Item(ByVal Keyword As String, Answer As String)
        Dim Data_Command As New System.Data.SQLite.SQLiteCommand(Data_Connection)
        Data_Command.CommandText = "DELETE FROM Reply WHERE (Keyword='" + Keyword + "' AND Answer='" + Answer + "')"
        Data_Command.ExecuteNonQuery()
    End Sub
#End Region
End Module