using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AVS
{
    static class Program
    {

        public static string filePath = "C:\\Users\\Public\\Documents\\AVS\\credentials.txt";
        public static string username;
        public static bool CheckContainer;
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()       
        {
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            // Path to save the extracted updater file
            string updaterPath = Path.Combine(Path.GetTempPath(), "AVSUpdater.exe");

            //// Check if the updater file already exists
            //if (!File.Exists(updaterPath))
            //{
            //    // Access the embedded resource stream
            //    using (var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("AVS.AVSUpdater.exe"))
            //    {
            //        if (resourceStream != null)
            //        {
            //            // Create a new file in the temporary folder
            //            using (var fileStream = new FileStream(updaterPath, FileMode.Create, FileAccess.Write))
            //            {
            //                // Copy the content of the resource stream to the file
            //                resourceStream.CopyTo(fileStream);
            //            }

            //            Console.WriteLine($"Updater extracted to {updaterPath}");
            //        }
            //        else
            //        {
            //            Console.WriteLine("Embedded resource not found.");
            //        }
            //    }
            //}
            //else
            //{
            //    Console.WriteLine("Updater already exists.");
            //}


            //if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1)
            //    return;

            if (File.Exists(filePath))
            {

              tryReadCredentials();

                Application.Run(new MainForm(username,CheckContainer));
              
            }
            else
            {
             
                Application.Run(new LoginForm(null,false,"LOGIN"));
            }

                                
        }

        // Method to check if a form of a specific type is already open
        private static bool IsFormOpen(Type formType)
        {
            foreach (Form form in Application.OpenForms)
            {
                if (form.GetType() == formType)
                {
                    // Form of the specified type is already open
                    return true;
                }
            }
            // Form of the specified type is not open
            return false;
        }


        private static void tryReadCredentials()
        {
            // Initialize variables to store the values
            username = null;
            CheckContainer = false;

            try
            {
                // Check if the file exists
                if (File.Exists(filePath))
                {
                    // Read the lines from the file
                    string[] lines = File.ReadAllLines(filePath);

                    // Check if there are enough lines (username and password)
                    if (lines.Length >= 2)
                    {
                        username = lines[0];
                        CheckContainer = Convert.ToBoolean(lines[2]);
                       
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error reading credentials: " + ex.Message);
            }

           
        }

    }
}

