﻿
Partial Class order_centre_process_upload
    Inherits System.Web.UI.Page

    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 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

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.QueryString("UploadId") IsNot Nothing Then

            Dim _uploadId As String = Request.QueryString("UploadId").ToString()
            Dim _javaUploadTempLocation As String = ConfigurationManager.AppSettings("PrintItTempDataPath").ToString() & "\" & Request.QueryString("UploadId").ToString()
            Dim _allFiles As String()
            Dim _file As String

            If Session("TempJobId") Is Nothing Then
                Response.Redirect("login.aspx?ex=3")
            End If

            If System.IO.Directory.Exists(_javaUploadTempLocation) Then
                _allFiles = System.IO.Directory.GetFiles(_javaUploadTempLocation)

                ' removing zip list files used by uploader to resume folders
                For Each _file In _allFiles
                    Dim _fileName As String = _file.Substring((_javaUploadTempLocation.Length + 1), (_file.Length - _javaUploadTempLocation.Length - 1))
                    If _fileName.Substring(0, 7) = "gsezip_" Then
                        Try
                            System.IO.File.Delete(_file)
                        Catch ex As Exception
                        End Try
                    End If
                Next

                _allFiles = System.IO.Directory.GetFiles(_javaUploadTempLocation)
                Dim _files As String = ""
                For Each _file In _allFiles
                    Dim _fileObject As New System.IO.FileInfo(_file)
                    Dim _fileName As String = _fileObject.Name
                    Dim _fileSize As String = _fileObject.Length
                    _files = _files & _fileName & "@@@" & _fileSize & "%%%"
                Next

                Dim _jobId As String = Session("TempJobId")
                Session("JobId") = _jobId
                GoPrint2Adapter.UpdateNewJobFiles(_jobId, CurrentCustomerId, CurrentActivationId, _
                                     _files, "t", "f", "applet", MasterKey)

                Dim result As String = GoPrint2Adapter.UpdateJobFilesSetDownloadIdFromHttpUpload(_uploadId, _jobId, _
                                                                          CurrentActivationId, MasterKey)
                Dim message As String = "JobID: " & _jobId & "<br>"
                message += "Result: " & result

                'com.racadtech.Framework.Util.Mail.SMTPClient.sendMail(New String() {"amram@racadtech.com"}, "Applet Debug", message, True, "support@racadtech.com")

                If (result.ToLower().StartsWith("false")) Then
                    com.racadtech.Framework.Util.Mail.SMTPClient.sendMail(New String() {"support@racadtech.com"}, "FAILED APPLET UPLOAD!!!", message, True, "support@racadtech.com")
                End If

                Session("TempJobId") = Nothing
                Dim script As String = "<SCRIPT LANGUAGE='JavaScript'>window.onload=top.location.href = 'thank_you.aspx';</SCRIPT>"
                ClientScript.RegisterClientScriptBlock(Me.GetType(), "ClientScript", script)
            Else
                Session("TempJobId") = Nothing
                Response.Redirect("login.aspx?ex=1")
            End If
        Else
            Session("TempJobId") = Nothing
            Response.Redirect("login.aspx?ex=2")
        End If
    End Sub

End Class
