/*
 * Copyright 2018 Red Hat, Inc. and/or its affiliates.
 *
 * 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 org.guvnor.messageconsole.client.console.widget.button;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.guvnor.messageconsole.client.console.MessageConsoleScreen;
import org.jboss.errai.ui.client.local.api.elemental2.IsElement;
import org.uberfire.client.mvp.PlaceManager;
import org.uberfire.client.mvp.PlaceStatus;
import org.uberfire.client.mvp.UberElemental;
import org.uberfire.mvp.impl.DefaultPlaceRequest;

public class ViewHideAlertsButtonPresenter {

    public interface View extends UberElemental<ViewHideAlertsButtonPresenter>,
                                  IsElement {

        void setAlertsActive(boolean active);

        void addCssClassToButtons(String cssClass);
    }

    private PlaceManager placeManager;

    private View view;

    @Inject
    public ViewHideAlertsButtonPresenter(final PlaceManager placeManager,
                                         final View view) {
        this.placeManager = placeManager;
        this.view = view;
    }

    @PostConstruct
    public void init() {
        view.init(this);

        final boolean isAlertsActive = PlaceStatus.OPEN.equals(placeManager.getStatus(MessageConsoleScreen.ALERTS));
        view.setAlertsActive(isAlertsActive);

        placeManager.registerOnOpenCallback(new DefaultPlaceRequest(MessageConsoleScreen.ALERTS),
                                            () -> view.setAlertsActive(true));
        placeManager.registerOnCloseCallback(new DefaultPlaceRequest(MessageConsoleScreen.ALERTS),
                                             () -> view.setAlertsActive(false));
    }

    public void addCssClassToButtons(final String cssClass) {
        view.addCssClassToButtons(cssClass);
    }

    public void viewAlerts() {
        placeManager.goTo(MessageConsoleScreen.ALERTS);
    }

    public void hideAlerts() {
        placeManager.closePlace(MessageConsoleScreen.ALERTS);
    }

    public View getView() {
        return view;
    }
}
