using System;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Threading.Tasks;

namespace MosswartMassacre
{
    public static class UpdateManager
    {
        private const string UPDATE_URL = "https://git.snakedesert.se/SawatoMosswartsEnjoyersClub/MosswartMassacre/raw/branch/master/MosswartMassacre/bin/Release/MosswartMassacre.dll";
        
        private static bool updateAvailable = false;
        private static string remoteFileHash = string.Empty;
        private static string localFileHash = string.Empty;
        private static DateTime lastCheckTime = DateTime.MinValue;
        
        public static bool IsUpdateAvailable => updateAvailable;
        public static DateTime LastCheckTime => lastCheckTime;
        
        /// <summary>
        /// Calculate SHA256 hash of a file
        /// </summary>
        private static string CalculateFileHash(string filePath)
        {
            using (var sha256 = SHA256.Create())
            {
                using (var stream = File.OpenRead(filePath))
                {
                    byte[] hashBytes = sha256.ComputeHash(stream);
                    return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
                }
            }
        }
        
        /// <summary>
        /// Calculate SHA256 hash of byte array
        /// </summary>
        private static string CalculateHash(byte[] data)
        {
            using (var sha256 = SHA256.Create())
            {
                byte[] hashBytes = sha256.ComputeHash(data);
                return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
            }
        }
        
        public static async Task<bool> CheckForUpdateAsync()
        {
            try
            {
                PluginCore.WriteToChat("[Update] Checking for updates...");
                
                // Get local file hash
                string localPath = GetLocalDllPath();
                if (!File.Exists(localPath))
                {
                    PluginCore.WriteToChat("[Update] Error: Could not find local DLL file");
                    return false;
                }
                
                PluginCore.WriteToChat("[Update] Calculating local file hash...");
                localFileHash = CalculateFileHash(localPath);
                
                // Download remote file and calculate hash
                using (var client = new HttpClient())
                {
                    client.Timeout = TimeSpan.FromSeconds(30);
                    
                    PluginCore.WriteToChat("[Update] Downloading remote file for comparison...");
                    var remoteData = await client.GetByteArrayAsync(UPDATE_URL);
                    
                    if (remoteData == null || remoteData.Length == 0)
                    {
                        PluginCore.WriteToChat("[Update] Error: Could not download remote file");
                        return false;
                    }
                    
                    PluginCore.WriteToChat("[Update] Calculating remote file hash...");
                    remoteFileHash = CalculateHash(remoteData);
                }
                
                // Compare hashes
                updateAvailable = !string.Equals(localFileHash, remoteFileHash, StringComparison.OrdinalIgnoreCase);
                lastCheckTime = DateTime.Now;
                
                if (updateAvailable)
                {
                    PluginCore.WriteToChat($"[Update] Update available!");
                    PluginCore.WriteToChat($"[Update] Local hash:  {localFileHash}");
                    PluginCore.WriteToChat($"[Update] Remote hash: {remoteFileHash}");
                }
                else
                {
                    PluginCore.WriteToChat("[Update] Up to date - hashes match");
                }
                
                return true;
            }
            catch (HttpRequestException ex)
            {
                PluginCore.WriteToChat($"[Update] Network error: {ex.Message}");
                return false;
            }
            catch (TaskCanceledException)
            {
                PluginCore.WriteToChat("[Update] Request timed out");
                return false;
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"[Update] Check failed: {ex.Message}");
                return false;
            }
        }
        
        public static async Task<bool> DownloadAndInstallUpdateAsync()
        {
            if (!updateAvailable)
            {
                PluginCore.WriteToChat("[Update] No update available. Run /mm checkforupdate first.");
                return false;
            }
            
            try
            {
                PluginCore.WriteToChat("[Update] Downloading update...");
                
                string localPath = GetLocalDllPath();
                string tempPath = localPath + ".tmp";
                string backupPath = localPath + ".bak";
                
                // Download to temp file
                using (var client = new HttpClient())
                {
                    client.Timeout = TimeSpan.FromSeconds(30);
                    
                    var response = await client.GetAsync(UPDATE_URL);
                    response.EnsureSuccessStatusCode();
                    
                    using (var fileStream = File.Create(tempPath))
                    {
                        await response.Content.CopyToAsync(fileStream);
                    }
                }
                
                // Validate downloaded file by hash
                PluginCore.WriteToChat("[Update] Validating downloaded file...");
                var downloadedHash = CalculateFileHash(tempPath);
                if (!string.Equals(downloadedHash, remoteFileHash, StringComparison.OrdinalIgnoreCase))
                {
                    File.Delete(tempPath);
                    PluginCore.WriteToChat($"[Update] Download validation failed. Hash mismatch!");
                    PluginCore.WriteToChat($"[Update] Expected: {remoteFileHash}");
                    PluginCore.WriteToChat($"[Update] Got:      {downloadedHash}");
                    return false;
                }
                
                PluginCore.WriteToChat("[Update] Download complete, installing...");
                
                // Atomically replace current file with new version (creates backup automatically)
                File.Replace(tempPath, localPath, backupPath);
                
                // Clear update flag
                updateAvailable = false;
                
                PluginCore.WriteToChat("[Update] Update installed successfully!");
                PluginCore.WriteToChat("[Update] Previous version backed up as MosswartMassacre.dll.bak");
                
                // Wait a moment for file system to settle, then trigger hot reload
                await System.Threading.Tasks.Task.Delay(1000);
                
                try
                {
                    // Touch the file to ensure FileSystemWatcher detects the change
                    File.SetLastWriteTime(localPath, DateTime.Now);
                    PluginCore.WriteToChat("[Update] Triggering hot reload...");
                }
                catch (Exception ex)
                {
                    PluginCore.WriteToChat($"[Update] Could not trigger hot reload: {ex.Message}");
                    PluginCore.WriteToChat("[Update] Please use /mm gui to reload manually");
                }
                
                return true;
            }
            catch (HttpRequestException ex)
            {
                PluginCore.WriteToChat($"[Update] Download error: {ex.Message}");
                return false;
            }
            catch (TaskCanceledException)
            {
                PluginCore.WriteToChat("[Update] Download timed out");
                return false;
            }
            catch (UnauthorizedAccessException)
            {
                PluginCore.WriteToChat("[Update] File access denied. Make sure the plugin directory is writable.");
                return false;
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"[Update] Install failed: {ex.Message}");
                return false;
            }
        }
        
        /// <summary>
        /// Check for update and auto-install if available. Used by startup auto-update.
        /// </summary>
        public static async Task CheckAndInstallAsync()
        {
            try
            {
                bool checkOk = await CheckForUpdateAsync();
                if (checkOk && updateAvailable)
                {
                    PluginCore.WriteToChat("[Update] Auto-installing update...");
                    await DownloadAndInstallUpdateAsync();
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"[Update] Auto-update failed: {ex.Message}");
            }
        }

        private static string GetLocalDllPath()
        {
            // Get the path to the current DLL
            string assemblyPath = typeof(PluginCore).Assembly.Location;
            
            // If empty (hot reload scenario), use AssemblyDirectory + filename
            if (string.IsNullOrEmpty(assemblyPath))
            {
                return Path.Combine(PluginCore.AssemblyDirectory, "MosswartMassacre.dll");
            }
            
            return assemblyPath;
        }
        
        public static string GetUpdateStatus()
        {
            if (lastCheckTime == DateTime.MinValue)
            {
                return "Update Status: Not checked";
            }
            
            return updateAvailable ? "Update Status: Update available" : "Update Status: Up to date";
        }
    }
}