﻿using AutoMapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using TheUnacademicPortfolio.Business.Profiles;
using TheUnacademicPortfolio.Business.Services;
using TheUnacademicPortfolio.Business.Services.Contracts;
using TheUnacademicPortfolio.Data.Context;
using TheUnacademicPortfolio.Data.Repositories;
using TheUnacademicPortfolio.Data.Repositories.Contracts;
using TheUnacademicPortfolio.Profiles;

namespace TheUnacademicPortfolio {
    public class Startup {
        private MapperConfiguration _mapperConfiguration;

        public Startup (IConfiguration configuration) {
            Configuration = configuration;
            _mapperConfiguration = new MapperConfiguration (cfg => {
                cfg.AddProfile<ApiProfile> ();
                cfg.AddProfile<BusinessMappingProfile> ();
            });
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices (IServiceCollection services) {
            services.AddMvc ().AddJsonOptions (opt =>
                opt.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver ()
            );

            services.AddDbContext<TheUnacademicPortfolioContext> (
                options => options.UseMySql (Configuration.GetConnectionString ("DefaultConnection")), ServiceLifetime.Transient);

            services.AddSingleton (opt => _mapperConfiguration.CreateMapper ());

            services.AddTransient<IStudentService, StudentService> ();
            services.AddTransient<ITeacherService, TeacherService> ();
            services.AddTransient<IMajorService, MajorService> ();
            services.AddTransient<ISubjectService, SubjectService> ();
            services.AddTransient<IProjectService, ProjectService> ();

            services.AddTransient<IStudentRepository, StudentRepository> ();
            services.AddTransient<ITeacherRepository, TeacherRepository> ();
            services.AddTransient<IMajorRepository, MajorRepository> ();
            services.AddTransient<ISubjectRepository, SubjectRepository> ();
            services.AddTransient<IProjectRepository, ProjectRepository> ();

            services.AddCors (options => {
                options.AddPolicy ("AllowAll", p => {
                    p.AllowAnyOrigin ()
                        .AllowAnyHeader ()
                        .AllowAnyMethod ();
                });
            });
            
            services.AddMvc ();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure (IApplicationBuilder app, IHostingEnvironment env) {
            if (env.IsDevelopment ()) {
                app.UseDeveloperExceptionPage ();

            }

            app.UseCors ("AllowAll");

            app.UseDefaultFiles ();
            app.UseStaticFiles ();
            app.UseMvc ();
        }
    }
}