using Rabbit.WeiXin.Utility;
using System;
using System.Collections;

namespace Rabbit.WeiXin.MP.Api.Material
{
    /// <summary>
    /// ËØ²Ä·þÎñ»ùÀà¡£
    /// </summary>
    public abstract class MaterialServiceBase
    {
        #region Protected Method

        /// <summary>
        /// ÉÏ´«¡£
        /// </summary>
        /// <typeparam name="TResult">½á¹ûÀàÐÍ¡£</typeparam>
        /// <param name="url">ÇëÇóµØÖ·¡£</param>
        /// <param name="bytes">ÄÚÈÝ¡£</param>
        /// <param name="fieldName">ÎÄ¼þÃû³Æ¡£</param>
        /// <param name="func">ÆäËû×Ö¶ÎÊý¾Ý¡£</param>
        /// <returns>½á¹û¡£</returns>
        protected static TResult Upload<TResult>(string url, byte[] bytes, string fieldName, Func<CreateBytes, byte[][]> func = null) where TResult : class
        {
            var createBytes = new CreateBytes();
            var list = new ArrayList
            {
                createBytes.CreateFieldData(fieldName, FileHelper.GetRandomFileName(bytes), FileHelper.GetContentType(bytes), bytes),
            };
            if (func != null)
                foreach (var b in func(createBytes))
                {
                    list.Add(b);
                }
            var data = createBytes.JoinBytes(list);

            return WeiXinHttpHelper.PostResultByJson<TResult>(url, data, createBytes.ContentType);
        }

        #endregion Protected Method
    }
}