/*
 * Copyright 2016 Crown Copyright
 *
 * 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 stroom.main.client.presenter;

import stroom.alert.client.event.AlertEvent;
import stroom.content.client.event.RefreshCurrentContentTabEvent;
import stroom.core.client.KeyboardInterceptor;
import stroom.core.client.presenter.CorePresenter;
import stroom.task.client.TaskEndEvent;
import stroom.task.client.TaskStartEvent;
import stroom.task.client.event.OpenTaskManagerEvent;
import stroom.ui.config.client.UiConfigCache;
import stroom.ui.config.shared.UiConfig;
import stroom.widget.tab.client.event.MaximiseEvent;
import stroom.widget.util.client.DoubleSelectTester;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasDoubleClickHandlers;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.http.client.UrlBuilder;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.MyPresenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;

public class MainPresenter extends MyPresenter<MainPresenter.MainView, MainPresenter.MainProxy> {

    @ContentSlot
    public static final Type<RevealContentHandler<?>> MENUBAR = new Type<>();
    @ContentSlot
    public static final Type<RevealContentHandler<?>> EXPLORER = new Type<>();
    @ContentSlot
    public static final Type<RevealContentHandler<?>> CONTENT = new Type<>();
    private final Timer refreshTimer;
    private boolean click;

    @Inject
    public MainPresenter(final EventBus eventBus,
                         final MainView view,
                         final MainProxy proxy,
                         final KeyboardInterceptor keyboardInterceptor,
                         final UiConfigCache clientPropertyCache) {
        super(eventBus, view, proxy);

        // Handle key presses.
        keyboardInterceptor.register(view.asWidget());

        addRegisteredHandler(TaskStartEvent.getType(), event -> {
            // DebugPane.debug("taskStart:" + event.getTaskCount());

            // Always try and start spinner even if it might be spinning
            // already.
            if (view.getSpinner() != null) {
                view.getSpinner().start();
            }
        });
        addRegisteredHandler(TaskEndEvent.getType(), event -> {
            // DebugPane.debug("taskEnd:" + event.getTaskCount());

            // Only stop spinner if we are no longer executing any tasks.
            if (event.getTaskCount() == 0) {
                if (view.getSpinner() != null) {
                    view.getSpinner().stop();
                }
            }
        });
        registerHandler(clientPropertyCache.addPropertyChangeHandler(
                event -> {
                    final UiConfig uiConfig = event.getProperties();
                    getView().setBanner(uiConfig.getMaintenanceMessage());
                    if (uiConfig == null || uiConfig.getRequireReactWrapper()) {
                        final Object parentIframe = getParentIframe();
                        if (parentIframe == null) {
                            AlertEvent.fireWarn(
                                    this,
                                    "You are reached Stroom outside of an IFrame you will now be redirected",
                                    () -> {
                                        UrlBuilder builder = new UrlBuilder();
                                        builder.setProtocol(Window.Location.getProtocol());
                                        builder.setHost(Window.Location.getHost());
                                        Window.Location.replace(builder.buildString());
                                    });
                        }
                    }
                }
        ));

        registerHandler(view.getSpinner().addClickHandler(event -> {
            if (click) {
                click = false;
                OpenTaskManagerEvent.fire(MainPresenter.this);

            } else {
                final Timer clickTimer = new Timer() {
                    @Override
                    public void run() {
                        if (click) {
                            stopAutoRefresh();
                            // refresh();
                            startAutoRefresh();
                        }

                        click = false;
                    }
                };
                click = true;
                clickTimer.schedule(DoubleSelectTester.DOUBLE_SELECT_DELAY);
            }
        }));
        addRegisteredHandler(MaximiseEvent.getType(), event -> view.maximise(event.getView()));

        refreshTimer = new Timer() {
            @Override
            public void run() {
                RefreshCurrentContentTabEvent.fire(MainPresenter.this);
            }
        };

        // Start the auto refresh timer.
        startAutoRefresh();
    }

    private void startAutoRefresh() {
        refreshTimer.scheduleRepeating(30000); // 30 second default.
    }

    private void stopAutoRefresh() {
        refreshTimer.cancel();
    }

    @Override
    protected void revealInParent() {
        RevealContentEvent.fire(this, CorePresenter.CORE, this);
        RootPanel.get("logo").setVisible(false);
    }

//    public native boolean isInIFrame() /*-{
//        try {
//            return window.frameElement != null;
//            //return window.self !== window.top;
//        } catch (e) {
//            return true;
//        }
//	}-*/;
//
//    public native Object getIframe() /*-{
//        try {
//            return window.frameElement;
//        } catch (e) {
//            return true;
//        }
//	}-*/;

    public native Object getParentIframe() /*-{
        return window.parent.frameElement;
	}-*/;

    @ProxyCodeSplit
    public interface MainProxy extends Proxy<MainPresenter> {

    }

    public interface MainView extends View {

        SpinnerDisplay getSpinner();

        void maximise(View view);

        void setBanner(String text);
    }

    public interface SpinnerDisplay extends HasClickHandlers, HasDoubleClickHandlers {

        void start();

        void stop();
    }
}

@@ -6442,27 +6442,30 @@
         %7D%0A//
-%09%7D-
+    %7D 
 */;%0A//%0A//   
@@ -6626,19 +6626,20 @@
     %7D%0A//
-%09%7D-
+  %7D 
 */;%0A%0A   
@@ -6680,17 +6680,17 @@
 ame() /*
--
+ 
 %7B%0A      
@@ -6730,11 +6730,11 @@
 nt;%0A
-%09%7D-
+ %7D 
 */;%0A

Fix checkstyle issue