Imports com.racadtech.Framework.Database
Imports com.racadtech.Framework.Web.Security
Imports com.racadtech.services

Partial Class proba
    Inherits System.Web.UI.Page
    Dim _ws As WebWS = New WebWS()

    Private _webUserControl As New com.racadtech.Framework.Web.Security.UserControl()
    Private _goPrint2Adapter As New com.racadtech.services.GoPrint2Client()
    Private _customerDetails As New com.racadtech.services.CustomerDetails()
    Private _webAdapter As New WebWS()


    Public goPrint2_ConnectionString As String = ConfigurationManager.ConnectionStrings("PrintItConnectionString").ConnectionString
    Public epowerSDC_ConnectionString As String = ConfigurationManager.ConnectionStrings("EpowerSDCConnectionString").ConnectionString

    Public ReadOnly Property MembershipProviderInstance() As MembershipProvider
        Get
            Return Membership.Provider
        End Get
    End Property

    Public ReadOnly Property RoleProviderInstance() As RoleProvider
        Get
            Return Roles.Provider
        End Get
    End Property

    Public ReadOnly Property WebUserControl() As com.racadtech.Framework.Web.Security.UserControl
        Get
            Return _webUserControl
        End Get
    End Property

    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 Property CustomerInformation() As com.racadtech.services.CustomerDetails
        Get
            Return _customerDetails
        End Get
        Set(ByVal value As com.racadtech.services.CustomerDetails)
            _customerDetails = value
        End Set
    End Property
    Public ReadOnly Property MasterKey() As String
        Get
            Return "877k31hg333-d03423332wq-312dkazlfs-433398j543"
        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
            Return WebAdapter.GetCustomerIdByUserId(CurrentUserId)
        End Get
    End Property
    Public ReadOnly Property CurrentActivationId() As String
        Get
            Return WebAdapter.GetActivationIdByStoreId(Session("StoreId")).ToString()
        End Get
    End Property


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Dim _allUsers As MembershipUserCollection = Membership.GetAllUsers
        'Dim _curUserProfile As ProfileCommon
        'Dim _max = 0
        'For Each _existenUser As MembershipUser In _allUsers
        '    _curUserProfile = Profile.GetProfile(_existenUser.UserName)
        '    If _curUserProfile.PaymentTypeId <> 2 Then _curUserProfile.PaymentTypeId = 1
        '    _curUserProfile.Save()
        'Next

        '---------------------------'
        '---------------------------'

        'Dim _user As MembershipUser = Membership.GetUser("amram")
        'Dim _curUserProfile As ProfileCommon = Profile.GetProfile(_user.UserName)
        '_curUserProfile.StoreId = 601
        '_curUserProfile.Save()


        '---------------------------'
        '---------------------------'

        'Dim _ws As WebWS = New WebWS()
        'Dim _string As String = "871,873,872,874,875,876,877,878,879,880,881,882,883,884,885,886,887,888,889,890,891,892,893,894,895,896,897,898,899,900,901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,917,918,919,920,921,922,923,924,925,926,927,928,929,930,931,932,933,934,935,936,937,938,939,940,941,942"
        'Dim _arr() As String = _string.Split(",")

        'For i As Integer = 0 To _arr.Length - 1
        '    _ws.InsertSectionForStore(_arr(i), 1, False)
        '    '_ws.InsertSectionForStore(_arr(i), 2, False)
        '    '_ws.InsertSectionForStore(_arr(i), 3, False)
        '    '_ws.InsertSectionForStore(_arr(i), 4, False)
        '    '_ws.InsertSectionForStore(_arr(i), 5, False)
        'Next

        '---------------------------'
        '---------------------------'


        'Dim _allUsers As MembershipUserCollection = Membership.GetAllUsers
        'Dim _curUserProfile As ProfileCommon
        'For Each _user As MembershipUser In _allUsers
        '    'If _ws.GetCreditCardInformationByUserId(_user.ProviderUserKey).Tables(0).Rows.Count = 0 Then
        '    '    _curUserProfile = Profile.GetProfile(_user.UserName)
        '    '    _ws.InsertStartBillingDate(_user.ProviderUserKey, _curUserProfile.StartBillingDate, False)
        '    'End If
        '    _curUserProfile = Profile.GetProfile(_user.UserName)
        '    If _curUserProfile.StoreId = 871 Then
        '        Response.Write("UserName : " & _curUserProfile.UserName & "<br/>")
        '        Response.Write("FName : " & _curUserProfile.FName & "<br/>")
        '        Response.Write("LName : " & _curUserProfile.LName & "<br/>")
        '        Response.Write("StoreId : " & _curUserProfile.StoreId & "<br/>")
        '        Response.Write("StoreName : " & _curUserProfile.StoreName & "<br/>")
        '        Response.Write("Address : " & _curUserProfile.Address & "<br/>")
        '        Response.Write("Address1 : " & _curUserProfile.Address1 & "<br/>")
        '        Response.Write("City : " & _curUserProfile.City & "<br/>")
        '        Response.Write("Country : " & _curUserProfile.Country & "<br/>")
        '        Response.Write("Phone : " & _curUserProfile.Zip & "<br/>")
        '        Response.Write("Fax : " & _curUserProfile.Fax & "<br/>")
        '        Response.Write("PackageId : " & _curUserProfile.PackageId & "<br/>")
        '        Response.Write("StartBillingDate : " & _curUserProfile.StartBillingDate & "<br/>")

        '    End If
        'Next


        'Dim _allUsers As MembershipUserCollection = Membership.GetAllUsers
        'Dim _curUserProfile As ProfileCommon
        'Dim _max As Integer = 0
        'Dim _string As String = "871,873,872,874,875,876,877,878,879,880,881,882,883,884,885,886,887,888,889,890,891,892,893,894,895,896,897,898,899,900,901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,917,918,919,920,921,922,923,924,925,926,927,928,929,930,931,932,933,934,935,936,937,938,939,940,941,942"
        ''Dim _string As String = "871"
        'Dim _arr() As String = _string.Split(",")
        'For Each _existenUser As MembershipUser In _allUsers
        '    _curUserProfile = Profile.GetProfile(_existenUser.UserName)
        '    For i As Integer = 0 To _arr.Length - 1
        '        If _curUserProfile.StoreId = _arr(i) Then
        '            _curUserProfile.ExpirationDate = CType((Now().Month.ToString() & "/" & Now().Day.ToString() & "/" & (Convert.ToInt32(Now().Year) + 3).ToString()), Date)
        '            Exit For
        '        End If
        '    Next
        '    _curUserProfile.Save()
        'Next

        'Dim _ds As System.Data.DataSet = _ws.GetAllFiles()

        'For i As Integer = 0 To _ds.Tables(0).Rows.Count - 1
        '    If _ds.Tables(0).Rows(i).Item("Filename") = "filename1" And _ds.Tables(0).Rows(i).Item("Filesize") = 302131 Then
        '        _ws.GetValue("Delete From PI_Job_Information Where JobId=" & _ds.Tables(0).Rows(i).Item("JobId"))
        '        _ws.GetValue("Delete From PI_Job_Files Where JobId=" & _ds.Tables(0).Rows(i).Item("JobId"))
        '    End If
        'Next

        'Dim _activationKey As String = (New RandomKeyGenerator()).Generate("abcdefghijklmnopqrstuvwxyz", "0123456789", 15)

        'Response.Write(Format(Now.AddDays(30), "dd/MMM/yyyy"))

        Dim _label As Label = CType(Me.FindControl("Label1"), Label)
        _label.Text = ""

        Dim _textBox As TextBox = CType(Me.FindControl("TextBox1"), TextBox)
        If _textBox.Text <> "" Then
            Dim PI_StoreId As Integer = _textBox.Text
            Session("StoreId") = PI_StoreId
            Dim SDC_StoreId As Integer = GetValue("SELECT SDC_StoreId FROM [PrintIt].[dbo].[PI_SDC_StoresMapping] Where PI_StoreId='" & PI_StoreId.ToString() & "'")

            Dim _ds As System.Data.DataSet = GetAllCustomers(SDC_StoreId)
            Dim y As Integer = _ds.Tables(0).Rows.Count - 1

            For x As Integer = 0 To _ds.Tables(0).Rows.Count - 1
                Dim _userId As Guid = _ds.Tables(0).Rows(x)("UserId")
                Dim _ds_aspnet_Users As System.Data.DataSet = Get_aspnet_Users(_userId)
                Dim _userName As String = PI_StoreId.ToString() & "_" & _ds_aspnet_Users.Tables(0).Rows(0)("UserName")
                Dim _ds_aspnet_Membership As System.Data.DataSet = Get_aspnet_Membership(_userId)
                Dim _userDetails As UserDetails = _
                            WebUserControl.CreateUser(MembershipProviderInstance, _
                            RoleProviderInstance, _
                            _userName, _
                            "whatever", _
                            _ds_aspnet_Membership.Tables(0).Rows(0)("Email").ToString(), _
                            "PrintItWebCustomer")
                If _userDetails.ErrorCode > 0 Then
                    Response.Write("User " & _userName & " has not been created due to the following reason : " & _userDetails.ErrorMessage & "<br/>")
                Else
                    Response.Write("User " & _userName & " has been created successfully <br/>")

                    Dim _userProfile As ProfileCommon
                    _userProfile = ProfileCommon.Create(_userName)
                    _userProfile.StoreId = 999999
                    _userProfile.ExpirationDate = CType("12/31/2099", Date)
                    _userProfile.Save()

                    Dim _customerDetails As com.racadtech.services.CustomerDetails = GoPrint2Adapter.UpdateCustomerDetailsWithUserId("".ToString(), _userDetails.UserId.ToString(), CurrentActivationId.ToString(), _
                                _ds.Tables(0).Rows(x)("FName").ToString(), _
                                _ds.Tables(0).Rows(x)("LName").ToString(), _
                                _ds.Tables(0).Rows(x)("Company").ToString(), _
                                _ds.Tables(0).Rows(x)("Address").ToString(), _
                                _ds.Tables(0).Rows(x)("Address1").ToString(), _
                                _ds.Tables(0).Rows(x)("City").ToString(), _
                                _ds.Tables(0).Rows(x)("State").ToString(), _
                                _ds.Tables(0).Rows(x)("Zip").ToString(), _
                                _ds.Tables(0).Rows(x)("Country").ToString(), _
                                _ds.Tables(0).Rows(x)("Phone").ToString(), _
                                _ds.Tables(0).Rows(x)("Fax").ToString(), _
                                _ds_aspnet_Membership.Tables(0).Rows(0)("Email").ToString(), _
                                MasterKey.ToString())

                    GetValue("Update [PrintIt].[dbo].[aspnet_Membership] Set Password = '" _
                    & _ds_aspnet_Membership.Tables(0).Rows(0)("Password") & _
                    "',  PasswordSalt = '" & _ds_aspnet_Membership.Tables(0).Rows(0)("PasswordSalt") & _
                    "' Where UserId='" & _userDetails.UserId.ToString() & "'")
                End If
            Next
        Else
            _label.Text = "Store Number is Empty!"
        End If

        _textBox.Text = ""

    End Sub

    Public Function GetAllCustomers(ByVal storeId As Integer) As System.Data.DataSet

        Dim _dataSet As New System.Data.DataSet
        Using _connection As New System.Data.SqlClient.SqlConnection(epowerSDC_ConnectionString)
            Dim _command As New System.Data.SqlClient.SqlCommand()
            _command.CommandType = Data.CommandType.Text
            _command.Connection = _connection
            _command.CommandText = "Select * From SDC_UserDetails Where StoreId=@StoreId"
            _command.Parameters.AddWithValue("@StoreId", storeId)
            Dim _dataAdapter As New System.Data.SqlClient.SqlDataAdapter(_command)
            _dataAdapter.Fill(_dataSet, "AllCustomers")
        End Using
        Return _dataSet

    End Function


    Public Function Get_aspnet_Users(ByVal userId As Guid) As System.Data.DataSet
        Dim _dataSet As New System.Data.DataSet
        Using _connection As New System.Data.SqlClient.SqlConnection(epowerSDC_ConnectionString)
            Dim _command As New System.Data.SqlClient.SqlCommand()
            _command.CommandType = Data.CommandType.Text
            _command.Connection = _connection
            _command.CommandText = "Select * From aspnet_Users Where UserId=@UserId"
            _command.Parameters.AddWithValue("@UserId", userId)
            Dim _dataAdapter As New System.Data.SqlClient.SqlDataAdapter(_command)
            _dataAdapter.Fill(_dataSet, "AllCustomers")
        End Using
        Return _dataSet
    End Function

    Public Function Get_aspnet_Membership(ByVal userId As Guid) As System.Data.DataSet
        Dim _dataSet As New System.Data.DataSet
        Using _connection As New System.Data.SqlClient.SqlConnection(epowerSDC_ConnectionString)
            Dim _command As New System.Data.SqlClient.SqlCommand()
            _command.CommandType = Data.CommandType.Text
            _command.Connection = _connection
            _command.CommandText = "Select * From aspnet_Membership Where UserId=@UserId"
            _command.Parameters.AddWithValue("@UserId", userId)
            Dim _dataAdapter As New System.Data.SqlClient.SqlDataAdapter(_command)
            _dataAdapter.Fill(_dataSet, "AllCustomers")
        End Using
        Return _dataSet
    End Function

    Public Function GetValue(ByVal _commandString As String)
        Using _connection As New System.Data.SqlClient.SqlConnection(epowerSDC_ConnectionString)
            _connection.Open()
            Dim _command As New System.Data.SqlClient.SqlCommand()
            _command.CommandType = Data.CommandType.Text
            _command.Connection = _connection
            _command.CommandText = _commandString
            Try
                Return _command.ExecuteScalar()
                Return 1
            Catch ex As Exception
                Return 0
            End Try
            _connection.Close()
        End Using
    End Function

End Class
