//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.IO;
using NopSolutions.NopCommerce.Common.Orders;
using NopSolutions.NopCommerce.Common.Payment;
using NopSolutions.NopCommerce.Common.Products;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.DataAccess.Media;

namespace NopSolutions.NopCommerce.Common.Media
{
    /// <summary>
    /// Download manager
    /// </summary>
    public class DownloadManager
    {
        #region Utilities
        private static DownloadCollection DBMapping(DBDownloadCollection dbCollection)
        {
            if (dbCollection == null)
                return null;

            DownloadCollection collection = new DownloadCollection();
            foreach (DBDownload dbItem in dbCollection)
            {
                Download item = DBMapping(dbItem);
                collection.Add(item);
            }

            return collection;
        }

        private static Download DBMapping(DBDownload dbItem)
        {
            if (dbItem == null)
                return null;

            Download item = new Download();
            item.DownloadID = dbItem.DownloadID;
            item.DownloadBinary = dbItem.DownloadBinary;
            item.ContentType = dbItem.ContentType;
            item.Extension = dbItem.Extension;
            item.IsNew = dbItem.IsNew;

            return item;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Gets a download url for an admin area
        /// </summary>
        /// <param name="download">Download instance</param>
        /// <returns>Download url</returns>
        public static string GetAdminDownloadUrl(Download download)
        {
            if (download == null)
                throw new ArgumentNullException("download");
            string url = CommonHelper.GetStoreAdminLocation(false) + "GetDownloadAdmin.ashx?DownloadID=" + download.DownloadID;
            return url;
        }

        /// <summary>
        /// Gets a download url for a product variant
        /// </summary>
        /// <param name="orderProductVariant">Order product variant instance</param>
        /// <returns>Download url</returns>
        public static string GetDownloadUrl(OrderProductVariant orderProductVariant)
        {
            if (orderProductVariant == null)
                throw new ArgumentNullException("orderProductVariant");

            string url = string.Empty;
            ProductVariant productVariant = orderProductVariant.ProductVariant;
            if (productVariant != null && productVariant.IsDownload)
            {
                Download download = productVariant.Download;
                url = CommonHelper.GetStoreLocation(false) + "GetDownload.ashx?OrderProductVariantID=" + orderProductVariant.OrderProductVariantID;
            }
            return url;
        }

        /// <summary>
        /// Gets a download
        /// </summary>
        /// <param name="DownloadID">Download identifier</param>
        /// <returns>Download</returns>
        public static Download GetDownloadByID(int DownloadID)
        {
            DBDownload dbItem = DBDownloadProvider.Provider.GetDownloadByID(DownloadID);
            Download download = DBMapping(dbItem);
            return download;
        }

        /// <summary>
        /// Deletes a download
        /// </summary>
        /// <param name="DownloadID">Download identifier</param>
        public static void DeleteDownload(int DownloadID)
        {
            DBDownloadProvider.Provider.DeleteDownload(DownloadID);
        }

        /// <summary>
        /// Inserts a download
        /// </summary>
        /// <param name="DownloadBinary">The download binary</param>
        /// <param name="ContentType">The mime-type of the download</param>
        /// <param name="Extension">The extension</param>
        /// <param name="IsNew">A value indicating whether the download is new</param>
        /// <returns>Download</returns>
        public static Download InsertDownload(byte[] DownloadBinary, string ContentType, string Extension, bool IsNew)
        {
            if (ContentType == null)
                ContentType = string.Empty;
            if (Extension == null)
                Extension = string.Empty;

            DBDownload dbItem = DBDownloadProvider.Provider.InsertDownload(DownloadBinary, ContentType, Extension, IsNew);
            Download download = DBMapping(dbItem);
            return download;
        }

        /// <summary>
        /// Updates the download
        /// </summary>
        /// <param name="DownloadID">The download identifier</param>
        /// <param name="DownloadBinary">The download binary</param>
        /// <param name="ContentType">The mime-type of the download</param>
        /// <param name="Extension">The extension</param>
        /// <param name="IsNew">A value indicating whether the download is new</param>
        /// <returns>Download</returns>
        public static Download UpdateDownload(int DownloadID, byte[] DownloadBinary, string ContentType, string Extension, bool IsNew)
        {
            if (ContentType == null)
                ContentType = string.Empty;
            if (Extension == null)
                Extension = string.Empty;

            DBDownload dbItem = DBDownloadProvider.Provider.UpdateDownload(DownloadID, DownloadBinary, ContentType, Extension, IsNew);
            Download download = DBMapping(dbItem);
            return download;
        }

        /// <summary>
        /// Gets the download binary array
        /// </summary>
        /// <param name="fs">File stream</param>
        /// <param name="size">Download size</param>
        /// <returns>Download binary array</returns>
        public static byte[] GetDownloadBits(Stream fs, int size)
        {
            byte[] binary = new byte[size];
            fs.Read(binary, 0, size);
            return binary;
        }
        #endregion
    }
}
