namespace FireflyClient
{
    public partial class FireflyClient
    {
        #region List Operations
        
        /// <summary>
        /// Adds values to the beginning of a list
        /// </summary>
        private int ListLeftPush(string key, params string[] values)
        {
            var args = new List<string> { key };
            args.AddRange(values);
            
            var response = ExecuteCommand("LPUSH", [.. args]);
            if (!response.StartsWith(':')) return 0;
            return int.TryParse(response[1..].TrimEnd('\r', '\n'), out var result) ? result : 0;
        }
        
        /// <summary>
        /// Adds values to the end of a list
        /// </summary>
        private int ListRightPush(string key, params string[] values)
        {
            var args = new List<string> { key };
            args.AddRange(values);
            
            var response = ExecuteCommand("RPUSH", [.. args]);
            if (!response.StartsWith(':')) return 0;
            return int.TryParse(response[1..].TrimEnd('\r', '\n'), out var result) ? result : 0;
        }
        
        /// <summary>
        /// Removes and returns the first element of a list
        /// </summary>
        private string ListLeftPop(string key)
        {
            var response = ExecuteCommand("LPOP", key);
            return response.StartsWith('+') ? response[1..].TrimEnd('\r', '\n') : string.Empty;
        }
        
        /// <summary>
        /// Removes and returns the last element of a list
        /// </summary>
        private string ListRightPop(string key)
        {
            var response = ExecuteCommand("RPOP", key);
            return response.StartsWith('+') ? response[1..].TrimEnd('\r', '\n') : string.Empty;
        }

        /// <summary>
        /// Gets the length of a list
        /// </summary>
        private int ListLength(string key)
        {
            var response = ExecuteCommand("LLEN", key);
            if (!response.StartsWith(':')) return 0;
            return int.TryParse(response[1..].TrimEnd('\r', '\n'), out var result) ? result : 0;
        }
        
        /// <summary>
        /// Gets a range of elements from a list
        /// </summary>
        private List<string> ListRange(string key, int start, int stop)
        {
            var response = ExecuteCommand("LRANGE", key, start.ToString(), stop.ToString());
            return ParseArrayResponse(response);
        }
        
        /// <summary>
        /// Gets the element at the specified index in a list
        /// </summary>
        private string ListIndex(string key, int index)
        {
            var response = ExecuteCommand("LINDEX", key, index.ToString());
            return response.StartsWith('+') ? response[1..].TrimEnd('\r', '\n') : string.Empty;
        }
        
        /// <summary>
        /// Sets the element at the specified index in a list
        /// </summary>
        private bool ListSet(string key, int index, string value)
        {
            var response = ExecuteCommand("LSET", key, index.ToString(), value);
            return response.StartsWith("+OK");
        }
        
        /// <summary>
        /// Returns the index of the first occurrence of an element in a list
        /// </summary>
        private int ListPosition(string key, string element, int rank = 1, int maxlen = 0)
        {
            var args = new List<string> { key, element };
            
            if (rank != 1)
            {
                args.Add("RANK");
                args.Add(rank.ToString());
            }
            
            if (maxlen > 0)
            {
                args.Add("MAXLEN");
                args.Add(maxlen.ToString());
            }
            
            var response = ExecuteCommand("LPOS", [.. args]);
            if (!response.StartsWith(':')) return -1;
            return int.TryParse(response[1..].TrimEnd('\r', '\n'), out var result) ? result : -1;
        }
        
        /// <summary>
        /// Trims a list to the specified range
        /// </summary>
        private bool ListTrim(string key, int start, int stop)
        {
            var response = ExecuteCommand("LTRIM", key, start.ToString(), stop.ToString());
            return response.StartsWith("+OK");
        }
        
        /// <summary>
        /// Removes elements equal to the given value from a list
        /// </summary>
        private int ListRemove(string key, int count, string element)
        {
            var response = ExecuteCommand("LREM", key, count.ToString(), element);
            if (!response.StartsWith(':')) return 0;
            return int.TryParse(response[1..].TrimEnd('\r', '\n'), out var result) ? result : 0;
        }
        
        #endregion
    }
}