using System;
using System.Collections;
using System.Diagnostics;
using System.IO;

namespace Globale_Variablen.Global
{
    public sealed class DirectoriesFiles
    {
        public static int DatensatzArchivieren(string ZIPPath, string WorkingDirectory, string ArchivName, string SourceName)
        {
            try
            {
                Process process = new Process();
                ProcessStartInfo startInfo = new ProcessStartInfo(ZIPPath) {
                    CreateNoWindow = true,
                    UseShellExecute = false,
                    WorkingDirectory = WorkingDirectory,
                    Arguments = " -add=update \"" + ArchivName + "\" \"" + SourceName + "\""
                };
                Process.Start(startInfo).WaitForExit();
                return 0;
            }
            catch (Exception exception)
            {
                //MessageBox.Show(exception.Message.ToString() + " [PE 3201]");
                return -1;
            }
        }

        public static int DatenSatzDearchivieren(string ZIPPath, string WorkingDirectory, string ArchivName, string SourceName, string DestDir)
        {
            XNRegistry registry = new XNRegistry(@"Votan_Applications\");
            string pat=System.Environment.CurrentDirectory;
            if (registry.IsOpen())
            {
                ZIPPath = (string)registry.GetVal(@"VOTANA\DirFiles", "DIR_ZipExe", "FALSE");
                string str = ZIPPath + "pkzip25.exe";
                if ((ZIPPath == "FALSE") || !File.Exists(str))
                {
                    ZIPPath = ((string)registry.GetVal(@"VOTANA\DirFiles", "DIR_ZipExeDV", "FALSE")) + @"\";
                }
            }
            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            string path = WorkingDirectory + @"\" + DestDir;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            //string str3 = ZIPPath + "pkzip25.exe";
            string str3 = pat + @"\pkzip25.exe";
            if (!File.Exists(str3))
            {
                //MessageBox.Show(str3);
                //MessageBox.Show("NOT Found");
            }
            startInfo = new ProcessStartInfo(str3) {
                WorkingDirectory = WorkingDirectory,
                Arguments = " -extract=up \"" + ArchivName + "\" \"" + DestDir + "\" \"" + SourceName + "\""
            };
            try
            {
                process = new Process();
                Process.Start(startInfo).WaitForExit();
            }
            catch (Exception exception)
            {
                //MessageBox.Show(exception.ToString() + " [PE 3200]");
            }
            return 0;
        }

        public static string FindDirectory(string SearchPath, string StartDirectory)
        {
            ArrayList dirs = new ArrayList();
            try
            {
                dirs = SearchDirectories(StartDirectory);
                if (dirs != null)
                {
                    return FindDirectoryFromList(dirs, SearchPath);
                }
                return string.Empty;
            }
            catch (Exception exception)
            {
                //MessageBox.Show(exception.StackTrace + " [PE 3202]");
                return string.Empty;
            }
        }

        private static string FindDirectoryFromList(ArrayList Dirs, string SearchPath)
        {
            foreach (string str2 in Dirs)
            {
                if (Path.GetFileNameWithoutExtension(str2).ToUpper() == SearchPath.ToUpper())
                {
                    return str2;
                }
            }
            return string.Empty;
        }

        public static ArrayList SearchDirectories(string StartDirectoy)
        {
            try
            {
                ArrayList list = new ArrayList();
                string[] directories = Directory.GetDirectories(StartDirectoy);
                int length = directories.Length;
                foreach (string str in directories)
                {
                    list.Add(str);
                    ArrayList c = new ArrayList();
                    c = SearchDirectories(str);
                    if (c.Count > 0)
                    {
                        list.AddRange(c);
                    }
                }
                return list;
            }
            catch (Exception exception)
            {
                //MessageBox.Show(exception.Message + " [PE 3203]");
                return null;
            }
        }
    }
}