// DtkSDK.ApiParameters
using System.Collections.Generic;

namespace Dataoke
{
    public class ApiParameters
    {
        private List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>();

        public List<KeyValuePair<string, string>> Value => parameters;

        public void Add(string key, string value)
        {
            parameters.Add(new KeyValuePair<string, string>(key, value));
        }

        public bool Delete(string key)
        {
            try
            {
                int find = parameters.FindIndex((KeyValuePair<string, string> a) => a.Key == key);
                if (find >= 0)
                {
                    parameters.RemoveAt(find);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool EditOrAdd(string key, string value)
        {
            try
            {
                int find = parameters.FindIndex((KeyValuePair<string, string> a) => a.Key == key);
                if (find >= 0)
                {
                    parameters.RemoveAt(find);
                }
                Add(key, value);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Edit(string key, string value)
        {
            try
            {
                int find = parameters.FindIndex((KeyValuePair<string, string> a) => a.Key == key);
                if (find >= 0)
                {
                    parameters.RemoveAt(find);
                    Add(key, value);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void Clear()
        {
            parameters.Clear();
        }
    }
}