namespace Carelearning.Prototype
{
    using DotVVM.Framework.Configuration;
    using DotVVM.Framework.ResourceManagement;
    using DotVVM.Framework.Controls.Bootstrap4;
    using DotVVM.Contrib.Select2;
    using Microsoft.Extensions.DependencyInjection;
    using System.Reflection;

    public class DotvvmStartup : IDotvvmStartup, IDotvvmServiceConfigurator
    {
        public void Configure(DotvvmConfiguration config, string applicationPath)
        {
            var assembly = Assembly.GetExecutingAssembly();
            config.Markup.Assemblies.Add(assembly.FullName);

            ConfigureControls(config, applicationPath);
            ConfigureResources(config, applicationPath);
            ConfigureRoutes(config, applicationPath);

            // Configure Bootstrap 4 with LibMan-managed resources
            config.AddBootstrap4Configuration(new DotvvmBootstrapOptions
            {
                BootstrapCssResource = new StylesheetResource(new UrlResourceLocation("~/Styles/css/bootstrap.min.css")),
                BootstrapJsResource = new ScriptResource(new UrlResourceLocation("~/Styles/js/bootstrap.bundle.min.js")),
                JQueryResource = new ScriptResource(new UrlResourceLocation("~/Scripts/jquery.min.js")),
                IncludeBootstrapResourcesInPage = true,
                IncludeJQueryResourceInPage = true
            });
        }

        public void ConfigureServices(IDotvvmServiceCollection svc)
        {
            svc.AddBusinessPack();
            svc.AddDefaultTempStorages("temp");
            svc.AddHotReload();
        }

        #region Private Methods
        private void ConfigureControls(DotvvmConfiguration config, string applicationPath)
        {
            // register code-only controls and markup controls
            // Add control configurations here as needed
            config.AddContribSelect2Configuration();
        }

        private void ConfigureResources(DotvvmConfiguration config, string applicationPath)
        {
            RegisterCss(config);
            RegisterJavascript(config);
        }

        private void ConfigureRoutes(DotvvmConfiguration config, string applicationPath)
        {
            config.RouteTable.Add("Default", "", "Views/Default.dothtml");

            // Auto-discover additional routes if needed
            // config.RouteTable.AutoDiscoverRoutes(new DefaultRouteStrategy(config));
        }

        private void RegisterCss(DotvvmConfiguration config)
        {
            // Register custom CSS resources
            config.Resources.Register("Styles", new StylesheetResource
            {
                Location = new UrlResourceLocation("~/Styles/styles.css")
            });
        }

        private void RegisterJavascript(DotvvmConfiguration config)
        {
            // Register SignalR from LibMan
            config.Resources.Register("SignalR", new ScriptResource
            {
                Dependencies = new[] { "jquery" },
                Location = new UrlResourceLocation("~/Scripts/jquery.signalR.min.js")
            });

            // Add more custom JavaScript resources here as needed
        }
        #endregion
    }
}
