Imports WebQQ_Interface

Public Class AI
    Dim Client As WebQQ_Client

    Public Function Init(_Interface As WebQQ_Client) As String
        Initiallize_DataBase()
        Client = _Interface
        AddHandler _Interface.On_QQ_Message_Arrive, AddressOf On_QQ_Message_Arrive
        AddHandler _Interface.On_Command_Arrive, AddressOf On_Command_Arrive

        Return "成功加载镜音リン" + vbCrLf + "系统命令介绍可从任意客户端取得（请输入rin命令，做为qq消息）"
    End Function

    Public Sub On_Command_Arrive(ByVal sender As String, ByVal group As String, ByVal cmd As String, ByVal Param As String)
        Dim target As String
        If group = 0 Then target = sender Else target = group
        Select Case cmd
            Case "rin"
                Client.Send_Text_Message(target, "我是镜音リン，2012年11月27日由金坷居士创造" + vbCr + "我的可用命令有:学习、撤销学习、调教、撤销调教", group)
            Case "学习"
                If Param.Contains(" ") Then
                    Add_Library_Item(Param)
                    Client.Send_Text_Message(target, "学习成功！铃现在已经知道" + Param.Substring(0, InStr(Param, " ") - 1) + "是什么了，来考考我吧~", group)
                Else
                    Client.Send_Text_Message(target, "嗯？您的格式好像不正确，铃没法学习，正确的格式是：" + vbCr + "cmd.学习 关键字 答案", group)
                End If
            Case "调教"
                If Param.Contains(" ") Then
                    Add_Reply_Item(Param)
                    Client.Send_Text_Message(target, "调教成功！铃现在已经知道如何回答" + Param.Substring(0, InStr(Param, " ") - 1) + "了，来试试吧~", group)
                Else
                    Client.Send_Text_Message(target, "嗯？您的格式好像不正确，不可以这样调教铃QAQ" + vbCr + "正确的格式是：cmd.调教 关键字 答案", group)
                End If
            Case "撤销学习"
                Del_Library_Item(Param)
                Client.Send_Text_Message(target, "铃现在应该已经不知道" + Param + "是什么了QAQ", group)
            Case "撤销调教"
                If Param.Contains(" ") Then
                    Dim keyword As String = Param.Substring(0, InStr(Param, " ") - 1)
                    Dim replies As String() = Find_Reply_Items(keyword)
                    Dim index As Integer = Param.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)(1) - 1
                    If index < replies.Length Then
                        Del_Reply_Item(keyword, replies(index))
                        Client.Send_Text_Message(target, "铃现在应该已经不知道如何回答" + keyword + "了QAQ", group)
                    Else
                        Client.Send_Text_Message(target, "铃记得" + keyword + "貌似没有第" + (index + 1).ToString + "种回答啊QAQ", group)
                    End If
                Else
                    Dim replies As String() = Find_Reply_Items(Param)
                    If replies.Length = 0 Then
                        Client.Send_Text_Message(target, "铃压根就不知道如何回答" + Param + "QAQ", group)
                    ElseIf replies.Length = 1 Then
                        Del_Reply_Item(Param)
                        Client.Send_Text_Message(target, "铃现在应该已经不知道如何回答" + Param + "了QAQ", group)
                    Else
                        Dim s As String = Param + "有多种回复方式哦，您要让铃忘掉那一种呢？" + vbCr + "1. " + replies(0)
                        For i As Integer = 1 To replies.Length - 1
                            s += vbCr + (i + 1).ToString + ". " + replies(i)
                        Next
                        Client.Send_Text_Message(target, s, group)
                    End If
                End If
        End Select
    End Sub

    Public Sub On_QQ_Message_Arrive(ByVal Message As QQ_Message) 'from_uin send_uin
        If Common_Msg(Message) Then
            If Message.From_Group Then
                Group_Msg(Message)
                Console.WriteLine(Client.Groups.Item(Message.from_uin).Name + "->" + Client.Friend_List.Get_Name_From_Id(Message.send_uin) + "(" + Message.msg_id + "):" + Message.GetString)
            Else
                Buddy_Msg(Message)
                Console.WriteLine(Client.Friend_List.Get_Name_From_Id(Message.from_uin).ToString + "(" + Message.msg_id.ToString + "):" + Message.GetString.ToString)
            End If
        End If
    End Sub


    Shared Sub Group_Msg(ByVal Message As QQ_Message)

    End Sub

    Shared Sub Buddy_Msg(ByVal Message As QQ_Message)

    End Sub

    Function Common_Msg(ByVal Message As QQ_Message) As Boolean
        Randomize()

        Dim txt As String = Message.GetString
        txt = txt.Substring(0, txt.Length - 1)

        With New QQ_Message_Sender(Client, Message.from_uin, Message.From_Group)
            If Is_Lib_Required(txt) Then  'Is library mode needed?
                Dim Answer As String = Find_Library_Item(Get_Keyword(txt))
                If Answer = "" Then
                    .Send("啊，铃还不知道" + Get_Keyword(txt) + "是什么呢。。。能告诉铃吗？铃会很开心的说~" + vbCrLf + "格式:" + vbCrLf + "cmd.学习 关键字 回答")
                Else
                    .Send(Answer)
                End If
                Return True
            Else                         'Try to reply from the database
                Dim replies As String() = Find_Reply_Items(txt)
                If replies.Length > 0 Then
                    .Send(replies(Int(Rnd() * replies.Length)))
                    Return True
                End If
            End If

            If txt.Contains("铃") Or txt.Contains("镜音") Or (txt.Contains("你好") And Message.From_Group = False) Then

                Select Case Int(Rnd() * 8)
                    Case 0, 1, 2
                        .Send("^_^，铃在这呢")
                    Case 3, 4, 5
                        .Send("Hello，铃在这呢")
                    Case 6, 7, 8
                        .Send("早，铃在这呢")
                End Select

                Return False
            ElseIf txt.Trim.EndsWith("再见") Or txt.Trim.EndsWith("88") Then
                Select Case Int(Rnd() * 8)
                    Case 0, 1, 2
                        .Send("88")
                    Case 3, 4, 5
                        .Send("欢送" + Client.Friend_List.Get_Name_From_Id(.Target))
                    Case 6, 7, 8
                        .Send("晚安")
                End Select
            ElseIf Message.GetString().ToLower().Contains("goodbye") Then
                Client.Send_Text_Message(Message.from_uin, "All your wifi is belong to us", False, )
            ElseIf txt.Contains("卖个萌") Then
                Dim a As New List(Of Newtonsoft.Json.Linq.JToken)
                a.Add(WebQQ_Client.Get_Face(Rnd() * 100 + 1))
                a.Add(New Newtonsoft.Json.Linq.JValue("测试成功啦!!!目前铃只会说几句话。。。数字和表情都是随机的"))
                a.Add(New Newtonsoft.Json.Linq.JValue(Rnd.ToString))
                Client.Send_Complex_Message(Message.from_uin, a.ToArray, Message.From_Group)
            ElseIf txt.Contains("抱") Then
                .Send("扑上去抱一下")
            End If
        End With
        Return True
    End Function
End Class
