Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml.Linq

Public Module BLWFile
    Public Sub SaveXML(fileName As String, sites As IList(Of LinkSite), keywords As IList(Of Keyword))
        Dim doc As New Xml.XmlDocument()
        Dim project = doc.CreateElement("BacklinksWarriorProject")
        Dim decl = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes")
        doc.AppendChild(decl)
        'project.SetAttribute()
        doc.AppendChild(project)

        For Each site In sites
            Dim item = doc.CreateElement("LinkSite")
            item.SetAttribute("Details", site.Notes)
            item.SetAttribute("Keyword", site.Keyword)
            item.SetAttribute("PageRank", site.PageRank)
            item.SetAttribute("Status", LinkStieStatusToString(site.Status))
            item.SetAttribute("URL", site.PageUrl)
            project.AppendChild(item)
        Next

        For Each keyword In keywords
            Dim item = doc.CreateElement("SearchTerm")
            item.SetAttribute("Keyword", keyword.Keyword)
            item.SetAttribute("ExcludeNoFollows", "True")
            item.SetAttribute("MaxResults", "100")
            project.AppendChild(item)
        Next

        Try
            doc.Save(fileName)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Unable to save file.")
        End Try
    End Sub

    Public Function LoadXML(fileName As String) As ProjectData
        Dim doc As XDocument = XDocument.Load(fileName)

        Dim sites = From s In doc.Descendants("LinkSite")
                    Select New LinkSite With {
                        .Notes = s.Attribute("Details"),
                        .Keyword = s.Attribute("Keyword"),
                        .PageRank = CInt(s.Attribute("PageRank")),
                        .Status = ParseLinkSiteStatusEnum(s.Attribute("Status")),
                        .PageUrl = s.Attribute("URL")
                    }

        Dim keywords = From k In doc.Descendants("SearchTerm")
                       Select New Keyword With {
                           .Keyword = k.Attribute("Keyword")
                       }

        Return New ProjectData With {.Sites = sites.ToList(), .Keywords = keywords.ToList()}
    End Function

    Public Sub SaveCSV(fileName As String, sites As IList(Of LinkSite))
        Try
            Using file As New IO.StreamWriter(fileName)
                ' Disable Google PR
                'If My.Settings.PREnabled Then
                '    file.WriteLine("Status,Keyword,PageRank,URL,Notes")

                '    For Each site In sites
                '        file.WriteLine("{0},{1},{2},{3},{4}", LinkStieStatusToString(site.Status), site.Keyword, site.PageRank, site.PageUrl, site.Notes)
                '    Next
                'Else
                file.WriteLine("Status,Keyword,URL,Notes")

                For Each site In sites
                    file.WriteLine("{0},{1},{2},{3}", LinkStieStatusToString(site.Status), site.Keyword, site.PageUrl, site.Notes)
                Next
                'End If

                file.Flush()
                file.Close()
            End Using
        Catch ex As IO.IOException
            MessageBox.Show(ex.Message, "Unable to save file")
        End Try
    End Sub

    Public Sub SaveHTML(fileName As String, sites As IList(Of LinkSite))
        Try
            Using file As New IO.StreamWriter(fileName)
                file.WriteLine("<html>")
                file.WriteLine("<head>")
                file.WriteLine("<title>Backlinks Warrior Report</title>")
                file.WriteLine("</head>")
                file.WriteLine("<body>")
                file.WriteLine("<p align=""center""><font face=""Verdana"" size=""2"">Backlinks Warrior Report<br>")
                file.WriteLine(String.Format("{0:M/dd/yyyy h:mm:ss tt}</font></p>", Date.Now))
                file.WriteLine("<div align=""center"">")
                file.WriteLine("<center>")
                file.WriteLine("<table border=""1"" cellspacing=""1"" style=""border-collapse: collapse"" bordercolor=""#111111"" width=""95%"" id=""AutoNumber1"">")
                file.WriteLine("<tr>")
                file.WriteLine("<td width=""20%"" bgcolor=""#0099FF""><b><font face=""Verdana"" size=""2"">Status</font></b></td>")

                ' Disable Google PR
                'If My.Settings.PREnabled Then
                '    file.WriteLine("<td width=""20%"" align=""center"" bgcolor=""#0099FF""><b>")
                '    file.WriteLine("<font face=""Verdana"" size=""2"">PageRank</font></b></td>")
                'End If

                file.WriteLine("<td width=""20%"" align=""center"" bgcolor=""#0099FF""><b>")
                file.WriteLine("<font face=""Verdana"" size=""2"">Keyword</font></b></td>")

                file.WriteLine("<td width=""20%"" align=""center"" bgcolor=""#0099FF""><b>")
                file.WriteLine("<font face=""Verdana"" size=""2"">URL</font></b></td>")

                file.WriteLine("<td width=""20%"" align=""center"" bgcolor=""#0099FF""><b>")
                file.WriteLine("<font face=""Verdana"" size=""2"">Notes</font></b></td>")
                file.WriteLine("</tr>")

                For Each site In sites
                    file.WriteLine("<tr>")
                    file.WriteLine(String.Format("<td width=""20%""><font face=""Verdana"" size=""2"">{0}</font></td>", LinkStieStatusToString(site.Status)))
                    file.WriteLine(String.Format("<td width=""20%"" align=""center""><font face=""Verdana"" size=""2"">{0}</font></td>", site.Keyword))

                    ' Disable Google PR
                    'If My.Settings.PREnabled Then
                    '    file.WriteLine(String.Format("<td width=""20%"" align=""center""><font face=""Verdana"" size=""2"">{0}</font></td>", site.PageRank))
                    'End If

                    file.WriteLine(String.Format("<td width=""20%"" align=""center""><font face=""Verdana"" size=""2""><a href=""{0}"">{0}</a></font></td>", site.PageUrl))
                    file.WriteLine(String.Format("<td width=""20%"" align=""center""><font face=""Verdana"" size=""2"">{0}</font></td>", site.Notes))
                    file.WriteLine("</tr>")
                Next

                file.WriteLine("</table>")
                file.WriteLine("</center>")
                file.WriteLine("</div>")
                file.WriteLine("</body>")
                file.WriteLine("</html>")

                file.Flush()
                file.Close()
            End Using
        Catch ex As IO.IOException
            MessageBox.Show(ex.Message, "Unable to save file")
        End Try
    End Sub

    Private Function ParseLinkSiteStatusEnum(s As String) As LinkSiteStatus
        Try
            Return [Enum].Parse(GetType(LinkSiteStatus), s, True)
        Catch ex As Exception
            Select Case s
                Case "Left Comment"
                    Return LinkSiteStatus.Added
                Case "Ignore"
                    Return LinkSiteStatus.Ignored
            End Select
        End Try

        Return LinkSiteStatus.New
    End Function

    Private Function LinkStieStatusToString(l As LinkSiteStatus) As String
        Select Case l
            Case LinkSiteStatus.Added
                Return "Left Comment"
            Case LinkSiteStatus.Ignored
                Return "Ignore"
            Case LinkSiteStatus.New
                Return "New"
            Case Else
                Return "New"
        End Select
    End Function

End Module

Public Class ProjectData
    Public Property Sites As IList(Of LinkSite)
    Public Property Keywords As IList(Of Keyword)
End Class