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

#pragma warning disable CS8604 // Possible null reference argument.
namespace EasyDevCore.Common
{
    /// <summary>
    /// 
    /// </summary>
    public static class ArgumentHelper
    {
        /// <summary>
        /// Outs the specified alias.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">The object.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static T Out<T>(this T obj, out T @name)
        {
            @name = obj;
            return obj;
        }

        /// <summary>
        /// Outs the specified selector.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="obj">The object.</param>
        /// <param name="selector">The selector.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static T Out<T, TResult>(this T obj, Func<T, TResult> selector, out TResult @name)
        {
            @name = selector(obj);
            return obj;
        }

        /// <summary>
        /// Gets the arguments has value pair format
        /// Ex1: new { Name = ... }
        /// Ex2: "Name1,Name2", "a", "b"
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <returns></returns>
        public static IDictionary<string, object> GetArgValues(params object[] args)
        {
            IDictionary<string, object> values = new Dictionary<string, object>();
            if (args.Length == 1)
            {
                return args[0].GetPropertyValues();
            }
            else
            {
                if (args[0] is not string)
                {
                    throw new ArgumentException(args[0].GetTypeName() + " is not supported !");
                }
                var names = (args[0] as string).SplitByCommaOrSemiColon();
                if (names.Length != args.Length - 1)
                {
                    throw new ArgumentException("Length of paramNames and values are not the same !");
                }
                for (int i = 0; i < names.Length; i++)
                {
                    values.Add(names[i], args[i + 1]);
                }
                return values;
            }
        }

    }
}
