Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms

Imports Spread.Common.DataStore
Imports FarPoint.Win.Spread
Imports FarPoint.PDF
Imports Spread.PDFExport.CustomForm
Imports FarPoint.Win.Spread.CellType
Imports FarPoint.Win.Spread.Chart
Imports Spread.Common.DataStore.Models
Imports Spread.Common.Features.Spread.Common.Features.Sample

Partial Public Class CustomPDFExport
    Inherits BaseSampleControl
    Private _ownerPassword As String = Nothing
    Private _userPassword As String = Nothing
    Private _printInfo As PrintInfo
    Private _printingForm As PrintingInformation
    Public Sub New()
        InitializeComponent()
        InitializeControls()
        SetupSpreadSheet()
    End Sub
    Private Sub InitializeControls()
        _printInfo = New PrintInfo()
        Name = "Custom PDF Export"
        permissionComboBox.DroppedDown = False

        For Each item As Object In [Enum].GetValues(GetType(PdfPermissions))
            permissionListBox.Items.Add(item)
        Next
    End Sub

    Private Sub FlowPanel_SizeChanged(sender As Object, e As EventArgs)
        For Each control As Control In flowPanel.Controls
            control.Width = flowPanel.Width - 10
        Next
    End Sub

    Private Sub PermissionListBox_ItemCheck(sender As Object, e As ItemCheckEventArgs)
        If e.Index = permissionListBox.Items.Count - 1 Then
            If e.NewValue = CheckState.Checked Then
                For count As Integer = 0 To permissionListBox.Items.Count - 2
                    permissionListBox.SetItemChecked(count, True)
                Next
            Else
                For count As Integer = 0 To permissionListBox.Items.Count - 2
                    permissionListBox.SetItemChecked(count, False)
                Next
            End If
        End If
    End Sub

    Private Sub Controls_Click(sender As Object, e As MouseEventArgs)
        If Not permissionListBox.Bounds.Contains(e.Location) Then
            permissionListBox.Visible = False
        End If
    End Sub

    Private Sub PermissionComboBox_MouseClick(sender As Object, e As MouseEventArgs)
        permissionComboBox.DroppedDown = False
        permissionListBox.Visible = Not permissionListBox.Visible
    End Sub

    Private Sub SetupSpreadSheet()
        sheetView.ColumnHeader.Rows(0).Height = 30

        sheetView.DataAutoSizeColumns = False
        sheetView.Rows.[Default].Height = 50
        sheetView.DataSource = GetDataTable()
        sheetView.AlternatingRows(1).BackColor = Color.WhiteSmoke
        sheetView.Columns.Add(sheetView.Columns.Count, 1)
        sheetView.Columns(sheetView.Columns.Count - 1).Label = "Overall Result"
        sheetView.Columns(sheetView.Columns.Count - 1).Width = 120

        Dim cellRange As SheetCellRange() = New SheetCellRange(sheetView.Rows.Count - 1) {}
        Dim locationCellRange As SheetCellRange() = New SheetCellRange(sheetView.Rows.Count - 1) {}
        For row As Integer = 0 To sheetView.Rows.Count - 1
            cellRange(row) = New SheetCellRange(sheetView, row, 1, 1, sheetView.Columns.Count - 2)
            locationCellRange(row) = New SheetCellRange(sheetView, row, sheetView.Columns.Count - 1, 1, 1)
        Next
        Dim setting As New ExcelSparklineSetting()
        setting.ShowMarkers = True
        setting.ShowNegative = True
        setting.NegativeColor = Color.Red
        setting.SeriesColor = Color.Olive
        sheetView.AddSparkline(cellRange, locationCellRange, SparklineType.Column, setting)

    End Sub
    Private Function GetDataTable() As DataTable
        Dim students As List(Of Student) = New DataService().StudentDashboardDataSource.GetAllStudents()
        Dim table As New DataTable()
        table.Columns.Add("StudentName", GetType(String))
        table.Columns.Add("Physics", GetType(Integer))
        table.Columns.Add("Chemistry", GetType(Integer))
        table.Columns.Add("Math", GetType(Integer))
        table.Columns.Add("English", GetType(Integer))
        table.Columns.Add("Music", GetType(Integer))

        Dim random As New Random()
        For Each student As Student In students
            table.Rows.Add(student.Name, random.[Next](30, 100), random.[Next](30, 100), random.[Next](30, 100), random.[Next](30, 100), random.[Next](30, 100))
        Next
        Return table
    End Function
    Private Sub PrintInfoButton_Click(sender As Object, e As EventArgs)
        _printingForm = New PrintingInformation(sheetView, _printInfo)
        _printingForm.ShowDialog()
    End Sub

    Private Sub ExportButton_Click(sender As Object, e As EventArgs)
        If ownerTextBox.Text = "" OrElse userTextBox.Text = "" Then
            MessageBox.Show("Password can not be blank")
            Return
        End If
        If ownerTextBox.Text = userTextBox.Text Then
            MessageBox.Show("OwnerPassword and UserPassword can not be same")
            Return
        End If
        _ownerPassword = ownerTextBox.Text
        _userPassword = userTextBox.Text
        Dim saveFileDialog As New SaveFileDialog()
        saveFileDialog.Filter = "Pdf file (*.pdf)| *.pdf"
        saveFileDialog.Title = "Save Spread to PDF Format."
        If saveFileDialog.ShowDialog() = DialogResult.OK Then

            _printInfo.PrintToPdf = True
            _printInfo.PdfWriteMode = PdfWriteMode.[New]
            _printInfo.PdfWriteTo = PdfWriteTo.File
            _printInfo.PdfFileName = saveFileDialog.FileName
            _printInfo.PdfSecurity = New PdfSecurity(_ownerPassword, _userPassword, GetPermissions())
            _printInfo.PrintType = PrintType.CellRange
            sheetView.PrintInfo = _printInfo
            spread.PrintSheet(sheetView)

            ownerTextBox.Text = InlineAssignHelper(userTextBox.Text, "")
            permissionComboBox.SelectedIndex = permissionComboBox.Items.Count - 1
            MessageBox.Show("Sheet exported as PDF ")
        End If
    End Sub
    Private Function GetPermissions() As PdfPermissions
        Dim result As PdfPermissions = PdfPermissions.None
        For Each item As Object In permissionListBox.CheckedItems
            result = result Or CType(item, PdfPermissions)
        Next
        Return result
    End Function
    Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
        target = value
        Return value
    End Function

End Class

'
'CustomPDFExport.Designer.vb
'

Partial Class CustomPDFExport
	''' <summary> 
	''' Required designer variable.
	''' </summary>
	Private components As System.ComponentModel.IContainer = Nothing

    ''' <summary> 
    ''' Clean up any resources being used.
    ''' </summary>
    ''' <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    Protected Sub Dispose(disposing As Boolean)
        If disposing AndAlso (components IsNot Nothing) Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

#Region "Component Designer generated code"

    ''' <summary> 
    ''' Required method for Designer support - do not modify 
    ''' the contents of this method with the code editor.
    ''' </summary>
    Private Sub InitializeComponent()
		Me.spread = New FarPoint.Win.Spread.FpSpread()
		Me.sheetView = New FarPoint.Win.Spread.SheetView()
		Me.permissionListBox = New System.Windows.Forms.CheckedListBox()
		Me.permissionLabel = New System.Windows.Forms.Label()
		Me.permissionComboBox = New System.Windows.Forms.ComboBox()
		Me.userTextBox = New System.Windows.Forms.TextBox()
		Me.userLabel = New System.Windows.Forms.Label()
		Me.ownerTextBox = New System.Windows.Forms.TextBox()
		Me.ownerLabel = New System.Windows.Forms.Label()
		Me.printInfoButton = New System.Windows.Forms.Button()
		Me.flowPanel = New System.Windows.Forms.FlowLayoutPanel()
		Me.exportButton = New System.Windows.Forms.Button()
		Me.pnlMain.SuspendLayout()
		Me.pnlControls.SuspendLayout()
		DirectCast(Me.spread, System.ComponentModel.ISupportInitialize).BeginInit()
		DirectCast(Me.sheetView, System.ComponentModel.ISupportInitialize).BeginInit()
		Me.flowPanel.SuspendLayout()
		Me.SuspendLayout()
		' 
		' pnlMain
		' 
		Me.pnlMain.Controls.Add(Me.spread)
		' 
		' pnlControls
		' 
		Me.pnlControls.Controls.Add(Me.flowPanel)
		AddHandler Me.pnlControls.MouseClick, New System.Windows.Forms.MouseEventHandler(AddressOf Me.Controls_Click)
		' 
		' spread
		' 
		Me.spread.Dock = System.Windows.Forms.DockStyle.Fill
		Me.spread.Location = New System.Drawing.Point(0, 0)
		Me.spread.Name = "spread"
		Me.spread.Sheets.AddRange(New FarPoint.Win.Spread.SheetView() {Me.sheetView})
		Me.spread.Size = New System.Drawing.Size(477, 487)
		Me.spread.TabIndex = 0
		AddHandler Me.spread.MouseClick, New System.Windows.Forms.MouseEventHandler(AddressOf Me.Controls_Click)
		' 
		' sheetView
		' 
		Me.sheetView.Reset()
		Me.sheetView.SheetName = "CustomPDF"
		' Formulas and custom names must be loaded with R1C1 reference style
		Me.sheetView.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1
		Me.sheetView.Columns.[Default].Width = 90F
		Me.sheetView.Rows.[Default].Height = 25F
		Me.sheetView.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1
		' 
		' permissionListBox
		' 
		Me.permissionListBox.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
		Me.permissionListBox.FormattingEnabled = True
		Me.permissionListBox.Location = New System.Drawing.Point(3, 133)
		Me.permissionListBox.Margin = New System.Windows.Forms.Padding(3, 0, 3, 5)
		Me.permissionListBox.Name = "permissionListBox"
		Me.permissionListBox.Size = New System.Drawing.Size(143, 94)
		Me.permissionListBox.TabIndex = 15
		Me.permissionListBox.Visible = False
		AddHandler Me.permissionListBox.ItemCheck, New System.Windows.Forms.ItemCheckEventHandler(AddressOf Me.PermissionListBox_ItemCheck)
		' 
		' permissionLabel
		' 
		Me.permissionLabel.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
		Me.permissionLabel.Location = New System.Drawing.Point(3, 93)
		Me.permissionLabel.Margin = New System.Windows.Forms.Padding(3)
		Me.permissionLabel.Name = "permissionLabel"
		Me.permissionLabel.Size = New System.Drawing.Size(143, 13)
		Me.permissionLabel.TabIndex = 13
		Me.permissionLabel.Text = "Permission"
		AddHandler Me.permissionLabel.MouseClick, New System.Windows.Forms.MouseEventHandler(AddressOf Me.Controls_Click)
		' 
		' permissionComboBox
		' 
		Me.permissionComboBox.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
		Me.permissionComboBox.DropDownHeight = 1
		Me.permissionComboBox.FormattingEnabled = True
		Me.permissionComboBox.IntegralHeight = False
		Me.permissionComboBox.Location = New System.Drawing.Point(3, 112)
		Me.permissionComboBox.Margin = New System.Windows.Forms.Padding(3, 3, 3, 0)
		Me.permissionComboBox.Name = "permissionComboBox"
		Me.permissionComboBox.Size = New System.Drawing.Size(143, 21)
		Me.permissionComboBox.TabIndex = 12
		AddHandler Me.permissionComboBox.MouseClick, New System.Windows.Forms.MouseEventHandler(AddressOf Me.PermissionComboBox_MouseClick)
		' 
		' userTextBox
		' 
		Me.userTextBox.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
		Me.userTextBox.Location = New System.Drawing.Point(3, 67)
		Me.userTextBox.Name = "userTextBox"
		Me.userTextBox.Size = New System.Drawing.Size(143, 20)
		Me.userTextBox.TabIndex = 11
		AddHandler Me.userTextBox.MouseClick, New System.Windows.Forms.MouseEventHandler(AddressOf Me.Controls_Click)
		' 
		' userLabel
		' 
		Me.userLabel.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
		Me.userLabel.Location = New System.Drawing.Point(3, 48)
		Me.userLabel.Margin = New System.Windows.Forms.Padding(3)
		Me.userLabel.Name = "userLabel"
		Me.userLabel.Size = New System.Drawing.Size(143, 13)
		Me.userLabel.TabIndex = 10
		Me.userLabel.Text = "User Password"
		AddHandler Me.userLabel.MouseClick, New System.Windows.Forms.MouseEventHandler(AddressOf Me.Controls_Click)
		' 
		' ownerTextBox
		' 
		Me.ownerTextBox.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
		Me.ownerTextBox.Location = New System.Drawing.Point(3, 22)
		Me.ownerTextBox.Name = "ownerTextBox"
		Me.ownerTextBox.Size = New System.Drawing.Size(143, 20)
		Me.ownerTextBox.TabIndex = 9
		AddHandler Me.ownerTextBox.MouseClick, New System.Windows.Forms.MouseEventHandler(AddressOf Me.Controls_Click)
		' 
		' ownerLabel
		' 
		Me.ownerLabel.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
		Me.ownerLabel.Location = New System.Drawing.Point(3, 3)
		Me.ownerLabel.Margin = New System.Windows.Forms.Padding(3)
		Me.ownerLabel.Name = "ownerLabel"
		Me.ownerLabel.Size = New System.Drawing.Size(143, 13)
		Me.ownerLabel.TabIndex = 8
		Me.ownerLabel.Text = "Owner Password"
		AddHandler Me.ownerLabel.MouseClick, New System.Windows.Forms.MouseEventHandler(AddressOf Me.Controls_Click)
		' 
		' printInfoButton
		' 
		Me.printInfoButton.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
		Me.printInfoButton.Location = New System.Drawing.Point(3, 235)
		Me.printInfoButton.Name = "printInfoButton"
		Me.printInfoButton.Size = New System.Drawing.Size(143, 23)
		Me.printInfoButton.TabIndex = 16
		Me.printInfoButton.Text = "Configure PrintInfo"
		Me.printInfoButton.UseVisualStyleBackColor = True
		AddHandler Me.printInfoButton.Click, New System.EventHandler(AddressOf Me.PrintInfoButton_Click)
		' 
		' flowPanel
		' 
		Me.flowPanel.Controls.Add(Me.ownerLabel)
		Me.flowPanel.Controls.Add(Me.ownerTextBox)
		Me.flowPanel.Controls.Add(Me.userLabel)
		Me.flowPanel.Controls.Add(Me.userTextBox)
		Me.flowPanel.Controls.Add(Me.permissionLabel)
		Me.flowPanel.Controls.Add(Me.permissionComboBox)
		Me.flowPanel.Controls.Add(Me.permissionListBox)
		Me.flowPanel.Controls.Add(Me.printInfoButton)
		Me.flowPanel.Controls.Add(Me.exportButton)
		Me.flowPanel.Dock = System.Windows.Forms.DockStyle.Fill
		Me.flowPanel.FlowDirection = System.Windows.Forms.FlowDirection.TopDown
		Me.flowPanel.Location = New System.Drawing.Point(0, 0)
		Me.flowPanel.Name = "flowPanel"
		Me.flowPanel.Size = New System.Drawing.Size(157, 487)
		Me.flowPanel.TabIndex = 0
		AddHandler Me.flowPanel.SizeChanged, New System.EventHandler(AddressOf Me.FlowPanel_SizeChanged)
		' 
		' exportButton
		' 
		Me.exportButton.Location = New System.Drawing.Point(3, 264)
		Me.exportButton.Name = "exportButton"
		Me.exportButton.Size = New System.Drawing.Size(143, 23)
		Me.exportButton.TabIndex = 14
		Me.exportButton.Text = "Export"
		Me.exportButton.UseVisualStyleBackColor = True
		AddHandler Me.exportButton.Click, New System.EventHandler(AddressOf Me.ExportButton_Click)
		' 
		' CustomPDFExport
		' 
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.Name = "CustomPDFExport"
		Me.pnlMain.ResumeLayout(False)
		Me.pnlControls.ResumeLayout(False)
		DirectCast(Me.spread, System.ComponentModel.ISupportInitialize).EndInit()
		DirectCast(Me.sheetView, System.ComponentModel.ISupportInitialize).EndInit()
		Me.flowPanel.ResumeLayout(False)
		Me.flowPanel.PerformLayout()
		Me.ResumeLayout(False)

	End Sub

	#End Region
	Private spread As FarPoint.Win.Spread.FpSpread
	Private sheetView As FarPoint.Win.Spread.SheetView
	Private permissionListBox As System.Windows.Forms.CheckedListBox
	Private permissionLabel As System.Windows.Forms.Label
	Private permissionComboBox As System.Windows.Forms.ComboBox
	Private userTextBox As System.Windows.Forms.TextBox
	Private userLabel As System.Windows.Forms.Label
	Private ownerTextBox As System.Windows.Forms.TextBox
	Private ownerLabel As System.Windows.Forms.Label
	Private printInfoButton As System.Windows.Forms.Button
	Private flowPanel As System.Windows.Forms.FlowLayoutPanel
	Private exportButton As System.Windows.Forms.Button
End Class
