﻿Imports System.Net
Imports System.IO

Partial Class order_centre_view_ticket
    Inherits System.Web.UI.Page

    Private _webAdapter As New WebWS()
    Private _goPrint2Adapter As New com.racadtech.services.GoPrint2Client()
    Private _jobInformationHTML As String
    Private _numberOfFiles As String
    Private _fileSubmitted As String
    Private _totalFileSize As String
    Public ReadOnly Property WebAdapter() As WebWS
        Get
            Return _webAdapter
        End Get
    End Property
    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
            If Request("AID") Is Nothing Then
                If Session("StoreId") Is Nothing Then
                    Return WebAdapter.GetActivationIdByUserId(Membership.GetUser(CurrentUser).ProviderUserKey).ToString()
                Else
                    Return WebAdapter.GetActivationIdByStoreId(Session("StoreId")).ToString()
                End If
            Else
                Return Request("AID").ToString()
            End If
        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 CurrentJobId() As String
        Get
            If Request.QueryString("From") Is Nothing Then
                Return Session("JobId").ToString()
            Else
				If Request.QueryString("JobId") Is Nothing Then
					If Request.QueryString("DownloadId") Is Nothing
						Return ""
					Else
						Return WebAdapter.GetJobIdFromDownloadId(Request.QueryString("DownloadId"))
					End If
				Else 
					Return Request.QueryString("JobId")
				End If
            End If
        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 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 WebAdapter.GetCustomerIdByJobId(CurrentJobId)
            End If
        End Get
    End Property
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'http://localhost:1746/RacadService/Emails/GenerateOrderDisplay.aspx?
        'activationId=1a79236f-a871-4650-8fcc-86dbee935904&
        'customerId=f032a313-9b51-4fe5-9ee4-cf806531c305&
        'nof=1&
        'fileSubmitted=test.pdf&
        'totalFileSize=123456&
        'jobId=11421
		Dim _isStore = False
        If Request.QueryString("From") Is Nothing Then
            If (Not System.Web.HttpContext.Current.User.Identity.IsAuthenticated()) Then
                Response.Redirect("~/order_centre/login.aspx")
            End If
		Else
			If Request.QueryString("From").Equals("store") Then
				_isStore = True
			End If
        End If

        Dim _jobFilesDS As Data.DataSet = WebAdapter.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 _domain As String = Request.Url.Host.ToString().Split(".")(0)
        Dim _baseDomain As String = "services.racadtech.com"
        If _domain.StartsWith("localhost") Or _domain.Equals("beta") Or _domain.Equals("xerox-2") Or _domain.Equals("amram") Then
            _baseDomain = "beta.services.racadtech.com"
        End If

        Dim URL As String = "http://" & _baseDomain & "/RacadService/Emails/GenerateOrderDisplay.aspx?"
        URL = URL + "activationId=" & CurrentActivationId & "&"
        URL = URL + "customerId=" & CurrentCustomerId & "&"
        URL = URL + "jobId=" & CurrentJobId & "&"
        URL = URL + "nof=" & NumberOfFiles & "&"
        URL = URL + "lang=" & System.Threading.Thread.CurrentThread.CurrentCulture.Name & "&"
        URL = URL + "fileSubmitted=" & Server.UrlEncode(FileSubmitted) & "&"
        URL = URL + "totalFileSize=" & TotalFileSize
		If (_isStore) Then
			URL = URL + "&isstore=true"
		End If

        'Response.Write("URL = " & URL )
        Dim _request As HttpWebRequest = HttpWebRequest.Create(URL)
        _request.Method = WebRequestMethods.Http.Post
        _request.ContentLength = 0
        Dim _response As HttpWebResponse = _request.GetResponse()
        Dim _reader As New StreamReader(_response.GetResponseStream())
        Dim _tmp As String = _reader.ReadToEnd()
        _response.Close()
        Response.Write(_tmp)
    End Sub
End Class
