// Copyright 2016 Eivind Vegsundvåg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ninja.eivind.hotsreplayuploader.preloader;

import javafx.application.Preloader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;

public class ProgressMonitor implements ApplicationContextInitializer<ConfigurableApplicationContext>, ApplicationListener<ApplicationEvent> {

    /*
     * As of 2016-12-17, the bean definition count is 81. We might shrink this, though we'll probably grow it.
     * In any case, >= 75 seems to mean the context is fully mapped
     */
    private static final int MIN_BEAN_DEF_COUNT = 75;
    private static final Logger logger = LoggerFactory.getLogger(ProgressMonitor.class);
    private ConfigurableListableBeanFactory beanFactory;
    private PreloaderHandle handle;

    public ProgressMonitor(PreloaderHandle handle) {
        this.handle = handle;
    }


    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        applicationContext.addApplicationListener(this);
        beanFactory = applicationContext.getBeanFactory();
        beanFactory.addBeanPostProcessor(new ProgressMonitorBeanPostProcessor(beanFactory, handle, MIN_BEAN_DEF_COUNT));
    }

    private void notifyContextIsFullyLoaded() {
        handle.notifyPreloader(new Preloader.ProgressNotification(1));
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationReadyEvent) {
            notifyContextIsFullyLoaded();
        }
    }
}

@@ -677,65 +677,8 @@
 er;%0A
-import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 impo
@@ -1462,154 +1462,8 @@
 75;%0A
-    private static final Logger logger = LoggerFactory.getLogger(ProgressMonitor.class);%0A    private ConfigurableListableBeanFactory beanFactory;%0A
     
@@ -1494,16 +1494,16 @@
 handle;%0A
+
 %0A    pub
@@ -1692,113 +1692,151 @@
     
-applicationContext.addApplicationListener(this);%0A        beanFactory = applicationContext.getBeanFactory(
+final ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();%0A        applicationContext.addApplicationListener(this
 );%0A 

fix quality issues in progressmonitor