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

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

namespace SavePathDemo
{

    public class WebTest : BaseWebAiiTest
    {
        #region [ Dynamic Pages Reference ]

        private Pages _pages;

        /// <summary>
        /// Gets the Pages object that has references
        /// to all the elements, frames or regions
        /// in this project.
        /// </summary>
        public Pages Pages
        {
            get
            {
                if (_pages == null)
                {
                    _pages = new Pages(Manager.Current);
                }
                return _pages;
            }
        }

        #endregion
        
        // Add your test methods here...
        
        // -------- ENTER FOLDER PATH AND FILE TYPE HERE -----------//
        
        //enter folder path without dates (if not created locally it will be automatically), keep the "@"
        private string folderPath = @"C:\AK\Test Studio Tests\Demo Tests\SavePathDemo\SaveFolder";
        //enter file type with "." (dot) at the beggining
        private string fileType = ".pdf";
        private string fileName = "New_File_all_all_";
        
        //---------------------------------------------------------//
    
        [CodedStep(@"New Coded Step")]
        public void CraeteFilePath()
        {
            //creates the FILE path
            string folderPathDates = CreateFolderCurrentDate();
            int fileOrderNum = GetLastorderNum();
            if(fileOrderNum >= 1)
            {
                fileOrderNum++;
            }
            //create string builder for easier string manipulation
            StringBuilder sb = new StringBuilder();
            //add the leading zeros to the order num (e.g 001)
            sb.Append(folderPathDates);
            sb.Append(@"\");
            sb.Append(fileName);
            sb.Append(fileOrderNum.ToString("000"));
            sb.Append(fileType);
            
            //assigning name for the binding
            SetExtractedValue("DownloadFilePath", sb.ToString());            
        }
        
        //creates the folder path with current date
        private string CreateFolderCurrentDate()
        {
            //gets the current date
            DateTime currentDate = DateTime.Now;
            //sets the date in the wanted format
            string dateFormated = currentDate.ToString("yyyyMMdd");
            StringBuilder sb = new StringBuilder();
            //building the download file path
            sb.Append(folderPath);
            sb.Append("_");
            sb.Append(dateFormated);
            
            return sb.ToString();
        }
        
        private int GetLastorderNum()
        {
            //get all files from the folder which is used to save the downlaods
            var folder = CreateFolderCurrentDate();
            var fileNames = Directory.EnumerateFiles(folder).ToList<string>();
            int maxNum = int.MinValue;
            if(fileNames.Count == 0)
            {
                maxNum = 1;
            }
            else
            {
                int fileTypeLenght = fileType.Length;
                //goes through the files names and finds the last order number (001)
                foreach(var name in fileNames)
                {
                    //trims only the number (limitied to 999 (3 digits)
                    var orderNum = int.Parse(name.Substring(name.Length - (fileTypeLenght + 3),3));
                    if(orderNum > maxNum)
                    {
                        maxNum = orderNum;
                    }
                }
            }
            return maxNum;
        }
        
        
        //clears folder
        [CodedStep(@"New Coded Step")]
        public void ClearFolder()
        {
            DirectoryInfo di = new DirectoryInfo(CreateFolderCurrentDate());

            foreach (FileInfo file in di.GetFiles())
            {
                file.Delete(); 
            }
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                dir.Delete(true); 
            }
        }
    }
}
