using Minio;
using Minio.DataModel.Args;
using skyscraper5.Skyscraper.Plugins;
using skyscraper8.Skyscraper.Plugins;
using skyscraper8.Skyscraper.Scraper.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper5.Data.Minio
{
	[SkyscraperPlugin]
	[StorageId(1)]
	[StorageName("MinIO")]
	public class MinioObjectStorageFactory : ObjectStorageFactory
	{
		private static PluginLogger logger = PluginLogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

		public string Bucket { get; set; }

		public bool Secure { get; set; }

		public string SecretKey { get; set; }

		public string AccessKey { get; set; }

		public string Endpoint { get; set; }

		public ObjectStorage CreateObjectStorage()
		{
			IMinioClient mc = new MinioClient()
				.WithEndpoint(Endpoint)
				.WithCredentials(AccessKey, SecretKey)
				.WithSSL(Secure).Build();
			BucketExistsArgs bucketExistsArgs = new BucketExistsArgs().WithBucket(Bucket);
			Task<bool> bucketExistsAsync = mc.BucketExistsAsync(bucketExistsArgs);
			bucketExistsAsync.Wait();
			bool bucketExists = mc.BucketExistsAsync(bucketExistsArgs).Result;
			if (!bucketExists)
			{
				logger.Log(PluginLogLevel.Info, "Creating MinIO Bucket: {0}", Bucket);
				MakeBucketArgs makeBucketArgs = new MakeBucketArgs().WithBucket(Bucket);
				mc.MakeBucketAsync(makeBucketArgs).Wait();
			}

			MinioObjectStorage minioOs = new MinioObjectStorage(mc, Bucket);
			return minioOs;
		}

		public bool IsEquivalent(DataStorageFactory dataStorageFactory)
		{
			return false;
		}
	}
}
