using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebAPIBase.NetCore.BusinessCore
{
    public abstract class AbstractQueryStringBuilder : IQueryStringBuilder
    {
        public string QueryMainString;

        public string QueryKeyString;

        public string QueryOtherString;

        public bool IsNeedWhere = true;

        private ArrayList strategys = new ArrayList();

        private ArrayList orders = new ArrayList();

        public AbstractQueryStringBuilder()
        {
        }

        public virtual ArrayList GetStategys()
        {
            return strategys;
        }

        public virtual void AddStrategy(Strategy strategy, bool isUnique)
        {
            if (isUnique)
            {
                ReomveStrategy(strategy.Name);
            }
            AddStrategy(strategy);
        }

        public virtual void AddStrategy(Strategy strategy)
        {
            int parameterCount = strategy.GetParameterCount();
            for (int i = 0; i < parameterCount; i++)
            {
                string text = strategy.GetParameter(i);
                if (strategy.Type != StrategyType.StringIn && strategy.Type != StrategyType.Other && strategy.Type != StrategyType.StringRange && strategy.Type != StrategyType.NumberIn)
                {
                    text = text.Replace("'", "''");
                }
                strategy.SetParameter(i, text);
            }
            strategys.Add(strategy);
        }

        public virtual void ReomveStrategy(object strategyName)
        {
            int count = strategys.Count;
            if (count == 0)
            {
                return;
            }
            for (int num = count - 1; num >= 0; num--)
            {
                Strategy strategy = (Strategy)strategys[num];
                if (strategyName.ToString() == strategy.Name.ToString())
                {
                    strategys.Remove(strategy);
                }
            }
        }

        public virtual void ClearStrategy()
        {
            strategys.Clear();
        }

        public virtual void AddOrder(string Name, bool IsASC)
        {
            RemoveOrder(Name);
            orders.Add(new QueryOrder(Name, IsASC));
        }

        public virtual void RemoveOrder(string name)
        {
            int count = orders.Count;
            if (count == 0)
            {
                return;
            }
            for (int num = count - 1; num >= 0; num--)
            {
                QueryOrder queryOrder = (QueryOrder)orders[num];
                if (queryOrder.Name == name)
                {
                    orders.Remove(queryOrder);
                }
            }
        }

        public virtual string BuildStrategysString()
        {
            if (strategys.Count == 0)
            {
                return "";
            }
            int num = 0;
            StringBuilder stringBuilder = new StringBuilder();
            if (IsNeedWhere)
            {
                stringBuilder.Append(" where ");
            }
            else
            {
                stringBuilder.Append(" and ");
            }
            IEnumerator enumerator = strategys.GetEnumerator();
            while (enumerator.MoveNext())
            {
                if (num != 0)
                {
                    stringBuilder.Append(" and ");
                }
                Strategy strategy = (Strategy)enumerator.Current;
                stringBuilder.Append(BuildSingleStrategyString(strategy));
                num++;
            }
            return stringBuilder.ToString();
        }

        public virtual string BuildOrderString()
        {
            if (orders.Count == 0)
            {
                return "";
            }
            StringBuilder stringBuilder = new StringBuilder();
            int count = orders.Count;
            for (int i = 0; i < count; i++)
            {
                if (i != 0)
                {
                    stringBuilder.Append(" , ");
                }
                else
                {
                    stringBuilder.Append(" Order By ");
                }
                QueryOrder queryOrder = (QueryOrder)orders[i];
                string name = queryOrder.Name;
                bool sort = queryOrder.Sort;
                stringBuilder.Append(name);
                if (sort)
                {
                    stringBuilder.Append(" ASC ");
                }
                else
                {
                    stringBuilder.Append(" DESC ");
                }
            }
            return stringBuilder.ToString();
        }

        public virtual string BuildSingleStrategyString(Strategy strategy)
        {
            return StandardStrategyStringBuilder.BuildStrategyString(strategy);
        }

        public virtual string BuildMainQueryString()
        {
            return QueryMainString + BuildStrategysString() + BuildOrderString();
        }

        public virtual string BuildKeyQueryString()
        {
            return QueryKeyString + BuildStrategysString() + BuildOrderString();
        }

        public virtual string BuildOtherQueryString()
        {
            return QueryOtherString + BuildStrategysString() + BuildOrderString();
        }
    }
}
