Imports System.Console

Module Module_Main
    Public QQ_Number, QQ_Password, AI, CMD_Password, CMD_Head, clientId As String
    Public AI_MainClass As Object = Nothing
    Public Client As WebQQ_Interface.WebQQ_Client

    Sub Load_Configuration()
        Randomize()

        Dim Xml_Config_Doc As New Xml.XmlDocument
        Dim Xml_Config_Root As Xml.XmlNode
        Xml_Config_Doc.Load(My.Application.Info.DirectoryPath + "\Robot_Config.xml")
        Xml_Config_Root = Xml_Config_Doc.LastChild

        QQ_Number = Xml_Config_Root.SelectSingleNode("QQ").InnerText
        QQ_Password = Xml_Config_Root.SelectSingleNode("Password").InnerText
        clientId = Xml_Config_Root.SelectSingleNode("ClientID").InnerText
        AI = Xml_Config_Root.SelectSingleNode("AI").InnerText
        CMD_Password = Xml_Config_Root.SelectSingleNode("CMD_Password").InnerText
        CMD_Head = Xml_Config_Root.SelectSingleNode("CMD_Head").InnerText

        If clientId = "" Then clientId = 800000 + Rnd() * 100000

        Try
            Dim AI_Assembly As Reflection.Assembly = Reflection.Assembly.LoadFile(My.Application.Info.DirectoryPath + "\" + AI)
            For Each MyType As Type In AI_Assembly.GetTypes()
                If MyType.Name = "AI" Then
                    AI_MainClass = AI_Assembly.CreateInstance(MyType.FullName)
                    Exit For
                End If
            Next
        Catch ex As Exception
            AI_MainClass = Nothing
        End Try
    End Sub

    Function Check_Network() As Boolean
        Return My.Computer.Network.IsAvailable
    End Function

    Sub Main()
        Console.Title = "QQ Robot By 周行（科创论坛金坷居士）"

        WriteLine("Loading configuration file.......")
        Load_Configuration()
        WriteLine()
        WriteLine("QQ_Number: " + QQ_Number)
        WriteLine("QQ_Password: " + QQ_Password)
        WriteLine("Administrator Password: " + CMD_Password)
        WriteLine()



        WriteLine()
        WriteLine("Check Network Conditions.......")
        Check_Network()

        Client = New WebQQ_Interface.WebQQ_Client(QQ_Number, QQ_Password, clientId, CMD_Password)
        Client.CMD_Head = CMD_Head

        WriteLine("Getting Verify code......")
        Client.Check_Verify_Code()
        WriteLine("Verify code: " + Client.Verify_Code)
        WriteLine()


        WriteLine("Connecting......")
        If Not Client.Connect() Then
            WriteLine(Err.Description)
            ReadLine()
            End
        End If

        WriteLine("ptwebqq: " + Client.ptwebqq)
        WriteLine("uin: " + Client.Cookies("uin").Value)
        WriteLine("skey: " + Client.Cookies("skey").Value)
        WriteLine("Welcome! " + Client.UserName)
        WriteLine("Connection established!")
        WriteLine()



        WriteLine("Logging on......")
        Client.Logon()
        WriteLine("psessionid: " + Client.psessionid)
        WriteLine()
        WriteLine("Successfully log on!")



        WriteLine()
        WriteLine("Getting friend list...")
        WriteLine(Client.Grab_Friend_List())


        WriteLine()
        WriteLine("Getting group list...")
        WriteLine(Client.Grab_Group_list())


        WriteLine()
        WriteLine("Start listening...")
        Client.Listening_Thread.Start()

        WriteLine()
        WriteLine("Registing system command......")
        WriteLine("System command(A qq message send from a client or group,starts with '" + CMD_Head + ".' ,like: " + CMD_Head + ".about):")
        WriteLine("about - Show software info and help")
        WriteLine("admin - Get administrator access of the robot")
        WriteLine("stop - Kill the robot (administrator access needed)")

        AddHandler Client.On_Command_Arrive, AddressOf Client.Def_On_Command_Arrive

        If AI_MainClass Is Nothing Then
            WriteLine("Could not load AI module, starts log on test mode...")
        Else
            WriteLine("AI Module: " + AI)
            WriteLine("AI initiallized!")

            WriteLine(AI_MainClass.Init(Client))
        End If

        WriteLine()
        If AI_MainClass IsNot Nothing Then
            WriteLine("Give the control to AI......")
        Else
            WriteLine("Finish log on test!")
        End If


        AddHandler Client.On_Log, Sub(txt As String)
                                      WriteLine(txt)
                                  End Sub
        AddHandler Client.On_Error, Sub(txt As String)
                                        WriteLine(txt)
                                    End Sub
        AddHandler Client.Ext_Poll_Proc, Sub(Message)
                                             Console.WriteLine(Message.ToString)
                                         End Sub

        WriteLine("Press any key to quit......")
        ReadLine()
        End
    End Sub

End Module
