using Npgsql;
using skyscraper5.Skyscraper.Plugins;
using skyscraper8.Skyscraper.Plugins;
using skyscraper8.Skyscraper.Scraper.Storage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper5.Data.PostgreSql
{
	[SkyscraperPlugin]
	[StorageId(3)]
	[StorageName("PostgreSQL")]
	public class PostgresqlDataStoreFactory : DataStorageFactory
	{
		private static PluginLogger logger = PluginLogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

		public string Username { get; set; }

		public ushort Port { get; set; }

		public string Password { get; set; }

		public string Host { get; set; }

		public string Database { get; set; }

		public DataStorage CreateDataStorage()
		{
			logger.Log(PluginLogLevel.Info, "Connecting to postgres://{3}@{0}:{1}/{2}",Host,Port,Database,Username);
			NpgsqlConnectionStringBuilder ncsb = new NpgsqlConnectionStringBuilder();
			ncsb.Database = Database;
			ncsb.ApplicationName = "skyscraper5";
			ncsb.Host = Host;
			ncsb.Password = Password;
			ncsb.Pooling = true;
			ncsb.Port = Port;
			ncsb.Username = Username;

			if (Debugger.IsAttached)
				ncsb.Timeout = 1024;

			return new PostgresqlDataStore(ncsb);
		}
	}
}
