using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AVS
{
    class Updater
    {
        public string SVNversion;
        public string Softwareversion;
        SoftwareVersionUtility SoftwareVersionUtility = new SoftwareVersionUtility();

        public async Task CheckVersionAsync()
        {
            // Read version from SVN
            SVNversion = await SoftwareVersionUtility.CheckVersion();

            // Assembly version in properties
            Softwareversion = Assembly.GetExecutingAssembly().GetName().Version.ToString();

            // If versions are different, trigger the update
            if (SVNversion != Softwareversion)
            {
                DialogResult dialogResult = MessageBox.Show("A new version is available. Do you want to update now?",
                    "Update Available", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (dialogResult == DialogResult.Yes)
                {
                    // Start the update process
                    await StartUpdateProcessAsync();
                }
            }
        }

        public async Task StartUpdateProcessAsync()
        {
            try
            {
                string updaterPath = @"\\FileServer\eData\AVS\AVSUpdater.exe";

                // Copy the updater executable to a temporary location
                //if (!File.Exists(updaterPath))
                //{
                //    using (Stream resourceStream = Assembly.GetExecutingAssembly()
                //        .GetManifestResourceStream("AVS.Resources.AVSUpdater.exe"))
                //    {
                //        if (resourceStream != null)
                //        {
                //            using (FileStream fileStream = new FileStream(updaterPath, FileMode.Create, FileAccess.Write))
                //            {
                //                await resourceStream.CopyToAsync(fileStream);
                //            }
                //        }
                //        else
                //        {
                //            MessageBox.Show("Updater resource not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //            return;
                //        }
                //    }
                //}

                //string sourcePath = @"\\FileServer\eData\AVS";
                //string destinationPath = @"C:\Users\Public\Documents\AVS";

                // Start the updater process
                ProcessStartInfo processStartInfo = new ProcessStartInfo
                {
                    FileName = updaterPath,
                    //Arguments = $"\"{sourcePath}\" \"{destinationPath}\"",
                    UseShellExecute = false,
                    CreateNoWindow = true,
                };

                Process.Start(processStartInfo);

                // Close the current application
                await Task.Delay(3000); // Delay to allow the updater to initialize
                Environment.Exit(0);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Failed to start the update process: {ex.Message}", "Update Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}