﻿Imports System.Net
Imports System.IO
Imports System.Net.NetworkInformation
Imports System.Net.Mail

Public Class Form1
    Private WithEvents httpclient As WebClient
    Private WithEvents httpclient2 As WebClient
    Private WithEvents httpclient3 As WebClient
    Private WithEvents httpclient4 As WebClient
    Private WithEvents httpclient5 As WebClient
    Private Function getHTML(ByVal Address As String) As String
        Dim rt As String = ""
        Dim wRequest As WebRequest
        Dim wResponse As WebResponse
        Dim SR As StreamReader
        wRequest = WebRequest.Create(Address)
        wResponse = wRequest.GetResponse
        SR = New StreamReader(wResponse.GetResponseStream)
        rt = SR.ReadToEnd
        SR.Close()
        Return rt
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox7.Text = ""
        Dim searchSong As String = TextBox1.Text
        Dim newSearchSong As String = Replace(searchSong, " ", "_")
        Dim WebSearch As String = "http://mp3skull.com/mp3/" & newSearchSong & ".html"
        RichTextBox1.Text = getHTML(WebSearch)
        Try
            Dim strSource As String = RichTextBox1.Text
            Dim strSmaller As String = Mid(strSource, InStr(strSource, "http://dc"), 122)
            TextBox7.Text = strSmaller


            Dim sOne As String = TextBox7.Text
            Dim sOne2 As String = Mid(sOne, 110)
            Dim checkOne As String = TextBox14.Text
            If sOne2 = checkOne Then
                Dim sOne3 As String = Replace(sOne, checkOne, "/preview.mp3")
                TextBox7.Text = sOne3
            End If

            Dim sOne4444 As String = Mid(sOne, 117)
            If sOne4444 = "/previ" Then
                Dim sOne5555 As String = Replace(sOne, sOne4444, "/preview.mp3")
                TextBox7.Text = sOne5555
            End If

            Dim sOne444 As String = Mid(sOne, 116)
            If sOne444 = "/previe" Then
                Dim sOne555 As String = Replace(sOne, sOne444, "/preview.mp3")
                TextBox7.Text = sOne555
            End If

            Dim sOne44 As String = Mid(sOne, 115)
            If sOne44 = "/preview" Then
                Dim sOne55 As String = Replace(sOne, sOne44, "/preview.mp3")
                TextBox7.Text = sOne55
            End If

            Dim sOne4 As String = Mid(sOne, 114)
            If sOne4 = "/preview." Then
                Dim sOne5 As String = Replace(sOne, sOne4, "/preview.mp3")
                TextBox7.Text = sOne5
            End If

            Dim sOne6 As String = Mid(sOne, 113)
            If sOne6 = "/preview.m" Then
                Dim sOne7 As String = Replace(sOne, sOne6, "/preview.mp3")
                TextBox7.Text = sOne7
            End If

            Dim sOne8 As String = Mid(sOne, 112)
            If sOne8 = "/preview.mp" Then
                Dim sOne9 As String = Replace(sOne, sOne8, "/preview.mp3")
                TextBox7.Text = sOne9
            End If

            Dim sOne10 As String = Mid(sOne, 109)
            Dim checkOne2 As String = TextBox15.Text
            If sOne10 = checkOne2 Then
                Dim sOne11 As String = Replace(sOne, checkOne2, "/preview.mp3")
                TextBox7.Text = sOne11
            End If

            Dim sOne12 As String = Mid(sOne, 108)
            Dim checkOne3 As String = TextBox16.Text
            If sOne12 = checkOne3 Then
                Dim sOne13 As String = Replace(sOne, checkOne3, "/preview.mp3")
                TextBox7.Text = sOne13
            End If

            Dim sOne14 As String = Mid(sOne, 107)
            Dim checkOne4 As String = TextBox17.Text
            If sOne14 = checkOne4 Then
                Dim sOne15 As String = Replace(sOne, checkOne4, "/preview.mp3")
                TextBox7.Text = sOne15
            End If

            Dim sOne16 As String = Mid(sOne, 106)
            Dim checkOne5 As String = TextBox18.Text
            If sOne16 = checkOne5 Then
                Dim sOne17 As String = Replace(sOne, checkOne5, "/preview.mp3")
                TextBox7.Text = sOne17
            End If

            If TextBox7.Text = "" Then
                Label4.Visible = False
            Else
                Label4.Visible = True
                Timer1.Enabled = True
                Timer2.Enabled = True
            End If
        Catch ex As Exception
            MessageBox.Show("Critical error finding song. Please try again.")
        End Try
        ' strSmaller is rough download link
        ' Dim strSongName As String = TextBox2.Text ' this is equal to song name in text box 2
        ' Dim strNewSongName As String = Replace(strSmaller, "preview", strSongName)
    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        If TextBox2.Text = "" Then
            Button7.Enabled = False
        Else
            Button7.Enabled = True
        End If
    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
        If TextBox3.Text = "" Then
            Button8.Enabled = False
        Else
            Button8.Enabled = True
        End If
    End Sub

    Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
        If TextBox4.Text = "" Then
            Button9.Enabled = False
        Else
            Button9.Enabled = True
        End If
    End Sub

    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
        If TextBox5.Text = "" Then
            Button10.Enabled = False
        Else
            Button10.Enabled = True
        End If
    End Sub

    Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
        If TextBox6.Text = "" Then
            Button11.Enabled = False
        Else
            Button11.Enabled = True
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox8.Text = TextBox7.Text
        TextBox2.Text = TextBox1.Text
        If TextBox8.Text = "" Then
            MsgBox("No song file was accepted.")
        Else
            PictureBox1.Visible = True
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        TextBox9.Text = TextBox7.Text
        TextBox3.Text = TextBox1.Text
        If TextBox9.Text = "" Then
            MsgBox("No song file was accepted.")
        Else
            PictureBox2.Visible = True
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        TextBox10.Text = TextBox7.Text
        TextBox4.Text = TextBox1.Text
        If TextBox10.Text = "" Then
            MsgBox("No song file was accepted.")
        Else
            PictureBox3.Visible = True
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        TextBox11.Text = TextBox7.Text
        TextBox5.Text = TextBox1.Text
        If TextBox11.Text = "" Then
            MsgBox("No song file was accepted.")
        Else
            PictureBox4.Visible = True
        End If
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        TextBox12.Text = TextBox7.Text
        TextBox6.Text = TextBox1.Text
        If TextBox12.Text = "" Then
            MsgBox("No song file was accepted.")
        Else
            PictureBox5.Visible = True
        End If
    End Sub

    Private Sub Button12_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        Dim folderBrowser1 As New FolderBrowserDialog
        folderBrowser1.Description = "Choose default save location for MP3 files."
        folderBrowser1.ShowDialog()
        Dim defaultSelect As String = folderBrowser1.SelectedPath.ToString
        TextBox13.Text = defaultSelect
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Try
            If TextBox13.Text = "" Then
                MsgBox("Please choose a save location in the top right corner.")
            Else
                httpclient = New WebClient
                Dim sourceURL = TextBox8.Text
                Dim filedir = TextBox13.Text & "\" & TextBox2.Text & ".mp3"
                ProgressBar1.Value = 0
                ProgressBar1.Maximum = 100
                Label5.Text = TextBox2.Text
                Try
                    httpclient.DownloadFileAsync(New Uri(sourceURL), (filedir))
                Catch ex As Exception
                    MsgBox("failed" + ErrorToString(), MsgBoxStyle.Critical)
                End Try
            End If
        Catch ex As Exception
            MsgBox("No song file was accepted.")
        End Try
    End Sub
    Private Sub httpclient_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles httpclient.DownloadProgressChanged
        Label8.Text = ProgressBar1.Value & ("%")
        Dim firstNum As Double = e.TotalBytesToReceive / 1048576
        Label7.Text = "/" & Math.Round(firstNum, 2)
        Dim secNum As Double = e.BytesReceived / 1048576
        Label6.Text = Math.Round(secNum, 2)
        ProgressBar1.Value = e.ProgressPercentage
        If e.ProgressPercentage = ProgressBar1.Maximum Then
            Label8.Text = "Complete!"
            PictureBox11.Visible = True
        End If
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Try
            If TextBox13.Text = "" Then
                MsgBox("Please choose a save location in the top right corner.")
            Else
                httpclient2 = New WebClient
                Dim sourceURL = TextBox9.Text
                Dim filedir = TextBox13.Text & "\" & TextBox3.Text & ".mp3"
                ProgressBar4.Value = 0
                ProgressBar4.Maximum = 100
                Label24.Text = TextBox3.Text
                Try
                    httpclient2.DownloadFileAsync(New Uri(sourceURL), (filedir))
                Catch ex As Exception
                    MsgBox("failed" + ErrorToString(), MsgBoxStyle.Critical)
                End Try
            End If
        Catch ex As Exception
            MsgBox("No song file was accepted.")
        End Try
    End Sub
    Private Sub httpclient2_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles httpclient2.DownloadProgressChanged
        Label17.Text = ProgressBar4.Value & ("%")
        Dim firstNum As Double = e.TotalBytesToReceive / 1048576
        Label10.Text = "/" & Math.Round(firstNum, 2)
        Dim secNum As Double = e.BytesReceived / 1048576
        Label9.Text = Math.Round(secNum, 2)
        ProgressBar4.Value = e.ProgressPercentage
        If e.ProgressPercentage = ProgressBar4.Maximum Then
            Label17.Text = "Complete!"
            PictureBox12.Visible = True
        End If
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Try
            If TextBox13.Text = "" Then
                MsgBox("Please choose a save location in the top right corner.")
            Else
                httpclient3 = New WebClient
                Dim sourceURL = TextBox10.Text
                Dim filedir = TextBox13.Text & "\" & TextBox4.Text & ".mp3"
                ProgressBar2.Value = 0
                ProgressBar2.Maximum = 100
                Label23.Text = TextBox4.Text
                Try
                    httpclient3.DownloadFileAsync(New Uri(sourceURL), (filedir))
                Catch ex As Exception
                    MsgBox("failed" + ErrorToString(), MsgBoxStyle.Critical)
                End Try
            End If
        Catch ex As Exception
            MsgBox("No song file was accepted.")
        End Try
    End Sub
    Private Sub httpclient3_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles httpclient3.DownloadProgressChanged
        Label18.Text = ProgressBar2.Value & ("%")
        Dim firstNum As Double = e.TotalBytesToReceive / 1048576
        Label12.Text = "/" & Math.Round(firstNum, 2)
        Dim secNum As Double = e.BytesReceived / 1048576
        Label11.Text = Math.Round(secNum, 2)
        ProgressBar2.Value = e.ProgressPercentage
        If e.ProgressPercentage = ProgressBar2.Maximum Then
            Label18.Text = "Complete!"
            PictureBox13.Visible = True
        End If
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Try
            If TextBox13.Text = "" Then
                MsgBox("Please choose a save location in the top right corner.")
            Else
                httpclient4 = New WebClient
                Dim sourceURL = TextBox11.Text
                Dim filedir = TextBox13.Text & "\" & TextBox5.Text & ".mp3"
                ProgressBar5.Value = 0
                ProgressBar5.Maximum = 100
                Label22.Text = TextBox5.Text
                Try
                    httpclient4.DownloadFileAsync(New Uri(sourceURL), (filedir))
                Catch ex As Exception
                    MsgBox("failed" + ErrorToString(), MsgBoxStyle.Critical)
                End Try
            End If
        Catch ex As Exception
            MsgBox("No song file was accepted.")
        End Try
    End Sub
    Private Sub httpclient4_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles httpclient4.DownloadProgressChanged
        Label19.Text = ProgressBar5.Value & ("%")
        Dim firstNum As Double = e.TotalBytesToReceive / 1048576
        Label14.Text = "/" & Math.Round(firstNum, 2)
        Dim secNum As Double = e.BytesReceived / 1048576
        Label13.Text = Math.Round(secNum, 2)
        ProgressBar5.Value = e.ProgressPercentage
        If e.ProgressPercentage = ProgressBar5.Maximum Then
            Label19.Text = "Complete!"
            PictureBox14.Visible = True
        End If
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        Try
            If TextBox13.Text = "" Then
                MsgBox("Please choose a save location in the top right corner.")
            Else
                httpclient5 = New WebClient
                Dim sourceURL = TextBox12.Text
                Dim filedir = TextBox13.Text & "\" & TextBox6.Text & ".mp3"
                ProgressBar3.Value = 0
                ProgressBar3.Maximum = 100
                Label21.Text = TextBox6.Text
                Try
                    httpclient5.DownloadFileAsync(New Uri(sourceURL), (filedir))
                Catch ex As Exception
                    MsgBox("failed" + ErrorToString(), MsgBoxStyle.Critical)
                End Try
            End If
        Catch ex As Exception
            MsgBox("No song file was accepted.")
        End Try
    End Sub
    Private Sub httpclient5_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles httpclient5.DownloadProgressChanged
        Label20.Text = ProgressBar3.Value & ("%")
        Dim firstNum As Double = e.TotalBytesToReceive / 1048576
        Label16.Text = "/" & Math.Round(firstNum, 2)
        Dim secNum As Double = e.BytesReceived / 1048576
        Label15.Text = Math.Round(secNum, 2)
        ProgressBar3.Value = e.ProgressPercentage
        If e.ProgressPercentage = ProgressBar3.Maximum Then
            Label20.Text = "Complete!"
            PictureBox15.Visible = True
        End If
    End Sub

    Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click
        PictureBox1.Visible = False
        PictureBox11.Visible = False
        TextBox2.Text = ""
        TextBox8.Text = ""
        ProgressBar1.Value = 0
        Label5.Text = ""
        Label8.Text = "0%"
        Label6.Text = "0.00"
        Label7.Text = "/0.00"
    End Sub

    Private Sub PictureBox7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox7.Click
        PictureBox2.Visible = False
        PictureBox12.Visible = False
        TextBox3.Text = ""
        TextBox9.Text = ""
        ProgressBar4.Value = 0
        Label24.Text = ""
        Label17.Text = "0%"
        Label9.Text = "0.00"
        Label10.Text = "/0.00"
    End Sub

    Private Sub PictureBox8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox8.Click
        PictureBox3.Visible = False
        PictureBox13.Visible = False
        TextBox4.Text = ""
        TextBox10.Text = ""
        ProgressBar2.Value = 0
        Label23.Text = ""
        Label18.Text = "0%"
        Label11.Text = "0.00"
        Label12.Text = "/0.00"
    End Sub

    Private Sub PictureBox9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox9.Click
        PictureBox4.Visible = False
        PictureBox14.Visible = False
        TextBox5.Text = ""
        TextBox11.Text = ""
        ProgressBar5.Value = 0
        Label22.Text = ""
        Label19.Text = "0%"
        Label13.Text = "0.00"
        Label14.Text = "/0.00"
    End Sub

    Private Sub PictureBox10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox10.Click
        PictureBox5.Visible = False
        PictureBox15.Visible = False
        TextBox6.Text = ""
        TextBox12.Text = ""
        ProgressBar3.Value = 0
        Label21.Text = ""
        Label20.Text = "0%"
        Label15.Text = "0.00"
        Label16.Text = "/0.00"
    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        Dim openFileDialog1 As New OpenFileDialog
        openFileDialog1.Filter = "Mp3 files (*.mp3)|*.mp3|All files (*.*)|*.*"
        openFileDialog1.Title = "Choose Mp3 file to play."
        openFileDialog1.FileName = ""
        openFileDialog1.ShowDialog()
        AxWindowsMediaPlayer1.URL = openFileDialog1.FileName
        ListBox1.Items.Add(AxWindowsMediaPlayer1.URL)
    End Sub

    Private Sub PictureBox16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox16.Click
        ListBox1.Items.Remove(ListBox1.SelectedItem)
    End Sub

    Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
        Try
            AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
        Catch ex As Exception
            MsgBox("No file selected.")
        End Try
    End Sub

    Private Sub PictureBox11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox11.Click
        Try
            AxWindowsMediaPlayer1.URL = TextBox13.Text & "\" & TextBox2.Text & ".mp3"
            ListBox1.Items.Add(AxWindowsMediaPlayer1.URL)
        Catch ex As Exception
            MsgBox("Could not find file.")
        End Try
    End Sub

    Private Sub PictureBox12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox12.Click
        Try
            AxWindowsMediaPlayer1.URL = TextBox13.Text & "\" & TextBox3.Text & ".mp3"
            ListBox1.Items.Add(AxWindowsMediaPlayer1.URL)
        Catch ex As Exception
            MsgBox("Could not find file.")
        End Try
    End Sub

    Private Sub PictureBox13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox13.Click
        Try
            AxWindowsMediaPlayer1.URL = TextBox13.Text & "\" & TextBox4.Text & ".mp3"
            ListBox1.Items.Add(AxWindowsMediaPlayer1.URL)
        Catch ex As Exception
            MsgBox("Could not find file.")
        End Try
    End Sub

    Private Sub PictureBox14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox14.Click
        Try
            AxWindowsMediaPlayer1.URL = TextBox13.Text & "\" & TextBox5.Text & ".mp3"
            ListBox1.Items.Add(AxWindowsMediaPlayer1.URL)
        Catch ex As Exception
            MsgBox("Could not find file.")
        End Try
    End Sub

    Private Sub PictureBox15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox15.Click
        Try
            AxWindowsMediaPlayer1.URL = TextBox13.Text & "\" & TextBox6.Text & ".mp3"
            ListBox1.Items.Add(AxWindowsMediaPlayer1.URL)
        Catch ex As Exception
            MsgBox("Could not find file.")
        End Try
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Label4.Visible = True Then
            Label4.Visible = False
        ElseIf Label4.Visible = False Then
            Label4.Visible = True
        End If
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Timer1.Enabled = False
        Timer2.Enabled = False
    End Sub

    Private Sub PictureBox17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox17.Click
        RichTextBox2.Visible = True
        PictureBox17.Visible = False
        PictureBox18.Visible = True
        PictureBox19.Visible = True
    End Sub

    Private Sub PictureBox18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox18.Click
        If RichTextBox2.Text = "" Then
            MsgBox("Please write a message.")
        Else
            Dim Mail As New MailMessage
            Mail.Subject = "Tune Wire Response"
            Mail.To.Add("tune.wire2012@gmail.com")
            Mail.From = New MailAddress("tune.wire2012@gmail.com")
            Mail.Body = RichTextBox2.Text & vbNewLine & TimeOfDay & " " & DateString
            Dim SMTP As New SmtpClient("smtp.gmail.com")
            SMTP.EnableSsl = True
            'credentials go here
            SMTP.Port = "587"
            SMTP.Send(Mail)
            MsgBox("Message sent! Thanks for caring!")
            RichTextBox2.Visible = False
            PictureBox17.Visible = True
            PictureBox18.Visible = False
        End If
    End Sub
    Private Sub PictureBox19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox19.Click
        RichTextBox2.Visible = False
        PictureBox17.Visible = True
        PictureBox18.Visible = False
        PictureBox19.Visible = False
    End Sub
End Class

