Imports Telerik.TestStudio.Translators.Common
Imports Telerik.TestingFramework.Controls.TelerikUI.Blazor
Imports Telerik.TestingFramework.Controls.KendoUI.Angular
Imports Telerik.TestingFramework.Controls.KendoUI
Imports Telerik.WebAii.Controls.Html
Imports Telerik.WebAii.Controls.Xaml
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Linq
Imports System.IO

Imports ArtOfTest.Common.UnitTesting
Imports ArtOfTest.WebAii.Core
Imports ArtOfTest.WebAii.Controls.HtmlControls
Imports ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts
Imports ArtOfTest.WebAii.Design
Imports ArtOfTest.WebAii.Design.Execution
Imports ArtOfTest.WebAii.ObjectModel
Imports ArtOfTest.WebAii.Silverlight
Imports ArtOfTest.WebAii.Silverlight.UI
Imports ArtOfTest.WebAii.DesktopAutomation
Imports ArtOfTest.WebAii.DesktopAutomation.Controls
Imports ArtOfTest.WebAii.DesktopAutomation.FindExpressions

Namespace SavePathVB

    Public Class WebTest
        Inherits BaseWebAiiTest

#Region "[ Dynamic Pages Reference ]"

        Private _pages As Pages
        
        '''<summary>
        ''' Gets the Pages object that has references
        ''' to all the elements, frames or regions
        ''' in this project.
        '''</summary>
        Public ReadOnly Property Pages() As Pages
            Get
                If (_pages Is Nothing) Then
                    _pages = New Pages(Manager.Current)
                End If
                Return _pages
            End Get
        End Property
        
#End Region
        
        ' Add your test methods here...

        Private folderPath As String = "C:\AK\Test Studio Tests\Demo Tests\SavePathVB\SaveFolder"
        Private fileType As String = ".pdf"
        Private fileName As String = "New_File_all_all_"

       <CodedStep("New Coded Step")>
    Public Sub CraeteFilePath()
        Dim folderPathDates As String = CreateFolderCurrentDate()
        Dim fileOrderNum As Integer = GetLastorderNum()

        If fileOrderNum >= 1 Then
            fileOrderNum += 1
        End If

        Dim sb As StringBuilder = New StringBuilder()
        sb.Append(folderPathDates)
        sb.Append("\")
        sb.Append(fileName)
        sb.Append(fileOrderNum.ToString("000"))
        sb.Append(fileType)
        
        SetExtractedValue("DownloadFilePath", sb.ToString)
    End Sub

    Private Function CreateFolderCurrentDate() As String
        Dim currentDate As DateTime = DateTime.Now
        Dim dateFormated As String = currentDate.ToString("yyyyMMdd")
        Dim sb As StringBuilder = New StringBuilder()
        sb.Append(folderPath)
        sb.Append("_")
        sb.Append(dateFormated)
        Dim filePath = sb.ToString
        
        If Not Directory.Exists(filePath) Then
            Directory.CreateDirectory(filePath)
        End If
        
        Return filePath
    End Function

    Private Function GetLastorderNum() As Integer
        Dim folder = CreateFolderCurrentDate()
        Dim fileNames As List(Of String) = Directory.GetFiles(folder).ToList()
        Dim maxNum As Integer = Integer.MinValue

        If fileNames.Count = 0 Then
            maxNum = 1
        Else
            Dim fileTypeLenght As Integer = fileType.Length
            Dim name As String
            For Each name In fileNames
                Dim orderNum = Integer.Parse(name.Substring(name.Length - (fileTypeLenght + 3), 3))

                If orderNum > maxNum Then
                    maxNum = orderNum
                End If
            Next
        End If

        Return maxNum
    End Function

    <CodedStep("New Coded Step")>
    Public Sub ClearFolder()
        
        For Each file_path As String In Directory.GetFiles(CreateFolderCurrentDate)

            Try
               File.Delete(file_path)
            Catch ex As Exception

            End Try

        Next
     
    End Sub
    
End Class
End Namespace