using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;

namespace OnlineSalesAutoCrop.CoreAPI
{
	/// <summary>
	/// 
	/// </summary>
	public class Program
	{	
		/// <summary>
		/// 
		/// </summary>
		protected Program()
		{
			//Noting to do here	
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="args"></param>
		public static void Main(string[] args)
		{
			bool hasError = false;
			IHost host = CreateHostBuilder(args).Build();
			ILogger<Program> logger = host.Services.GetRequiredService<ILogger<Program>>();
			try
			{
				logger.LogInformation("Starting the application...");
				host.Run();
			}
			catch (Exception e)
			{
				hasError = true;
				logger.LogCritical(exception: e, message: "Application terminated unexpectedly.");
			}
			finally
			{
				if (hasError)
				{
					logger.LogCritical(message: "Application encountered an error.");
				}
				else
				{
					logger.LogInformation(message: "Application was started.");
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="args"></param>
		/// <returns></returns>

		public static IHostBuilder CreateHostBuilder(string[] args) =>
			Host.CreateDefaultBuilder(args)
				.ConfigureLogging(logging =>
				{
					logging.ClearProviders();
					logging.AddConsole();
					logging.AddDebug();
				})
				 .ConfigureWebHostDefaults(webBuilder =>
				{
					webBuilder.UseIISIntegration();
					webBuilder.UseStartup<Startup>();
				});
	}
}
