using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FWHandler
{
    public class FileHandler
    {
        private static string[] lines;

        public static string ReadAppPath(string cesta)
        {
            if (FileHandler.ReadAllFile(cesta) != true)
            {
                Console.WriteLine("Error, file not found !");
            }

            return lines[0];
        }

        public static string ReadProjectPath(string cesta)
        {
            if (FileHandler.ReadAllFile(cesta) != true)
            {
                Console.WriteLine("Error, file not found !");
            }

            return lines[1];
        }

        private static bool ReadAllFile(string cesta)
        {
            if (File.Exists(cesta))
            {
                lines = File.ReadAllLines(cesta);
                return true;
            }

            return false;
        }

        public static void DeleteFile(string pathWay)
        {
            if (File.Exists(pathWay))
            {
                File.Delete(pathWay);
            }
            else
            {
                System.Diagnostics.Debug.Write("Error: File does not exist !");
            }
        }

        public static void UnzipFile(string pathWhat, string pathWhere)
        {
            if (pathWhat != "" && pathWhere != "")
            {
                using (ZipArchive archive = ZipFile.Open(pathWhat, ZipArchiveMode.Update))
                {
                    archive.ExtractToDirectory(pathWhere);
                }
            }
            else
            {
                System.Diagnostics.Debug.Write("Error: Wrong, or non existing path to zip file!");
            }
        }

        public static string ReadSpecificText(string path, int row)
        {
            if (File.Exists(path))
            {
                string line;
                return line = File.ReadLines(path).Skip(row).Take(1).First();
            }
            else
            {
                return "Error: Wrong, or non existing path to .txt file";
            }
        }
        public static List<string> ReadSpecificText(string path, string text)
        {
            List<string> listStr = new List<string>();
            string temp = string.Empty;
            if (File.Exists(path))
            {
                System.IO.StreamReader file = new System.IO.StreamReader(path);
                while (!file.EndOfStream) 
                {
                   temp = file.ReadLine();
                    if (temp.Contains(text))
                    {
                          listStr.Add(temp);
                    }
                }
            }
            return listStr;        
        }
       

        public static void DeleteAll(string path)
        {
            DirectoryInfo di = new DirectoryInfo(path);

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

        public static void WriteTo(string pathway, string text)
        {
            if(pathway != null)
            {
                File.WriteAllText(pathway, text);
            }
        }

        public static void FileCopy(string sourcePath, string destinationPath)
        {
            try
            {
                if (sourcePath != "" && destinationPath != "")
                {
                    File.Copy(sourcePath, destinationPath, true);
                }
            }
            catch (IOException ex)
            {
                System.Diagnostics.Debug.Write(ex);
            }
        }

        public static string ReadLastLine(string sourcePath)
        {
            if(File.Exists(sourcePath))
            {
                return File.ReadLines(sourcePath).Last();               
            }
            else
            {
                return null;
            }
        }
        public static string ReadFirstLine(string sourcePath)
        {
            if (File.Exists(sourcePath))
            {
                return File.ReadLines(sourcePath).First();
            }
            else
            {
                return null;
            }
        }


        public static string OpenLastCreatedFile(string sourcePath)
        {
            if (Directory.Exists(sourcePath))
            {
                DirectoryInfo info = new DirectoryInfo(sourcePath);
                FileInfo[] files = info.GetFiles().OrderBy(p => p.LastWriteTime).ToArray();
                int maxIndex = files.Length;
                FileInfo file = files[maxIndex - 1];
                return file.FullName;
            }
            else
            {
                return null;
            }
        }

        public static string FirstFileName(string sourcePath)
        {
            string firstFileName = string.Empty;
            if (Directory.Exists(sourcePath))
            {
                DirectoryInfo info = new DirectoryInfo(sourcePath);
                firstFileName =
                info.GetFiles()
                .Select(fi => fi.Name)
                .FirstOrDefault(name => name != "Thumbs.db");
            }

            return firstFileName;
        }

        public static void CopyAllFiles(string sourcePath, string destinationPath)
        {
            foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
                Directory.CreateDirectory(dirPath.Replace(sourcePath, destinationPath));


            foreach (string newPath in Directory.GetFiles(sourcePath, "*.*",
                SearchOption.AllDirectories))
                File.Copy(newPath, newPath.Replace(sourcePath, destinationPath), true);
        }
        public static bool IsDirectoryEmpty(string path)
        {
            return !Directory.EnumerateFileSystemEntries(path).Any();
        }



    }
}
