using System;
using System.Drawing;
using System.Timers;
using VirindiViewService.Controls;

namespace MosswartMassacre.Views
{
    /// <summary>
    /// VVS-based tabbed main view - direct VirindiViewService integration.
    /// Replaces wrapper-based TabbedMainView with modern VVS controls.
    /// </summary>
    internal class VVSTabbedMainView : VVSBaseView
    {
        private static VVSTabbedMainView instance;
        
        #region Main Tab Controls
        private HudStaticText lblTotalKills;
        private HudStaticText lblKillsPer5Min;
        private HudStaticText lblKillsPerHour;
        private HudStaticText lblElapsedTime;
        private HudStaticText lblRareCount;
        private HudStaticText lblAutoLootRare;
        private HudStaticText lblStatus;
        private HudStaticText lblWebSocketStatus;
        private HudButton btnCheckUpdate;
        private HudButton btnInstallUpdate;
        private HudStaticText lblUpdateStatus;
        #endregion
        
        #region Settings Tab Controls
        private HudCheckBox chkRareMetaEnabled;
        private HudCheckBox chkWebSocketEnabled;
        private HudTextBox txtCharTag;
        private HudTextBox txtVTankPath;
        #endregion
        
        #region Statistics Tab Controls
        private HudStaticText lblDetailedKillsValue;
        private HudStaticText lblBestHourValue;
        private HudStaticText lblAverageKillsValue;
        private HudStaticText lblRareStatsValue;
        private HudStaticText lblRareRateValue;
        private HudStaticText lblSessionTimeValue;
        private HudStaticText lblLastKillValue;
        private HudButton btnResetStats;
        #endregion
        
        #region Navigation Tab Controls (Basic)
        private HudCheckBox chkNavVisualizationEnabled;
        private HudCombo cmbNavFiles;
        private HudButton btnRefreshNavFiles;
        private HudStaticText lblNavStatus;
        private HudButton btnLoadRoute;
        private HudButton btnClearRoute;
        #endregion
        
        #region Flag Tracker Tab Controls
        private HudButton btnOpenFlagTracker;
        // lblFlagTrackerStatus removed - not present in XML
        #endregion

        #region Chest Looter Tab Controls
        private HudTextBox txtChestName;
        private HudTextBox txtKeyName;
        private HudButton btnSetChest;
        private HudButton btnSetKey;
        private HudButton btnStartLooter;
        private HudButton btnStopLooter;
        private HudCheckBox chkEnableChests;
        private HudStaticText lblLooterStatus;
        #endregion

        #region Statistics Tracking
        private double bestHourlyKills = 0;
        private DateTime sessionStartTime;
        #endregion

        public VVSTabbedMainView(PluginCore core) : base(core)
        {
            instance = this;
            sessionStartTime = DateTime.Now;
        }

        #region Static Interface Methods
        public static void ViewInit()
        {
            try
            {
                // Check if VVS is available
                try
                {
                    var testParser = new VirindiViewService.XMLParsers.Decal3XMLParser();
                }
                catch (Exception vvsEx)
                {
                    PluginCore.WriteToChat("[ERROR] VVS not available: " + vvsEx.Message);
                    return;
                }
                
                if (instance == null)
                {
                    instance = new VVSTabbedMainView(null);
                }
                
                instance.InitializeView();
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat("Error initializing VVS tabbed view: " + ex.Message);
                PluginCore.WriteToChat("Stack trace: " + ex.StackTrace);
            }
        }

        public static void ViewDestroy()
        {
            try
            {
                if (instance != null)
                {
                    instance.Dispose();
                    instance = null;
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat("Error destroying VVS tabbed view: " + ex.Message);
            }
        }
        #endregion

        #region Initialization
        private void InitializeView()
        {
            try
            {
                // Create view from working original XML layout
                CreateFromXMLResource("MosswartMassacre.ViewXML.mainViewTabbed.xml");

                if (view == null)
                {
                    PluginCore.WriteToChat("[ERROR] View creation failed - view is null!");
                    return;
                }

                // Initialize all tab controls
                InitializeMainTabControls();
                InitializeSettingsTabControls();
                InitializeStatisticsTabControls();
                InitializeNavigationTabControls();
                InitializeFlagTrackerTabControls();
                InitializeChestLooterTabControls();

                // Initialize the base view and set initial position
                Initialize();
                
                // Make the view visible and show in plugin bar
                if (view != null)
                {
                    view.Visible = true;
                    view.ShowInBar = true;
                    PluginCore.WriteToChat("GUI initialized successfully!");
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat("Error in VVS InitializeView: " + ex.Message);
                PluginCore.WriteToChat("Stack trace: " + ex.StackTrace);
            }
        }

        private void InitializeMainTabControls()
        {
            try
            {
                // Main tab - existing functionality using VVS controls
                lblTotalKills = GetControl<HudStaticText>("lblTotalKills");
                lblKillsPer5Min = GetControl<HudStaticText>("lblKillsPer5Min");
                lblKillsPerHour = GetControl<HudStaticText>("lblKillsPerHour");
                lblElapsedTime = GetControl<HudStaticText>("lblElapsedTime");
                lblRareCount = GetControl<HudStaticText>("lblRareCount");
                lblAutoLootRare = GetControl<HudStaticText>("lblAutoLootRare");
                lblStatus = GetControl<HudStaticText>("lblStatus");
                lblWebSocketStatus = GetControl<HudStaticText>("lblWebSocketStatus");
                btnCheckUpdate = GetControl<HudButton>("btnCheckUpdate");
                btnInstallUpdate = GetControl<HudButton>("btnInstallUpdate");
                lblUpdateStatus = GetControl<HudStaticText>("lblUpdateStatus");
                
                // Hook up update button events
                if (btnCheckUpdate != null)
                    btnCheckUpdate.Hit += OnCheckUpdateClicked;
                if (btnInstallUpdate != null)
                    btnInstallUpdate.Hit += OnInstallUpdateClicked;
                
                // Update status displays immediately
                UpdateStatus();
                UpdateAutoLootRareIndicator();
                UpdateWebSocketStatus();
                RefreshUpdateStatus();
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error initializing main tab controls: {ex.Message}");
            }
        }

        private void InitializeSettingsTabControls()
        {
            try
            {
                // Settings tab controls
                chkRareMetaEnabled = GetControl<HudCheckBox>("chkRareMetaEnabled");
                chkWebSocketEnabled = GetControl<HudCheckBox>("chkWebSocketEnabled");
                txtCharTag = GetControl<HudTextBox>("txtCharTag");
                txtVTankPath = GetControl<HudTextBox>("txtVTankPath");
                
                // Hook up settings events
                if (chkRareMetaEnabled != null)
                    chkRareMetaEnabled.Change += OnRareMetaSettingChanged;
                if (chkWebSocketEnabled != null)
                    chkWebSocketEnabled.Change += OnWebSocketSettingChanged;
                if (txtCharTag != null)
                    txtCharTag.Change += OnCharTagChanged;
                if (txtVTankPath != null)
                    txtVTankPath.Change += OnVTankPathChanged;
                
                // Load current settings
                LoadCurrentSettings();
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error initializing settings tab controls: {ex.Message}");
            }
        }

        private void InitializeStatisticsTabControls()
        {
            try
            {
                // Statistics tab controls
                lblDetailedKillsValue = GetControl<HudStaticText>("lblDetailedKillsValue");
                lblBestHourValue = GetControl<HudStaticText>("lblBestHourValue");
                lblAverageKillsValue = GetControl<HudStaticText>("lblAverageKillsValue");
                lblRareStatsValue = GetControl<HudStaticText>("lblRareStatsValue");
                lblRareRateValue = GetControl<HudStaticText>("lblRareRateValue");
                lblSessionTimeValue = GetControl<HudStaticText>("lblSessionTimeValue");
                lblLastKillValue = GetControl<HudStaticText>("lblLastKillValue");
                btnResetStats = GetControl<HudButton>("btnResetStats");
                
                // Hook up statistics events
                if (btnResetStats != null)
                    btnResetStats.Hit += OnResetStatsClick;
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error initializing statistics controls: {ex.Message}");
            }
        }

        private void InitializeNavigationTabControls()
        {
            try
            {
                // Basic navigation tab controls
                chkNavVisualizationEnabled = GetControl<HudCheckBox>("chkNavVisualizationEnabled");
                cmbNavFiles = GetControl<HudCombo>("cmbNavFiles");
                btnRefreshNavFiles = GetControl<HudButton>("btnRefreshNavFiles");
                lblNavStatus = GetControl<HudStaticText>("lblNavStatus");
                btnLoadRoute = GetControl<HudButton>("btnLoadRoute");
                btnClearRoute = GetControl<HudButton>("btnClearRoute");
                
                // Hook up basic navigation events
                if (chkNavVisualizationEnabled != null)
                    chkNavVisualizationEnabled.Change += OnNavVisualizationEnabledChanged;
                if (btnRefreshNavFiles != null)
                    btnRefreshNavFiles.Hit += OnRefreshNavFilesClick;
                if (btnLoadRoute != null)
                    btnLoadRoute.Hit += OnLoadRouteClick;
                if (btnClearRoute != null)
                    btnClearRoute.Hit += OnClearRouteClick;
                
                // Initialize navigation system
                InitializeNavigationSystem();
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error initializing navigation controls: {ex.Message}");
            }
        }

        private void InitializeFlagTrackerTabControls()
        {
            try
            {
                // Flag Tracker tab controls
                btnOpenFlagTracker = GetControl<HudButton>("btnOpenFlagTracker");

                // Hook up Flag Tracker events
                if (btnOpenFlagTracker != null)
                    btnOpenFlagTracker.Hit += OnOpenFlagTrackerClick;
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error initializing flag tracker controls: {ex.Message}");
            }
        }

        private void InitializeChestLooterTabControls()
        {
            try
            {
                // Chest Looter tab controls
                txtChestName = GetControl<HudTextBox>("txtChestName");
                txtKeyName = GetControl<HudTextBox>("txtKeyName");
                btnSetChest = GetControl<HudButton>("btnSetChest");
                btnSetKey = GetControl<HudButton>("btnSetKey");
                btnStartLooter = GetControl<HudButton>("btnStartLooter");
                btnStopLooter = GetControl<HudButton>("btnStopLooter");
                chkEnableChests = GetControl<HudCheckBox>("chkEnableChests");
                lblLooterStatus = GetControl<HudStaticText>("lblLooterStatus");

                // Hook up Chest Looter events
                if (btnSetChest != null)
                    btnSetChest.Hit += OnSetChestClick;
                if (btnSetKey != null)
                    btnSetKey.Hit += OnSetKeyClick;
                if (btnStartLooter != null)
                    btnStartLooter.Hit += OnStartLooterClick;
                if (btnStopLooter != null)
                    btnStopLooter.Hit += OnStopLooterClick;
                if (chkEnableChests != null)
                    chkEnableChests.Change += OnEnableChestsChanged;

                // Load current settings
                LoadChestLooterSettings();
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error initializing chest looter controls: {ex.Message}");
            }
        }
        #endregion

        #region Event Handlers - Settings Tab
        private void OnRareMetaSettingChanged(object sender, EventArgs e)
        {
            try
            {
                PluginSettings.Instance.RareMetaEnabled = chkRareMetaEnabled.Checked;
                PluginCore.RareMetaEnabled = chkRareMetaEnabled.Checked;
                UpdateAutoLootRareIndicator();
                PluginCore.WriteToChat($"Rare meta automation {(chkRareMetaEnabled.Checked ? "ENABLED" : "DISABLED")}.");
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error in rare meta setting change: {ex.Message}");
            }
        }

        private void OnWebSocketSettingChanged(object sender, EventArgs e)
        {
            try
            {
                PluginSettings.Instance.WebSocketEnabled = chkWebSocketEnabled.Checked;
                PluginCore.WebSocketEnabled = chkWebSocketEnabled.Checked;
                if (chkWebSocketEnabled.Checked)
                {
                    WebSocket.Start();
                    PluginCore.WriteToChat("WebSocket streaming ENABLED.");
                }
                else
                {
                    WebSocket.Stop();
                    PluginCore.WriteToChat("WebSocket streaming DISABLED.");
                }
                UpdateWebSocketStatus();
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error in WebSocket setting change: {ex.Message}");
            }
        }

        private void OnCharTagChanged(object sender, EventArgs e)
        {
            try
            {
                PluginSettings.Instance.CharTag = txtCharTag.Text;
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error in char tag change: {ex.Message}");
            }
        }

        private void OnVTankPathChanged(object sender, EventArgs e)
        {
            try
            {
                PluginSettings.Instance.VTankProfilesPath = txtVTankPath.Text;
                // Refresh navigation system with new path
                if (PluginCore.navVisualization != null)
                {
                    PluginCore.navVisualization.RefreshNavFileList();
                    RefreshNavFileList();
                    UpdateNavigationStatus();
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error in VTank path change: {ex.Message}");
            }
        }

        private void OnCheckUpdateClicked(object sender, EventArgs e)
        {
            try
            {
                // Run update check asynchronously
                System.Threading.Tasks.Task.Run(async () =>
                {
                    await UpdateManager.CheckForUpdateAsync();
                    // Update UI (VVS controls are thread-safe for simple property updates)
                    try
                    {
                        RefreshUpdateStatus();
                    }
                    catch (Exception ex)
                    {
                        PluginCore.WriteToChat($"Error updating UI after check: {ex.Message}");
                    }
                });
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error checking for updates: {ex.Message}");
            }
        }

        private void OnInstallUpdateClicked(object sender, EventArgs e)
        {
            try
            {
                // Check if update check was performed first
                if (UpdateManager.LastCheckTime == DateTime.MinValue)
                {
                    PluginCore.WriteToChat("[Update] Check for updates first");
                    return;
                }
                
                if (!UpdateManager.IsUpdateAvailable)
                {
                    PluginCore.WriteToChat("[Update] No update available");
                    return;
                }
                
                // Run update installation asynchronously
                System.Threading.Tasks.Task.Run(async () =>
                {
                    await UpdateManager.DownloadAndInstallUpdateAsync();
                    // Update UI (VVS controls are thread-safe for simple property updates)
                    try
                    {
                        RefreshUpdateStatus();
                    }
                    catch (Exception ex)
                    {
                        PluginCore.WriteToChat($"Error updating UI after install: {ex.Message}");
                    }
                });
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error installing update: {ex.Message}");
            }
        }

        public static void RefreshUpdateStatus()
        {
            try
            {
                if (instance?.lblUpdateStatus != null)
                {
                    if (UpdateManager.LastCheckTime == DateTime.MinValue)
                    {
                        // Not checked yet
                        instance.lblUpdateStatus.Text = "";
                    }
                    else if (UpdateManager.IsUpdateAvailable)
                    {
                        // Update available
                        instance.lblUpdateStatus.Text = "Update available";
                    }
                    else
                    {
                        // Up to date
                        instance.lblUpdateStatus.Text = "Up to date";
                    }
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error refreshing update status: {ex.Message}");
            }
        }

        public void RefreshUpdateStatusInstance()
        {
            RefreshUpdateStatus();
        }

        public static void DebugUpdateControls()
        {
            try
            {
                PluginCore.WriteToChat("=== Update UI Debug (Main Tab) ===");
                PluginCore.WriteToChat($"Instance exists: {instance != null}");
                if (instance != null)
                {
                    PluginCore.WriteToChat($"btnCheckUpdate: {instance.btnCheckUpdate != null}");
                    PluginCore.WriteToChat($"btnInstallUpdate: {instance.btnInstallUpdate != null}");
                    PluginCore.WriteToChat($"lblUpdateStatus: {instance.lblUpdateStatus != null}");
                    
                    if (instance.lblUpdateStatus != null)
                    {
                        PluginCore.WriteToChat($"Current status text: '{instance.lblUpdateStatus.Text}'");
                    }
                    
                    PluginCore.WriteToChat($"UpdateManager.LastCheckTime: {UpdateManager.LastCheckTime}");
                    PluginCore.WriteToChat($"UpdateManager.IsUpdateAvailable: {UpdateManager.IsUpdateAvailable}");
                    
                    // Try to force refresh
                    RefreshUpdateStatus();
                    PluginCore.WriteToChat("Forced refresh completed");
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Debug error: {ex.Message}");
            }
        }

        private void LoadCurrentSettings()
        {
            try
            {
                if (PluginSettings.Instance != null)
                {
                    if (chkRareMetaEnabled != null)
                        chkRareMetaEnabled.Checked = PluginSettings.Instance.RareMetaEnabled;
                    if (chkWebSocketEnabled != null)
                        chkWebSocketEnabled.Checked = PluginSettings.Instance.WebSocketEnabled;
                    if (txtCharTag != null)
                        txtCharTag.Text = PluginSettings.Instance.CharTag ?? "default";
                    if (txtVTankPath != null)
                        txtVTankPath.Text = PluginSettings.Instance.VTankProfilesPath ?? "";
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error loading settings: {ex.Message}");
            }
        }
        #endregion

        #region Event Handlers - Statistics Tab
        private void OnResetStatsClick(object sender, EventArgs e)
        {
            try
            {
                PluginCore.RestartStats();
                sessionStartTime = DateTime.Now;
                bestHourlyKills = 0;
                UpdateStatistics();
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error resetting stats: {ex.Message}");
            }
        }
        #endregion

        #region Event Handlers - Flag Tracker Tab
        private void OnOpenFlagTrackerClick(object sender, EventArgs e)
        {
            try
            {
                // Open the Flag Tracker window
                FlagTrackerView.OpenFlagTracker();
                PluginCore.WriteToChat("Flag Tracker window opened");
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error opening Flag Tracker: {ex.Message}");
            }
        }
        #endregion

        #region Event Handlers - Chest Looter Tab

        private void OnSetChestClick(object sender, EventArgs e)
        {
            try
            {
                var core = Decal.Adapter.CoreManager.Current;
                var chest = core.WorldFilter[core.Actions.CurrentSelection];
                if (chest == null)
                {
                    PluginCore.WriteToChat("[ChestLooter] No chest selected");
                    return;
                }

                if (chest.ObjectClass != Decal.Adapter.Wrappers.ObjectClass.Container)
                {
                    PluginCore.WriteToChat("[ChestLooter] Selected object is not a container");
                    return;
                }

                txtChestName.Text = chest.Name;
                if (PluginCore.chestLooter != null)
                {
                    PluginCore.chestLooter.SetChestName(chest.Name);
                }
                PluginCore.WriteToChat($"[ChestLooter] Chest set to: {chest.Name}");
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"[ChestLooter] Error setting chest: {ex.Message}");
            }
        }

        private void OnSetKeyClick(object sender, EventArgs e)
        {
            try
            {
                var core = Decal.Adapter.CoreManager.Current;
                var key = core.WorldFilter[core.Actions.CurrentSelection];
                if (key == null)
                {
                    PluginCore.WriteToChat("[ChestLooter] No key selected");
                    return;
                }

                if (key.ObjectClass != Decal.Adapter.Wrappers.ObjectClass.Key)
                {
                    PluginCore.WriteToChat("[ChestLooter] Selected object is not a key");
                    return;
                }

                txtKeyName.Text = key.Name;
                if (PluginCore.chestLooter != null)
                {
                    PluginCore.chestLooter.SetKeyName(key.Name);
                }
                PluginCore.WriteToChat($"[ChestLooter] Key set to: {key.Name}");
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"[ChestLooter] Error setting key: {ex.Message}");
            }
        }

        private void OnStartLooterClick(object sender, EventArgs e)
        {
            try
            {
                if (PluginCore.chestLooter == null)
                {
                    PluginCore.WriteToChat("[ChestLooter] Chest looter not initialized");
                    return;
                }

                string chestName = txtChestName?.Text ?? "";
                string keyName = txtKeyName?.Text ?? "";

                if (string.IsNullOrEmpty(chestName))
                {
                    PluginCore.WriteToChat("[ChestLooter] Please set chest name first");
                    return;
                }

                if (string.IsNullOrEmpty(keyName))
                {
                    PluginCore.WriteToChat("[ChestLooter] Please set key name first");
                    return;
                }

                bool started = PluginCore.chestLooter.StartByName(chestName, keyName);
                if (started)
                {
                    UpdateLooterStatus("Starting...");
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"[ChestLooter] Error starting looter: {ex.Message}");
            }
        }

        private void OnStopLooterClick(object sender, EventArgs e)
        {
            try
            {
                if (PluginCore.chestLooter != null)
                {
                    PluginCore.chestLooter.Stop();
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"[ChestLooter] Error stopping looter: {ex.Message}");
            }
        }

        private void OnEnableChestsChanged(object sender, EventArgs e)
        {
            try
            {
                var settings = PluginSettings.Instance;
                if (settings?.ChestLooterSettings != null && chkEnableChests != null)
                {
                    settings.ChestLooterSettings.EnableChests = chkEnableChests.Checked;
                    PluginSettings.Save();
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"[ChestLooter] Error changing enable chests: {ex.Message}");
            }
        }

        private void LoadChestLooterSettings()
        {
            try
            {
                var settings = PluginSettings.Instance?.ChestLooterSettings;
                if (settings != null)
                {
                    if (txtChestName != null)
                        txtChestName.Text = settings.ChestName ?? "";
                    if (txtKeyName != null)
                        txtKeyName.Text = settings.KeyName ?? "";
                    if (chkEnableChests != null)
                        chkEnableChests.Checked = settings.EnableChests;
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"[ChestLooter] Error loading settings: {ex.Message}");
            }
        }

        private void UpdateLooterStatus(string status)
        {
            try
            {
                if (lblLooterStatus != null)
                {
                    lblLooterStatus.Text = $"Status: {status}";
                }
            }
            catch { }
        }

        public static void UpdateChestLooterStatus(string status)
        {
            try
            {
                if (instance != null)
                {
                    instance.UpdateLooterStatus(status);
                }
            }
            catch { }
        }

        /// <summary>
        /// Update chest/key text boxes from settings (called when set via command line)
        /// </summary>
        public static void RefreshChestLooterUI()
        {
            try
            {
                if (instance == null) return;

                var settings = PluginSettings.Instance?.ChestLooterSettings;
                if (settings != null)
                {
                    if (instance.txtChestName != null)
                        instance.txtChestName.Text = settings.ChestName ?? "";
                    if (instance.txtKeyName != null)
                        instance.txtKeyName.Text = settings.KeyName ?? "";
                }
            }
            catch { }
        }

        #endregion

        #region Event Handlers - Navigation Tab
        private void OnNavVisualizationEnabledChanged(object sender, EventArgs e)
        {
            try
            {
                if (PluginCore.navVisualization != null)
                {
                    PluginCore.navVisualization.SetEnabled(chkNavVisualizationEnabled.Checked);
                    UpdateNavigationStatus();
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error in nav visualization change: {ex.Message}");
            }
        }

        private void OnRefreshNavFilesClick(object sender, EventArgs e)
        {
            try
            {
                RefreshNavFileList();
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error refreshing nav files: {ex.Message}");
            }
        }

        private void OnLoadRouteClick(object sender, EventArgs e)
        {
            try
            {
                // Load from dropdown selection
                string routeName = null;
                
                if (cmbNavFiles != null && cmbNavFiles.Current >= 0 && cmbNavFiles.Count > 0 && 
                    cmbNavFiles.Current < cmbNavFiles.Count)
                {
                    routeName = ((HudStaticText)cmbNavFiles[cmbNavFiles.Current]).Text;
                }
                
                if (!string.IsNullOrEmpty(routeName) && routeName != "None")
                {
                    PluginCore.navVisualization.LoadRoute(routeName);
                    UpdateNavigationStatus();
                }
                else
                {
                    PluginCore.WriteToChat("[NavViz] Please select a route from the dropdown");
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"[NavViz] Error loading route: {ex.Message}");
            }
        }

        private void OnClearRouteClick(object sender, EventArgs e)
        {
            try
            {
                if (PluginCore.navVisualization != null)
                {
                    PluginCore.navVisualization.LoadRoute("None");
                    // Clear dropdown selection
                    if (cmbNavFiles != null && cmbNavFiles.Count > 0)
                    {
                        cmbNavFiles.Current = 0; // Select "None" which should be at index 0
                    }
                    UpdateNavigationStatus();
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error clearing route: {ex.Message}");
            }
        }

        private void InitializeNavigationSystem()
        {
            try
            {
                RefreshNavFileList();
                UpdateNavigationStatus();
                
                // Subscribe to route changes
                if (PluginCore.navVisualization != null)
                {
                    PluginCore.navVisualization.RouteChanged += (s, e) => UpdateNavigationStatus();
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error initializing navigation system: {ex.Message}");
            }
        }

        private void RefreshNavFileList()
        {
            try
            {
                if (PluginCore.navVisualization != null)
                {
                    PluginCore.navVisualization.RefreshNavFileList();
                    
                    // Update the basic dropdown
                    PopulateNavFileDropdown();
                        
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error refreshing nav file list: {ex.Message}");
            }
        }

        private void UpdateNavigationStatus()
        {
            try
            {
                if (lblNavStatus != null && PluginCore.navVisualization != null)
                {
                    lblNavStatus.Text = $"Status: {PluginCore.navVisualization.GetStatus()}";
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error updating navigation status: {ex.Message}");
            }
        }
        #endregion

        #region Public Update Methods (maintain compatibility with existing code)
        public static void UpdateKillStats(int totalKills, double killsPer5Min, double killsPerHour)
        {
            try
            {
                if (instance == null) return;

                if (instance.lblTotalKills != null)
                    instance.lblTotalKills.Text = $"Total Kills: {totalKills}";
                if (instance.lblKillsPer5Min != null)
                    instance.lblKillsPer5Min.Text = $"Kills per 5 Min: {killsPer5Min:F2}";
                if (instance.lblKillsPerHour != null)
                    instance.lblKillsPerHour.Text = $"Kills per Hour: {killsPerHour:F2}";
                
                // Update detailed statistics
                if (instance.lblDetailedKillsValue != null)
                    instance.lblDetailedKillsValue.Text = totalKills.ToString();
                if (instance.lblAverageKillsValue != null)
                    instance.lblAverageKillsValue.Text = $"{killsPerHour:F2} kills/hr";
                
                // Track best hourly performance
                if (killsPerHour > instance.bestHourlyKills)
                {
                    instance.bestHourlyKills = killsPerHour;
                    if (instance.lblBestHourValue != null)
                        instance.lblBestHourValue.Text = $"{instance.bestHourlyKills:F2} kills/hr";
                }
                
                // Update status displays
                instance.UpdateStatus();
                instance.UpdateAutoLootRareIndicator();
                instance.UpdateWebSocketStatus();
                instance.UpdateStatistics();
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error in UpdateKillStats: {ex.Message}");
            }
        }

        public static void UpdateElapsedTime(TimeSpan elapsed)
        {
            try
            {
                if (instance?.lblElapsedTime != null)
                {
                    int days = elapsed.Days;
                    int hours = elapsed.Hours;
                    int minutes = elapsed.Minutes;
                    int seconds = elapsed.Seconds;

                    if (days > 0)
                        instance.lblElapsedTime.Text = $"Time: {days}d {hours:D2}:{minutes:D2}:{seconds:D2}";
                    else
                        instance.lblElapsedTime.Text = $"Time: {hours:D2}:{minutes:D2}:{seconds:D2}";
                }
                        
                if (instance?.lblSessionTimeValue != null && instance?.lblElapsedTime != null)
                {
                    instance.lblSessionTimeValue.Text = instance.lblElapsedTime.Text.Replace("Time: ", "");
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error in UpdateElapsedTime: {ex.Message}");
            }
        }

        public static void UpdateRareCount(int rareCount)
        {
            try
            {
                if (instance?.lblRareCount != null)
                    instance.lblRareCount.Text = $"Rare Count: {rareCount}";
                if (instance?.lblRareStatsValue != null)
                    instance.lblRareStatsValue.Text = rareCount.ToString();
                
                // Calculate rare drop rate
                int totalKills = PluginCore.totalKills;
                if (totalKills > 0 && instance?.lblRareRateValue != null)
                {
                    double rareRate = (double)rareCount / totalKills * 100;
                    instance.lblRareRateValue.Text = $"{rareRate:F2}%";
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error in UpdateRareCount: {ex.Message}");
            }
        }

        public static void SetRareMetaToggleState(bool enabled)
        {
            try
            {
                if (instance?.chkRareMetaEnabled != null)
                    instance.chkRareMetaEnabled.Checked = enabled;
                instance?.UpdateAutoLootRareIndicator();
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error setting rare meta toggle state: {ex.Message}");
            }
        }

        public static void RefreshSettingsFromConfig()
        {
            try
            {
                instance?.LoadCurrentSettings();
                instance?.UpdateAutoLootRareIndicator();
                instance?.UpdateWebSocketStatus();
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error refreshing settings from config: {ex.Message}");
            }
        }

        public static void RestorePosition()
        {
            instance?.RestoreWindowPosition();
            instance?.KeepWindowInBounds();
        }
        #endregion

        #region Status Update Methods
        private void UpdateStatus()
        {
            try
            {
                if (lblStatus != null)
                {
                    string metaState = VtankControl.VtGetMetaState();
                    lblStatus.Text = $"Meta State: {metaState}";
                }
            }
            catch
            {
                if (lblStatus != null)
                    lblStatus.Text = "Meta State: Unknown";
            }
        }

        private void UpdateAutoLootRareIndicator()
        {
            try
            {
                if (lblAutoLootRare != null)
                {
                    bool isEnabled = PluginSettings.Instance?.RareMetaEnabled == true;
                    if (isEnabled)
                    {
                        lblAutoLootRare.Text = "Auto Loot Rare: [ON]";
                        // Try to set green color for ON status
                        try { lblAutoLootRare.TextColor = System.Drawing.Color.FromArgb(0, 128, 0); } catch { }
                    }
                    else
                    {
                        lblAutoLootRare.Text = "Auto Loot Rare: [OFF]";
                        // Try to set red color for OFF status
                        try { lblAutoLootRare.TextColor = System.Drawing.Color.FromArgb(255, 0, 0); } catch { }
                    }
                }
            }
            catch
            {
                if (lblAutoLootRare != null)
                    lblAutoLootRare.Text = "Auto Loot Rare: Unknown";
            }
        }
        
        private void UpdateWebSocketStatus()
        {
            try
            {
                if (lblWebSocketStatus != null)
                {
                    bool isConnected = PluginSettings.Instance?.WebSocketEnabled == true;
                    if (isConnected)
                    {
                        lblWebSocketStatus.Text = "WebSocket: [CONNECTED]";
                        // Try to set green color for CONNECTED status
                        try { lblWebSocketStatus.TextColor = System.Drawing.Color.FromArgb(0, 128, 0); } catch { }
                    }
                    else
                    {
                        lblWebSocketStatus.Text = "WebSocket: [DISCONNECTED]";
                        // Try to set red color for DISCONNECTED status
                        try { lblWebSocketStatus.TextColor = System.Drawing.Color.FromArgb(255, 0, 0); } catch { }
                    }
                }
            }
            catch
            {
                if (lblWebSocketStatus != null)
                    lblWebSocketStatus.Text = "WebSocket: Unknown";
            }
        }

        private void UpdateStatistics()
        {
            try
            {
                // Update last kill time
                if (lblLastKillValue != null)
                {
                    if (PluginCore.lastKillTime != DateTime.MinValue)
                    {
                        TimeSpan timeSinceLastKill = DateTime.Now - PluginCore.lastKillTime;
                        if (timeSinceLastKill.TotalMinutes < 60)
                            lblLastKillValue.Text = $"{timeSinceLastKill.TotalMinutes:F0} min ago";
                        else
                            lblLastKillValue.Text = $"{timeSinceLastKill.TotalHours:F1} hr ago";
                    }
                    else
                    {
                        lblLastKillValue.Text = "Never";
                    }
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error in UpdateStatistics: {ex.Message}");
            }
        }
        #endregion

        #region Helper Methods
        private void PopulateNavFileDropdown()
        {
            try
            {
                if (cmbNavFiles != null && PluginCore.navVisualization != null)
                {
                    cmbNavFiles.Clear();
                    cmbNavFiles.AddItem("None", "None");
                    
                    foreach (string navFile in PluginCore.navVisualization.AvailableNavFiles)
                    {
                        cmbNavFiles.AddItem(navFile, navFile);
                    }
                    
                    if (cmbNavFiles.Count > 0)
                        cmbNavFiles.Current = 0; // Select "None" which should be at index 0
                        
                }
            }
            catch (Exception ex)
            {
                PluginCore.WriteToChat($"Error populating nav file dropdown: {ex.Message}");
            }
        }
        #endregion

        #region IDisposable Override
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                try
                {
                    // Unsubscribe from events to prevent memory leaks
                    
                    // Settings tab event cleanup
                    if (chkRareMetaEnabled != null)
                        chkRareMetaEnabled.Change -= OnRareMetaSettingChanged;
                    if (chkWebSocketEnabled != null)
                        chkWebSocketEnabled.Change -= OnWebSocketSettingChanged;
                    if (txtCharTag != null)
                        txtCharTag.Change -= OnCharTagChanged;
                    if (txtVTankPath != null)
                        txtVTankPath.Change -= OnVTankPathChanged;
                    if (btnCheckUpdate != null)
                        btnCheckUpdate.Hit -= OnCheckUpdateClicked;
                    if (btnInstallUpdate != null)
                        btnInstallUpdate.Hit -= OnInstallUpdateClicked;
                    
                    // Statistics tab event cleanup
                    if (btnResetStats != null)
                        btnResetStats.Hit -= OnResetStatsClick;
                    
                    // Navigation tab event cleanup
                    if (chkNavVisualizationEnabled != null)
                        chkNavVisualizationEnabled.Change -= OnNavVisualizationEnabledChanged;
                    if (btnRefreshNavFiles != null)
                        btnRefreshNavFiles.Hit -= OnRefreshNavFilesClick;
                    if (btnLoadRoute != null)
                        btnLoadRoute.Hit -= OnLoadRouteClick;
                    if (btnClearRoute != null)
                        btnClearRoute.Hit -= OnClearRouteClick;
                    
                    // No enhanced events to clean up
                }
                catch (Exception ex)
                {
                    PluginCore.WriteToChat($"Error cleaning up VVS view events: {ex.Message}");
                }
            }

            // Call base dispose
            base.Dispose(disposing);
        }
        #endregion
    }
}