using Aliyun.OSS;
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using Org.BouncyCastle.Utilities.Collections;
using Wicture.DbRESTFul.Configuration;
using JiaZhiQuan.Common.Config;

namespace JiaZhiQuan.Common.AliOSS
{
    public class FileOperator
    {
        public async static Task<string> GetFileContent(string key, string bucketName, string endPoint, string accessKeyId, string accessKeySecret)
        {
            OssClient ossClient = new OssClient(endPoint, accessKeyId, accessKeySecret);
            return await GetFileContent(key, bucketName, ossClient);
        }

        public async static Task<string> GetFileContent(string key, string bucketName, OssClient ossClient)
        {
            GetObjectRequest request = new GetObjectRequest(bucketName, key);
            var rst = await Task.Factory.FromAsync(ossClient.BeginGetObject, ossClient.EndGetObject, request, null);
            if (rst.HttpStatusCode != System.Net.HttpStatusCode.OK) return string.Empty;
            using StreamReader sr = new StreamReader(rst.ResponseStream);
            return await sr.ReadToEndAsync();
        }

        public async static Task<Stream> GetFileStreamAsync(string key, bool isDev, string accessKeyId, string accessKeySecret)
        {
            var ossConfig = BuildOssConfig(isDev, accessKeyId, accessKeySecret);
            var ossClient = BuildOssClient(ossConfig);
            GetObjectRequest request = new GetObjectRequest(ossConfig.OSSBucket, key);
            var rst = await Task.Factory.FromAsync(ossClient.BeginGetObject, ossClient.EndGetObject, request, null);
            if (rst.HttpStatusCode != System.Net.HttpStatusCode.OK) return null;
            return rst.ResponseStream;
        }

        public static void RemoveFile(List<string> keys, string bucketName, string endPoint, string accessKeyId, string accessKeySecret)
        {
            OssClient ossClient = new OssClient(endPoint, accessKeyId, accessKeySecret);
            RemoveFile(keys, bucketName, ossClient);
        }

        public static void RemoveFile(List<string> keys, string bucketName, OssClient ossClient)
        {
            ossClient.DeleteObjects(new DeleteObjectsRequest(bucketName, keys));
        }

        /// <summary>
        /// 构建OSS操作对象，返回OSSClient和OSSConfig
        /// </summary>
        /// <param name="isDev"></param>
        /// <param name="accessKeyId"></param>
        /// <param name="accessKeySecret"></param>
        /// <returns></returns>
        public static OssClient BuildOssClient(OSSConfig OSSConfig)
        {
            var OSSClient = new OssClient(OSSConfig.EndPoint, OSSConfig.AccessKeyId, OSSConfig.AccessKeySecret);
            return OSSClient;
        }

        /// <summary>
        /// 创建OSSConfig对象
        /// </summary>
        /// <param name="isDev"></param>
        /// <param name="accessKeyId"></param>
        /// <param name="accessKeySecret"></param>
        /// <param name="bucket"></param>
        /// <returns></returns>
        public static OSSConfig BuildOssConfig(bool isDev, string accessKeyId, string accessKeySecret, string bucket = null)
        {
            var ossInternal = bool.Parse(ConfigurationManager.Settings.Variables["OSS.Internal"]);
            var OSSConfig = new OSSConfig
            {
                DomainUrl = isDev ? "https://olscdndev.olssglobal.com/" : "https://olscdn.olssglobal.com/",
                InternalDomainUrl = !ossInternal ? "" : isDev ? "https://olss-ols-cdn-dev.oss-cn-shanghai-internal.aliyuncs.com/" : "http://olss-ols-cdn.oss-cn-shanghai-internal.aliyuncs.com/",
                AccessKeyId = accessKeyId,
                AccessKeySecret = accessKeySecret,
                EndPoint = !ossInternal ? "oss-cn-shanghai.aliyuncs.com" : "oss-cn-shanghai-internal.aliyuncs.com"
            };
            if (string.IsNullOrEmpty(bucket))
            {
                // 设置默认bucket
                OSSConfig.OSSBucket = isDev ? "olss-ols-cdn-dev" : "olss-ols-cdn";
            }
            else
            {
                OSSConfig.OSSBucket = bucket;
            }
            return OSSConfig;
        }
    }
}
