namespace FireflyClient
{
    public partial class FireflyClient
    {
        #region List Operations
        
        /// <summary>
        /// Adds values to the beginning of a list
        /// </summary>
        public int ListLeftPush(string key, params string[] values)
        {
            var args = new List<string> { key };
            args.AddRange(values);
            
            string response = ExecuteCommand("LPUSH", [.. args]);
            if (response.StartsWith(':'))
            {
                if (int.TryParse(response[1..].TrimEnd('\r', '\n'), out int result))
                {
                    return result;
                }
            }
            return 0;
        }
        
        /// <summary>
        /// Adds values to the end of a list
        /// </summary>
        public int ListRightPush(string key, params string[] values)
        {
            var args = new List<string> { key };
            args.AddRange(values);
            
            string response = ExecuteCommand("RPUSH", [.. args]);
            if (response.StartsWith(':'))
            {
                if (int.TryParse(response[1..].TrimEnd('\r', '\n'), out int result))
                {
                    return result;
                }
            }
            return 0;
        }
        
        /// <summary>
        /// Removes and returns the first element of a list
        /// </summary>
        public string ListLeftPop(string key)
        {
            string response = ExecuteCommand("LPOP", key);
            if (response.StartsWith('+'))
            {
                return response[1..].TrimEnd('\r', '\n');
            }
            return string.Empty;
        }
        
        /// <summary>
        /// Removes and returns the last element of a list
        /// </summary>
        public string ListRightPop(string key)
        {
            string response = ExecuteCommand("RPOP", key);
            if (response.StartsWith('+'))
            {
                return response[1..].TrimEnd('\r', '\n');
            }
            return string.Empty;
        }
        
        /// <summary>
        /// Gets a range of elements from a list
        /// </summary>
        public List<string> ListRange(string key, int start, int stop)
        {
            string response = ExecuteCommand("LRANGE", key, start.ToString(), stop.ToString());
            return ParseArrayResponse(response);
        }
        
        /// <summary>
        /// Gets the element at the specified index in a list
        /// </summary>
        public string ListIndex(string key, int index)
        {
            string response = ExecuteCommand("LINDEX", key, index.ToString());
            if (response.StartsWith('+'))
            {
                return response[1..].TrimEnd('\r', '\n');
            }
            return string.Empty;
        }
        
        /// <summary>
        /// Sets the element at the specified index in a list
        /// </summary>
        public bool ListSet(string key, int index, string value)
        {
            string 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>
        public 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());
            }
            
            string response = ExecuteCommand("LPOS", [.. args]);
            if (response.StartsWith(':'))
            {
                if (int.TryParse(response[1..].TrimEnd('\r', '\n'), out int result))
                {
                    return result;
                }
            }
            return -1;
        }
        
        /// <summary>
        /// Trims a list to the specified range
        /// </summary>
        public bool ListTrim(string key, int start, int stop)
        {
            string response = ExecuteCommand("LTRIM", key, start.ToString(), stop.ToString());
            return response.StartsWith("+OK");
        }
        
        /// <summary>
        /// Removes elements equal to the given value from a list
        /// </summary>
        public int ListRemove(string key, int count, string element)
        {
            string response = ExecuteCommand("LREM", key, count.ToString(), element);
            if (response.StartsWith(':'))
            {
                if (int.TryParse(response[1..].TrimEnd('\r', '\n'), out int result))
                {
                    return result;
                }
            }
            return 0;
        }
        
        #endregion
    }
}