Imports Word = Microsoft.Office.Interop.Word
Public Class GUI_Export
    Private Shared gratingTable As New DataTable
    Public Shared Sub Generate_Quote()
        gratingTable = User_Input.Create_ExportTable()

        Dim doc As Word.Document = New Word.Document()

        'Page 1
        Create_HeaderFooter(doc)
        Create_QuoteInfo(doc)
        Create_ProductsInfo(doc)

        'Page 2-X
        doc.Words.Last.InsertBreak(Word.WdBreakType.wdPageBreak)
        Generate_Screenshot()
        Create_GratingPicture(doc)
        Create_GratingData(doc)

        doc.ExportAsFixedFormat(Settings.folderPaths("object_" & Data.objectNum & "_quote") & "\Specification.pdf", Word.WdExportFormat.wdExportFormatPDF)
        doc.Close(False)
    End Sub

    Private Shared Sub Generate_Screenshot()
        Dim gratingImg As New Bitmap(Individual.Panel_Grating.Width - 1, Individual.Panel_Grating.Height - 1)
        Dim graphicImg As Graphics = Graphics.FromImage(gratingImg)

        Draw_Grating.Draw_Grating(graphicImg, 12)

        gratingImg.Save(Settings.folderPaths("object_" & Data.objectNum & "_quote") & "\GUI.png", Imaging.ImageFormat.Png)

    End Sub

    Private Shared Sub Create_HeaderFooter(doc As Word.Document)
        doc.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = False

        'Header
        Dim headerTable As Word.Table
        headerTable = doc.Sections(1).Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.
            Tables.Add(doc.Sections(1).Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range, 1, 2)

        headerTable.Cell(1, 1).Range.InlineShapes.AddPicture(Settings.HLCtFolder & "\Files Needed\Weland_Logo.png")
        headerTable.Cell(1, 1).Range.InlineShapes(1).ScaleHeight = 50
        headerTable.Cell(1, 1).Range.InlineShapes(1).ScaleWidth = 50

        headerTable.Cell(1, 2).Range.Text = "QUOTE"
        headerTable.Cell(1, 2).Range.Font.Size = 18
        headerTable.Cell(1, 2).Range.Font.Bold = True
        headerTable.Cell(1, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
        headerTable.Cell(1, 2).VerticalAlignment = Word.WdVerticalAlignment.wdAlignVerticalBottom

        'Footer
        Dim footerTable As Word.Table
        footerTable = doc.Sections(1).Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.
            Tables.Add(doc.Sections(1).Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range, 3, 2)
        footerTable.Columns(1).SetWidth(360, 2)

        footerTable.Cell(1, 1).Range.Text = "Phone Number: "
        footerTable.Cell(2, 1).Range.Text = "Email: "
        footerTable.Cell(3, 1).Range.Text = "Webpage: "

        footerTable.Cell(1, 2).Range.Text = "0731-344 00"
        footerTable.Cell(2, 2).Range.Text = "info@weland.se"
        footerTable.Cell(3, 2).Range.Text = "www.weland.com"

        For i = 1 To footerTable.Rows.Count
            footerTable.Cell(i, 1).Range.Font.Bold = True
            For j = 1 To footerTable.Columns.Count
                footerTable.Cell(i, j).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
                footerTable.Cell(i, j).Range.Font.Size = 10
            Next
        Next
    End Sub

    Private Shared Sub Create_QuoteInfo(doc)
        Dim lineBreak As Word.Paragraph
        lineBreak = doc.Content.Paragraphs.Add
        lineBreak.Range.InsertParagraphBefore()
        lineBreak.Range.Text = ""
        lineBreak.Format.SpaceAfter = 20
        lineBreak.Range.InsertParagraphAfter()

        Dim infoTable As Word.Table
        Dim tableRange As Word.Range = doc.Bookmarks.Item("\endofdoc").Range
        infoTable = doc.Tables.Add(tableRange, 4, 2)
        infoTable.Columns(1).SetWidth(120, 2)
        infoTable.Rows.SetHeight(18, 2)

        Dim date2 As Date = Date.Today

        infoTable.Cell(1, 1).Range.Text = "Quote Date:"
        infoTable.Cell(1, 2).Range.Text = date2.ToString("dd-MM-yyyy")

        infoTable.Cell(2, 1).Range.Text = "Order number:"
        infoTable.Cell(2, 2).Range.Text = date2.ToString("ddMMyyyy") & Get_RandomNumber(10000, 99999)

        infoTable.Cell(3, 1).Range.Text = "Customer number:"
        infoTable.Cell(3, 2).Range.Text = Get_RandomNumber(10000, 99999)

        infoTable.Cell(4, 1).Range.Text = "Sales Representative:"
        infoTable.Cell(4, 2).Range.Text = "Sven Svensson"
    End Sub

    Private Shared Sub Create_ProductsInfo(doc)
        Dim lineBreak As Word.Paragraph
        Dim lineRange As Word.Range = doc.Bookmarks.Item("\endofdoc").Range
        lineBreak = doc.Content.Paragraphs.Add(lineRange)
        lineBreak.Range.InsertParagraphBefore()
        lineBreak.Range.Text = ""
        lineBreak.Format.SpaceAfter = 10
        lineBreak.Range.InsertParagraphAfter()

        Dim productsTable As Word.Table
        Dim tableRange As Word.Range = doc.Bookmarks.Item("\endofdoc").Range
        productsTable = doc.Tables.Add(tableRange, 5, 5) 'FIXA generiskt
        productsTable.Rows.SetHeight(18, 2)
        productsTable.Columns(1).SetWidth(240, 2)
        productsTable.Columns(2).SetWidth(78, 2)
        productsTable.Columns(3).SetWidth(40, 2)
        productsTable.Columns(4).SetWidth(60, 2)
        productsTable.Columns(5).SetWidth(52, 2)

        For c = 1 To productsTable.Columns.Count
            productsTable.Cell(1, c).Range.Font.Color = Word.WdColor.wdColorWhite
            productsTable.Cell(1, c).Shading.BackgroundPatternColor = Word.WdColor.wdColorBlack
            productsTable.Cell(1, c).VerticalAlignment = Word.WdVerticalAlignment.wdAlignVerticalBottom
            If c > 2 Then
                For r = 1 To productsTable.Rows.Count
                    productsTable.Cell(r, c).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
                Next
            End If
            If c = 5 Then
                For r = 2 To productsTable.Rows.Count
                    productsTable.Cell(r, c).Range.Font.Bold = True
                Next
            End If
        Next

        productsTable.Cell(1, 1).Range.Text = "Description"
        productsTable.Cell(1, 2).Range.Text = "Art. Nr."
        productsTable.Cell(1, 3).Range.Text = "QTY"
        productsTable.Cell(1, 4).Range.Text = "Unit Price"
        productsTable.Cell(1, 5).Range.Text = "Price"

        For i = 2 To productsTable.Rows.Count
            productsTable.Cell(i, 1).Range.Text = "Floor Grating " & gratingTable.Rows(0)("NAME") & " " &
                gratingTable.Rows(0)("LOADBAR_HEIGHT") & "/" & gratingTable.Rows(0)("LOADBAR_THICKNESS")
            If gratingTable.Rows(0)("SERRATED") Then
                productsTable.Cell(i, 2).Range.Text = gratingTable.Rows(0)("NAME").substring(0, 1) & "S" &
                    gratingTable.Rows(0)("LOADBAR_HEIGHT") & Get_RandomNumber(10000000, 99999999)
            Else
                productsTable.Cell(i, 2).Range.Text = gratingTable.Rows(0)("NAME") & gratingTable.Rows(0)("LOADBAR_HEIGHT") &
                    Get_RandomNumber(10000000, 99999999)
            End If
            productsTable.Cell(i, 3).Range.Text = 5.ToString("### ###")
            productsTable.Cell(i, 4).Range.Text = 20000.ToString("### ###")
            productsTable.Cell(i, 5).Range.Text = (CInt(Val(productsTable.Cell(i, 3).Range.Text)) * CInt(Val(productsTable.Cell(i, 4).Range.Text))).ToString("### ###")
            For j = 1 To productsTable.Columns.Count
                productsTable.Cell(i, j).Range.Font.Size = 10
            Next
        Next


        Dim lineBreak2 As Word.Paragraph
        lineRange = doc.Bookmarks.Item("\endofdoc").Range
        lineBreak2 = doc.Content.Paragraphs.Add(lineRange)
        lineBreak2.Range.InsertParagraphBefore()
        lineBreak2.Range.Text = ""
        lineBreak2.Format.SpaceAfter = 20
        lineBreak2.Range.InsertParagraphAfter()

        Dim priceTable As Word.Table
        tableRange = doc.Bookmarks.Item("\endofdoc").Range
        priceTable = doc.Tables.Add(tableRange, 1, 2)
        priceTable.Columns(1).SetWidth(370, 2)

        priceTable.Cell(1, 1).Range.Text = "Total Price:"
        priceTable.Cell(1, 1).Range.Font.Bold = True
        priceTable.Cell(1, 1).Range.Font.Size = 20

        Dim totPrice As Integer = 0
        For i = 2 To productsTable.Rows.Count


            totPrice += CInt(Val(productsTable.Cell(i, 5).Range.Text))
        Next
        priceTable.Cell(1, 2).Range.Text = totPrice.ToString("### ###")

        priceTable.Cell(1, 2).Range.Font.Bold = True
        priceTable.Cell(1, 2).Range.Font.Size = 20

        priceTable.Cell(1, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
        priceTable.Cell(1, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    End Sub

    Private Shared Sub Create_GratingPicture(doc As Word.Document)
        Dim drawingPara As Word.Paragraph
        Dim lineRange As Word.Range = doc.Bookmarks.Item("\endofdoc").Range
        drawingPara = doc.Content.Paragraphs.Add(lineRange)
        drawingPara.Range.InsertParagraphBefore()
        drawingPara.Format.SpaceBefore = 10
        drawingPara.Range.InlineShapes.AddPicture(Settings.folderPaths("object_" & Data.objectNum & "_quote") & "\GUI.png")
        drawingPara.Range.InlineShapes(1).ScaleHeight = 55
        drawingPara.Range.InlineShapes(1).ScaleWidth = 55
        drawingPara.Format.SpaceAfter = 1
        drawingPara.Range.InsertParagraphAfter()
    End Sub

    Private Shared Sub Create_GratingData(doc As Word.Document)
        Dim gratingInfoTable As Word.Table
        Dim tableRange As Word.Range = doc.Bookmarks.Item("\endofdoc").Range
        gratingInfoTable = doc.Tables.Add(tableRange, 10, 2)
        gratingInfoTable.Columns(1).SetWidth(80, 2)
        gratingInfoTable.Rows.SetHeight(18, 2)

        gratingInfoTable.Cell(1, 1).Range.Text = "Description: "
        gratingInfoTable.Cell(2, 1).Range.Text = "Type: "
        gratingInfoTable.Cell(3, 1).Range.Text = "Material: "
        gratingInfoTable.Cell(4, 1).Range.Text = "Mesh Size: "
        gratingInfoTable.Cell(5, 1).Range.Text = "Height: "
        gratingInfoTable.Cell(6, 1).Range.Text = "Thickness: "
        gratingInfoTable.Cell(7, 1).Range.Text = "Length: "
        gratingInfoTable.Cell(8, 1).Range.Text = "Width: "
        gratingInfoTable.Cell(9, 1).Range.Text = "Other: "

        For i = 1 To gratingInfoTable.Rows.Count
            gratingInfoTable.Cell(i, 1).Range.Font.Bold = True
        Next

        gratingInfoTable.Cell(1, 2).Range.Text = "Floor Grating " & gratingTable.Rows(0)("NAME") & " " &
                gratingTable.Rows(0)("LOADBAR_HEIGHT") & "/" & gratingTable.Rows(0)("LOADBAR_THICKNESS")
        If gratingTable.Rows(0)("TYPE") = "pressure_welded" Then
            gratingInfoTable.Cell(2, 2).Range.Text = "Pressure Welded"
        Else
            gratingInfoTable.Cell(2, 2).Range.Text = "Type A"
        End If

        gratingInfoTable.Cell(3, 2).Range.Text = gratingTable.Rows(0)("MATERIAL")
        gratingInfoTable.Cell(4, 2).Range.Text = gratingTable.Rows(0)("LOADBAR_SPACING") & "x" &
            gratingTable.Rows(0)("CROSSBAR_SPACING") & " (" & gratingTable.Rows(0)("NAME") & ")"
        gratingInfoTable.Cell(5, 2).Range.Text = gratingTable.Rows(0)("LOADBAR_HEIGHT") & "  [mm]"
        gratingInfoTable.Cell(6, 2).Range.Text = gratingTable.Rows(0)("LOADBAR_THICKNESS") & "  [mm]"
        gratingInfoTable.Cell(7, 2).Range.Text = gratingTable.Rows(0)("LENGTH") & "  [mm]"
        gratingInfoTable.Cell(8, 2).Range.Text = gratingTable.Rows(0)("WIDTH") & "  [mm]"

        If gratingTable.Rows(0)("SERRATED") AndAlso gratingTable.Rows(0)("LACQUERED") Then
            gratingInfoTable.Cell(9, 2).Range.Text = "Serrated, Lacquered"
        ElseIf gratingTable.Rows(0)("SERRATED") Then
            gratingInfoTable.Cell(9, 2).Range.Text = "Serrated"
        ElseIf gratingTable.Rows(0)("LACQUERED") Then
            gratingInfoTable.Cell(9, 2).Range.Text = "Lacquered"
        Else
            gratingInfoTable.Cell(9, 2).Range.Text = "-"
        End If

    End Sub


    Public Shared Function Get_RandomNumber(Min As Integer, Max As Integer) As Integer '
        Static Generator As System.Random = New System.Random()
        Return Generator.Next(Min, Max)
    End Function
End Class
