/*
 * Copyright (c) 2011, Oracle  and/or its affiliates. All rights reserved.
 */

package com.sun.javafx.tk.quantum;

import javafx.animation.Animation.Status;
import javafx.animation.FadeTransition;
import javafx.animation.PauseTransition;
import javafx.animation.SequentialTransition;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.util.Duration;

import com.sun.javafx.sg.prism.NGNode;

import java.util.Locale;
import java.util.ResourceBundle;

/**
 * @author Morris Meyer
 */
public class OverlayWarning {
    
    private static final Locale LOCALE = Locale.getDefault();
    
    private static final ResourceBundle RESOURCES =
        ResourceBundle.getBundle(OverlayWarning.class.getPackage().getName() +
                                 ".QuantumMessagesBundle", LOCALE);
    
    public static String localize(String msg) {
        return RESOURCES.getString(msg);
    }

    private static final float  PAD      = 40f;
    private static final float  RECTW    = 600f;
    private static final float  RECTH    = 100f;
    private static final float  ARC      = 20f;
    private static final int    FONTSIZE = 24;
    
    private ViewScene               view;
    private Group                   sceneRoot;
    private SequentialTransition    overlayTransition;
    private AbstractPainter         painter;
    private boolean                 warningTransition;
    
    public OverlayWarning(final ViewScene vs) {
        view = vs;
        
        sceneRoot = createOverlayGroup();
        painter = view.getPen().getPainter();                            

        // TODO - needs to be thread-safe - see RT-13813
        Scene.impl_setAllowPGAccess(true);
        painter.setOverlayRoot((NGNode)sceneRoot.impl_getPGNode());
        Scene.impl_setAllowPGAccess(false);
        
        PauseTransition pause = new PauseTransition(Duration.millis(4000));
        FadeTransition fade = new FadeTransition(Duration.millis(1000), sceneRoot);
        fade.setFromValue(1);
        fade.setToValue(0);
        
        overlayTransition = new SequentialTransition();
        overlayTransition.getChildren().add(pause);
        overlayTransition.getChildren().add(fade);
        
        overlayTransition.setOnFinished(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent event) {
                painter.setRenderOverlay(false);
                view.entireSceneNeedsRepaint();
                warningTransition = false;
            }
        });
    }

    protected ViewScene getView() {
        return view;
    }

    protected final void setView(ViewScene vs) {
        if (painter != null) {
            painter.setRenderOverlay(false);
            view.entireSceneNeedsRepaint();
        }

        view = vs;
        painter = vs.getPen().getPainter();

        // TODO - needs to be thread-safe - see RT-13813
        Scene.impl_setAllowPGAccess(true);
        painter.setOverlayRoot((NGNode)sceneRoot.impl_getPGNode());
        Scene.impl_setAllowPGAccess(false);

        painter.setRenderOverlay(true);
        view.entireSceneNeedsRepaint();

        overlayTransition.setOnFinished(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent event) {
                painter.setRenderOverlay(false);
                view.entireSceneNeedsRepaint();
                warningTransition = false;
            }
        });
   }
    
    protected void warn() {
        warningTransition = true;
        painter.setRenderOverlay(true);
        overlayTransition.play();
    }

    protected void cancel() {
        if (overlayTransition != null &&
            overlayTransition.getStatus() == Status.RUNNING) {

            overlayTransition.stop();
            
            painter.setRenderOverlay(false);
            view.entireSceneNeedsRepaint();
            warningTransition = false;
        }
    }

    protected boolean inWarningTransition() {
        return warningTransition;
    }
    
    private Text text;
    private Rectangle background;
    private Group root;
    
    private Group createOverlayGroup() {
        final Scene scene = new Scene(new Group());
        final Font font = new Font(Font.getDefault().getFamily(), FONTSIZE);
        final Text text = new Text();
        final Rectangle2D screenBounds = javafx.stage.Screen.getPrimary().getBounds();
        
        scene.setFill(null);
        
        String TEXT_CSS =
            "-fx-effect: dropshadow(two-pass-box, rgba(0,0,0,0.75), 3, 0.0, 0, 2);";
        text.setText(localize("OverlayWarningESC"));
        text.setStroke(Color.WHITE);
        text.setFill(Color.WHITE);
        text.setFont(font);
        text.setWrappingWidth(RECTW - PAD - PAD);
        text.setStyle(TEXT_CSS);
        text.setTextAlignment(TextAlignment.CENTER);
        
        Rectangle background = createBackground(text, screenBounds);

        Group root = (Group)scene.getRoot();
        root.getChildren().add(background);
        root.getChildren().add(text);

        Scene.impl_setAllowPGAccess(true);
        text.impl_updatePG();
        background.impl_updatePG();
        root.impl_updatePG();
        Scene.impl_setAllowPGAccess(false);
        return root;
    }
    
    private Rectangle createBackground(Text text, Rectangle2D screen) {
        Rectangle rectangle = new Rectangle();

        double textW = text.getLayoutBounds().getWidth();
        double textH = text.getLayoutBounds().getHeight();
        double rectX = (screen.getWidth() - RECTW) / 2.0;
        double rectY = (screen.getHeight() / 2.0);

        rectangle.setWidth(RECTW);
        rectangle.setHeight(RECTH);
        rectangle.setX(rectX);
        rectangle.setY(rectY - RECTH);
        rectangle.setArcWidth(ARC);
        rectangle.setArcHeight(ARC);
        rectangle.setFill(Color.gray(0.0, 0.6));
        
        text.setX(rectX + ((RECTW - textW) / 2.0));
        text.setY(rectY - (RECTH  / 2.0) + ((textH - text.getBaselineOffset()) / 2.0));

        return rectangle;
    }
}



