package codeOrchestra.colt.core.license;

import codeOrchestra.colt.core.ui.ColtApplication;
import codeOrchestra.util.StringUtils;
import org.controlsfx.dialog.Dialogs;

/**
 * @author Alexander Eliseyev
 */
public class StartupInterceptor {

    private final static StartupInterceptor instance = new StartupInterceptor();

    public static StartupInterceptor getInstance() {
        return instance;
    }

    public StartupInterceptType interceptStart() {
        final ExpirationStrategy expirationStrategy = ExpirationHelper.getExpirationStrategy();

        // Report serial number every 10 seconds
        if (StringUtils.isNotEmpty(CodeOrchestraLicenseManager.getLegacySerialNumber())) {
            if (expirationStrategy.isTrialOnly() && !new ActivationReporter(CodeOrchestraLicenseManager.getLegacySerialNumber()).report()) {
                Dialogs.create()
                        .owner(ColtApplication.get().getPrimaryStage())
                        .title("COLT License")
                        .message("COLT beta version requires an active internet connection to start.")
                        .nativeTitleBar()
                        .showError();
                return StartupInterceptType.EXIT_NO_CONNECTION;
            }

            new Thread() {
                public void run() {
                    while (true) {
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            // ignore
                        }
                        new ActivationReporter(CodeOrchestraLicenseManager.getLegacySerialNumber()).report();
                    }
                }

                ;
            }.start();
        }

        // Trial-only (beta versions) - no serial number is checked
        if (expirationStrategy.isTrialOnly()) {
            if (ExpirationHelper.getExpirationStrategy().hasExpired()) {
                expirationStrategy.showLicenseExpiredDialog();
                return StartupInterceptType.EXIT_EXPIRED;
            } else {
                if (UsagePeriods.getInstance().isCurrentTimePresentInUsagePeriods()) {
                    Dialogs.create()
                            .owner(ColtApplication.get().getPrimaryStage())
                            .title("Evaluation License")
                            .message("Something is wrong with the system clock\nCOLT was launched already on the currently set time.")
                            .nativeTitleBar()
                            .showError();

                    return StartupInterceptType.EXIT_UNKNOWN;
                }

                expirationStrategy.showLicenseExpirationInProgressDialog();

                return StartupInterceptType.START;
            }
        }

        // No-trial version (serial-number only)
        if (!expirationStrategy.allowTrial() && CodeOrchestraLicenseManager.noSerialNumberPresent() && !expirationStrategy.allowsDemo()) {
            if (!expirationStrategy.showLicenseExpiredDialog()) {
                return StartupInterceptType.EXIT_EXPIRED;
            }
        }

        // Trial version with no serial
        if ((expirationStrategy.allowTrial() && CodeOrchestraLicenseManager.noSerialNumberPresent()) || (!expirationStrategy.allowsDemo() && expirationStrategy.isSubscriptionBased() && !CodeOrchestraLicenseManager.noSerialNumberPresent())) {
            if (UsagePeriods.getInstance().isCurrentTimePresentInUsagePeriods()) {
                String title = expirationStrategy.isSubscriptionBased() ? "COLT Subscription" : "Evaluation License";
                Dialogs.create()
                        .owner(ColtApplication.get().getPrimaryStage())
                        .title(title)
                        .message("Something is wrong with the system clock\nCOLT was launched already on the currently set time.")
                        .nativeTitleBar()
                        .showError();

                return StartupInterceptType.EXIT_UNKNOWN;
            }

            boolean expired;
            if (ExpirationHelper.getExpirationStrategy().hasExpired()) {
                expired = !expirationStrategy.showLicenseExpiredDialog();
            } else {
                expirationStrategy.showLicenseExpirationInProgressDialog();
                expired = false;
            }

            if (expired) {
                expirationStrategy.handleExpiration();

                if (expirationStrategy.exitIfExpired()) {
                    return StartupInterceptType.EXIT_EXPIRED;
                }
            }
        }

        // Demo version with subscription
        if (expirationStrategy.allowsDemo() && expirationStrategy.isSubscriptionBased()) {
            if (UsagePeriods.getInstance().isCurrentTimePresentInUsagePeriods()) {
                Dialogs.create()
                        .owner(ColtApplication.get().getPrimaryStage())
                        .title("COLT Subscription")
                        .message("Something is wrong with the system clock\nCOLT was launched already on the currently set time.")
                        .nativeTitleBar()
                        .showError();

                return StartupInterceptType.EXIT_UNKNOWN;
            }

            boolean expired;
            if (ExpirationHelper.getExpirationStrategy().hasExpired()) {
                expired = !expirationStrategy.showLicenseExpiredDialog();
            } else {
                expirationStrategy.showLicenseExpirationInProgressDialog();
                expired = false;
            }

            if (expired) {
                expirationStrategy.handleExpiration();
            }
        }

        return StartupInterceptType.START;
    }

}

@@ -556,1206 +556,8 @@
 );%0A%0A
-        // Report serial number every 10 seconds%0A        if (StringUtils.isNotEmpty(CodeOrchestraLicenseManager.getLegacySerialNumber())) %7B%0A            if (expirationStrategy.isTrialOnly() && !new ActivationReporter(CodeOrchestraLicenseManager.getLegacySerialNumber()).report()) %7B%0A                Dialogs.create()%0A                        .owner(ColtApplication.get().getPrimaryStage())%0A                        .title(%22COLT License%22)%0A                        .message(%22COLT beta version requires an active internet connection to start.%22)%0A                        .nativeTitleBar()%0A                        .showError();%0A                return StartupInterceptType.EXIT_NO_CONNECTION;%0A            %7D%0A%0A            new Thread() %7B%0A                public void run() %7B%0A                    while (true) %7B%0A                        try %7B%0A                            Thread.sleep(10000);%0A                        %7D catch (InterruptedException e) %7B%0A                            // ignore%0A                        %7D%0A                        new ActivationReporter(CodeOrchestraLicenseManager.getLegacySerialNumber()).report();%0A                    %7D%0A                %7D%0A%0A                ;%0A            %7D.start();%0A        %7D%0A%0A
     

remove logic for "Report serial number every 10 seconds"