using Minio;
using Minio.DataModel.Args;
using MySqlConnector;
using skyscraper5.Data;
using skyscraper5.Data.MySql;
using skyscraper5.Skyscraper.Scraper.Storage;
using skyscraper5.Skyscraper.Scraper.Storage.Split;

namespace skyscraper5.Storage.MariaDbMinio
{
    [ScrapeStorageFactoryId(2,"MariaDB & MinIO",false)]
    public class MariaDbMinioStorageFactory : IScraperStorageFactory
    {
        public IScraperStroage CreateScraperStroage()
        {
            MySqlConnectionStringBuilder mcsb = new MySqlConnectionStringBuilder();
            mcsb.Database = MariaDbDatabase;
            mcsb.Password = MariaDbPassword;
            mcsb.Port = MariaDbPort;
            mcsb.Server = MariaDbHost;
            mcsb.UserID = MariaDbUsername;
            mcsb.ApplicationName = "skyscraper5";
            mcsb.Pooling = true;

            IMinioClient mc = new MinioClient()
                .WithEndpoint(MinioEndpoint)
                .WithCredentials(MinioAccessKey, MinioSecretKey)
                .WithSSL(MinioSecure).Build();
            BucketExistsArgs bucketExistsArgs = new BucketExistsArgs().WithBucket(MinioBucket);
            bool bucketExists = mc.BucketExistsAsync(bucketExistsArgs).Result;
            if (!bucketExists)
            {
                MakeBucketArgs makeBucketArgs = new MakeBucketArgs().WithBucket(MinioBucket);
                mc.MakeBucketAsync(makeBucketArgs).Wait();
            }

            MySqlDataStorage myds = new MySqlDataStorage(mcsb);

            MinioObjectStorage mos = new MinioObjectStorage(mc, MinioBucket);

            return new SplitScraperStorage(myds, mos);
        }


        public string MariaDbHost { get; set; }
        public ushort MariaDbPort { get; set; }
        public string MariaDbUsername { get; set; }
        public string MariaDbPassword { get; set; }
        public string MariaDbDatabase { get; set; }
        public string MinioEndpoint { get; set; }
        public string MinioAccessKey { get; set; }
        public string MinioSecretKey { get; set; }
        public bool MinioSecure { get; set; }
        public string MinioBucket { get; set; }
    }
}
