﻿Partial Class order_centre_thank_you
    Inherits System.Web.UI.Page

    Private _numberOfFiles As String
    Private _fileSubmitted As String
    Private _totalFileSize As String

    Private _goPrint2Adapter As New com.racadtech.services.GoPrint2Client()
    Private _webWS As New WebWS()

    Public ReadOnly Property GoPrint2Adapter() As com.racadtech.services.GoPrint2Client
        Get
            Return _goPrint2Adapter
        End Get
    End Property
    Public ReadOnly Property MasterKey() As String
        Get
            Return "877k31hg333-d03423332wq-312dkazlfs-433398j543"
        End Get
    End Property
    Public ReadOnly Property CurrentActivationId() As String
        Get
            Return _webWS.GetActivationIdByStoreId(Session("StoreId")).ToString()
        End Get
    End Property
    Public ReadOnly Property CurrentJobId() As String
        Get
            Return Session("JobId").ToString()
        End Get
    End Property
    Public ReadOnly Property JobFilesDirectory() As String
        Get
            Return ConfigurationManager.AppSettings.Get("JobFilesPath").ToString()
        End Get
    End Property
    Public Property NumberOfFiles() As String
        Get
            Return _numberOfFiles
        End Get
        Set(ByVal value As String)
            _numberOfFiles = value
        End Set
    End Property
    Public Property FileSubmitted() As String
        Get
            Return _fileSubmitted
        End Get
        Set(ByVal value As String)
            _fileSubmitted = value
        End Set
    End Property
    Public Property TotalFileSize() As String
        Get
            Return _totalFileSize
        End Get
        Set(ByVal value As String)
            _totalFileSize = value
        End Set
    End Property
    Public ReadOnly Property CurrentUser() As String
        Get
            Return System.Web.HttpContext.Current.User.Identity.Name.ToString()
        End Get
    End Property
    Public ReadOnly Property CurrentUserId() As String
        Get
            Return Membership.GetUser(CurrentUser).ProviderUserKey.ToString()
        End Get
    End Property
    Public ReadOnly Property CurrentCustomerId() As String
        Get
            If Not Session("CustomerId") Is Nothing Then
                Return Session("CustomerId")
            Else
                Return _webWS.GetCustomerIdByUserId(CurrentUserId)
            End If
        End Get
    End Property
    Public ReadOnly Property CurrentInterfaceSettings() As com.racadtech.services.PrintDriverInterface
        Get
            Return GoPrint2Adapter.GetPrintDriverInterface(MasterKey, CurrentActivationId)            
        End Get
    End Property
    Public ReadOnly Property BaseImageURL() As String
        Get
            If Not Session("BaseImageURL") Is Nothing Then
                Return Session("BaseImageURL")
            Else
                Return "themes/orange/"
            End If
        End Get
    End Property

    Public ReadOnly Property RefererUrlStr() As String
        Get
            If Not Session("RefererURL") Is Nothing Then
                Return Session("RefererURL")
            Else
                Return "-Empty-"
            End If
        End Get
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load        
        Session("ThankYou") = "true"
        'Response.Write("Thank You " & Session("ThankYou"))


        If Not Page.IsPostBack Then
            ' Disable Language Panel.
            Dim _languagesPanel As Web.UI.WebControls.Panel = Me.Master.FindControl("LanguagesPanel")
            Dim _languagesSpacer As Web.UI.HtmlControls.HtmlGenericControl = Me.Master.FindControl("LanguagesSpacerLabel")
            _languagesPanel.Visible = False
            _languagesSpacer.InnerHtml = "<br /><br />"

            ' Clear Temp Job Id.
            Session("TempJobId") = Nothing

            ' Update Thank You Message.
            Me.UploadedFilesHeader.Text = CurrentInterfaceSettings.ThankYouMessage

            If (Not System.Web.HttpContext.Current.User.Identity.IsAuthenticated()) Then
                Response.Redirect("~/order_centre/login.aspx")
            End If

            Dim _jobId As Integer = Convert.ToInt32(Session("JobId"))

            TicketNumberLabel1.Text = _jobId
            lblUploadResult.Visible = True
            lblUploadResult.Text = UploadedFiles(_jobId)
            Dim DownloadId As String = GoPrint2Adapter.UpdateJobFilesSetDownloadId(_jobId, CurrentActivationId, MasterKey)
            Dim DownloadURL As String = "http://" + Request.Url.Host.ToString() + "/common/DownloadJob.aspx?downloadId=" + DownloadId

            Dim reciepient As String() = New String() {GoPrint2Adapter.GetEmailSettings(MasterKey, CurrentActivationId).EmailAddress}


            Dim _jobFilesDS As Data.DataSet = _webWS.GetFilesByJobId(CurrentJobId)
            NumberOfFiles = _jobFilesDS.Tables(0).Rows.Count.ToString()
            FileSubmitted = _jobFilesDS.Tables(0).Rows(0).Item("Filename").ToString()

            Dim _jobFilesDR As Data.DataTableReader
            _jobFilesDR = _jobFilesDS.CreateDataReader()

            Dim _totalFileSizeInt As Integer = 0
            While _jobFilesDR.Read
                _totalFileSizeInt = _totalFileSizeInt + Integer.Parse(_jobFilesDR.Item("FileSize").ToString())
            End While
            Dim _fileSizeInKiloBytes As Integer = _totalFileSizeInt / 1024
            Dim _fileSizeInMegaBytes As Integer = _totalFileSizeInt / 1024 / 1024
            If (_totalFileSizeInt < 1023) Then
                TotalFileSize = _totalFileSizeInt & "%20Bytes"
            ElseIf (_fileSizeInKiloBytes < 1024) Then
                TotalFileSize = _fileSizeInKiloBytes & "%20Kb"
            ElseIf (_fileSizeInKiloBytes >= 1024) Then
                TotalFileSize = _fileSizeInMegaBytes & "%20Mb"
            End If

            Dim CustomerInformation As com.racadtech.services.CustomerDetails = _
                                            GoPrint2Adapter.GetCustomerDetails(MasterKey, CurrentCustomerId)
            Dim _custEmail As String = CustomerInformation.Email.ToString()
	    Dim _custFirstname As String = CustomerInformation.Firstname.ToString()
            Dim _custLastname As String = CustomerInformation.Lastname.ToString()
            Try
                ' Send Email to Customer.
                GoPrint2Adapter.SendTicketEmailReferer(_custEmail, _
                                                GoPrint2Adapter.GetEmailSettings(MasterKey, CurrentActivationId).EmailAddress, _
                                                "Job Submitted [" & CurrentJobId.ToString() & "] [ " & _custFirstname & " " & _custLastname & " ]", CurrentActivationId, _
                                                CurrentCustomerId, CurrentJobId, NumberOfFiles, FileSubmitted, TotalFileSize, _
                                                "false", System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName, _
                                                MasterKey, RefererUrlStr)
            Catch ex As Exception
            End Try

            Try
                ' Send Email to Store Owner.
                Dim _productionEmail As String = GoPrint2Adapter.GetEmailSettings(MasterKey, CurrentActivationId).EmailAddress
                GoPrint2Adapter.SendTicketEmailReferer(_productionEmail, _
                                                _custEmail, "Job Submitted [" & CurrentJobId.ToString() & "] [ " & _custFirstname & " " & _custLastname & " ]", CurrentActivationId, _
                                                CurrentCustomerId, CurrentJobId, NumberOfFiles, FileSubmitted, TotalFileSize, _
                                                "true", Profile.Language, MasterKey, RefererUrlStr)

                Try
                    Dim _curMembershipUser As MembershipUser = Membership.GetUser(_webWS.GetUserIdByStoreId(Session("StoreId")))

                    If (Not _curMembershipUser.Email.Equals(_productionEmail)) Then
                        GoPrint2Adapter.SendTicketEmailReferer(_curMembershipUser.Email, _
                                                        "GoPrint2 <support@goprint2.com>", "Job Submitted [" & CurrentJobId.ToString() & "] [ " & _custFirstname & " " & _custLastname & " ]", CurrentActivationId, _
                                                          CurrentCustomerId, CurrentJobId, NumberOfFiles, FileSubmitted, TotalFileSize, _
                                                          "true", Profile.Language, MasterKey, RefererUrlStr)
                    End If
                Catch ex1 As Exception
                End Try

            Catch ex As Exception
            End Try
        End If
    End Sub

    Protected Function UploadedFiles(ByVal _jobId As Integer) As String
        Try
            Dim _message As String = ""

            _message = _message & "<table border='0' cellspacing='2' width='500' >" _
            & "<tr><td align='left' valign='top' class='files_uploaded_header'>" _
            & GetLocalResourceObject("FileNameColumn") _
            & "</td><td align='center' valign='top' class='files_uploaded_header'>" _
            & GetLocalResourceObject("FileSizeColumn") _
            & "</td></tr>"

            Dim _jobFilesDS As Data.DataSet = _webWS.GetFilesByJobId(_jobId)
            Dim _jobFilesDR As Data.DataTableReader
            _jobFilesDR = _jobFilesDS.CreateDataReader()

            While _jobFilesDR.Read
                Dim _fileSize As Integer = Integer.Parse(_jobFilesDR.Item("FileSize").ToString())
                Dim _fileSizeInKiloBytes As Integer = _fileSize / 1024
                Dim _fileSizeInMegaBytes As Integer = _fileSize / 1024 / 1024

                Dim _fileSizeStr As String = " "
                If (_fileSize < 1023) Then
                    _fileSizeStr = _fileSize & " Bytes"
                ElseIf (_fileSizeInKiloBytes < 1024) Then
                    _fileSizeStr = _fileSizeInKiloBytes & " Kb"
                ElseIf (_fileSizeInKiloBytes >= 1024) Then
                    _fileSizeStr = _fileSizeInMegaBytes & " Mb"
                End If

                _message = _message & "<tr><td align='left' valign='top' style='color: #ff0000;padding: 4px 4px 2px 0px;'>"
                If _jobFilesDR.Item("ServerLocation").ToString() <> "" Then
                    _message = _message & "<a target='_blank' href='" & _jobFilesDR.Item("ServerLocation") & "'>" & _
                                "<img src='img/icon_preview.gif' alt='Preview' border='0' style='vertical-align:middle'></a>&nbsp;"
                End If
                _message = _message & _jobFilesDR.Item("FileName") & "</td><td align='center' valign='top' style='color:#2f638d; padding: 4px 4px 2px 0px;'>" & _fileSizeStr & "</td></tr>"

                    'If _jobFilesDR.Item("FileSize") < 1024 Then
                    '    _message = _message & "<td align='center' valign='top' style='color:#2f638d; padding: 5px 5px 5px 5px;'>" & _jobFilesDR.Item("FileSize") & " byte</td>"
                    'ElseIf 1024 <= _jobFilesDR.Item("FileSize") < 1048576 Then
                    '    _message = _message & "<td align='center' valign='top' style='color:#2f638d; padding: 5px 5px 5px 5px;'>" & Math.Round((_jobFilesDR.Item("FileSize") / 1024), 2) & " Kb </td>"
                    'Else
                    '    _message = _message & "<td align='center' valign='top' style='color:#2f638d; padding: 5px 5px 5px 5px;'>" & Math.Round((_jobFilesDR.Item("FileSize") / 1048576), 2) & " Mb </td>"
                    'End If

            End While

            ''Dim _array As ArrayList = _commonFunctions.PopulateArray(_connectionString, "Select Filename From SDC_Job_Files Where JobId = " & _jobId)
            ''Dim _arrayMember As Object
            ''Dim i = 0
            ''For Each _arrayMember In _array
            ''    _message = _message & CType(_arrayMember, String) & "<br/>"
            ''Next

            ' 
            ' 

            _message = _message & "</table>"
            Return _message
        Catch ex As Exception
            Response.Redirect("~/customer/error_page.aspx")
            Return Nothing
        End Try

    End Function

    ' removing zip list files used by uploader to resume folders
    'Public Function RemoveSystemFiles() As Boolean
    '    Dim _result As Boolean = False
    '    Dim _path As String = JobFilesDirectory & "\" & CurrentJobId
    '    Dim _allFiles As String() = System.IO.Directory.GetFiles(_path)
    '    Response.Write(_path.Length & " <br/>")

    '    For Each _file As String In _allFiles
    '        Dim _fileName As String = _file.Substring((_path.Length + 1), (_file.Length - _path.Length - 1))
    '        Response.Write(_fileName & " <br/>")
    '        If _fileName.Substring(0, 7) = "gsezip_" Then
    '            Response.Write("Find file with gsezip_ prefix <br/>")
    '            Try
    '                System.IO.File.Delete(_file)
    '                _result = True
    '                Response.Write("Delete file with gsezip_ prefix <br/>")
    '            Catch ex As Exception
    '                _result = False
    '                Response.Write("Cannot delete file with gsezip_ prefix <br/>")
    '            End Try
    '        End If
    '    Next
    '    Return _result
    'End Function

End Class

