using System;

namespace MosswartMassacre
{
    /// <summary>
    /// Settings for the Chest Looter feature
    /// These settings are persisted per-character via PluginSettings
    /// </summary>
    public class ChestLooterSettings
    {
        // Target configuration
        public string ChestName { get; set; } = "";
        public string KeyName { get; set; } = "";

        // Feature toggles
        public bool Enabled { get; set; } = false;
        public bool EnableChests { get; set; } = true;
        public bool AutoSalvageAfterLooting { get; set; } = false;
        public bool JumpWhenLooting { get; set; } = false;
        public bool BlockVtankMelee { get; set; } = false;
        public bool TestMode { get; set; } = false;
        public bool VerboseLogging { get; set; } = false;

        // Timing and retry settings
        public int DelaySpeed { get; set; } = 1000;           // Delay for unlock/open/close in ms
        public int OverallSpeed { get; set; } = 100;          // Overall looter tick rate in ms
        public int MaxUnlockAttempts { get; set; } = 10;      // Max attempts to unlock chest
        public int MaxOpenAttempts { get; set; } = 10;        // Max attempts to open chest
        public int AttemptsBeforeBlacklisting { get; set; } = 500;  // Item loot attempts before giving up

        // Jump looting settings
        public int JumpHeight { get; set; } = 100;            // Jump height (full bar is 1000)

        // UI state
        public bool ShowChestLooterTab { get; set; } = true;

        /// <summary>
        /// Constructor with default values
        /// </summary>
        public ChestLooterSettings()
        {
            // All defaults set via property initializers above
        }

        /// <summary>
        /// Validate settings and apply constraints
        /// </summary>
        public void Validate()
        {
            // Ensure OverallSpeed isn't too fast (can cause issues)
            if (OverallSpeed < 100)
                OverallSpeed = 100;

            // Ensure delays are reasonable
            if (DelaySpeed < 500)
                DelaySpeed = 500;

            // Ensure attempt limits are positive
            if (MaxUnlockAttempts < 1)
                MaxUnlockAttempts = 1;
            if (MaxOpenAttempts < 1)
                MaxOpenAttempts = 1;
            if (AttemptsBeforeBlacklisting < 1)
                AttemptsBeforeBlacklisting = 1;

            // Clamp jump height to reasonable range
            if (JumpHeight < 0)
                JumpHeight = 0;
            if (JumpHeight > 1000)
                JumpHeight = 1000;
        }

        /// <summary>
        /// Reset all settings to default values
        /// </summary>
        public void Reset()
        {
            ChestName = "";
            KeyName = "";
            Enabled = false;
            EnableChests = true;
            AutoSalvageAfterLooting = false;
            JumpWhenLooting = false;
            BlockVtankMelee = false;
            TestMode = false;
            VerboseLogging = false;
            DelaySpeed = 1000;
            OverallSpeed = 100;
            MaxUnlockAttempts = 10;
            MaxOpenAttempts = 10;
            AttemptsBeforeBlacklisting = 500;
            JumpHeight = 100;
            ShowChestLooterTab = true;
        }

        /// <summary>
        /// Create a copy of these settings
        /// </summary>
        public ChestLooterSettings Clone()
        {
            return new ChestLooterSettings
            {
                ChestName = this.ChestName,
                KeyName = this.KeyName,
                Enabled = this.Enabled,
                EnableChests = this.EnableChests,
                AutoSalvageAfterLooting = this.AutoSalvageAfterLooting,
                JumpWhenLooting = this.JumpWhenLooting,
                BlockVtankMelee = this.BlockVtankMelee,
                TestMode = this.TestMode,
                VerboseLogging = this.VerboseLogging,
                DelaySpeed = this.DelaySpeed,
                OverallSpeed = this.OverallSpeed,
                MaxUnlockAttempts = this.MaxUnlockAttempts,
                MaxOpenAttempts = this.MaxOpenAttempts,
                AttemptsBeforeBlacklisting = this.AttemptsBeforeBlacklisting,
                JumpHeight = this.JumpHeight,
                ShowChestLooterTab = this.ShowChestLooterTab
            };
        }
    }
}
