package ctbrec.ui.news;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Set;

import ctbrec.io.HtmlParser;
import ctbrec.ui.DesktopIntegration;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;

public class StatusPane extends StackPane {

    TextArea content;
    Button reply;

    public StatusPane(Status status) {
        String text = HtmlParser.getText("<div>" + status.getContent() + "</div>", "div");

        content = new TextArea(text);
        content.setMaxHeight(130);
        content.setEditable(false);
        content.setWrapText(true);
        getChildren().add(content);

        ZonedDateTime createdAt = status.getCreationTime();
        String creationTime = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT).format(createdAt);
        Label time = new Label(creationTime);
        time.setStyle("-fx-background-color: -fx-base");
        time.setOpacity(.7);
        time.setPadding(new Insets(3));
        time.setOnMouseEntered(evt -> time.setOpacity(1));
        time.setOnMouseExited(evt -> time.setOpacity(.7));
        getChildren().add(time);
        StackPane.setMargin(time, new Insets(5, 5, 5, 10));
        StackPane.setAlignment(time, Pos.BOTTOM_LEFT);
        Rectangle clip = new Rectangle(time.getWidth(), time.getHeight());
        clip.heightProperty().bind(time.heightProperty());
        clip.widthProperty().bind(time.widthProperty());
        clip.setArcHeight(5);
        clip.arcWidthProperty().bind(clip.arcHeightProperty());
        time.setClip(clip);

        reply = new Button("Reply");
        reply.setOnAction((evt) -> DesktopIntegration.open(status.getUrl()));
        getChildren().add(reply);
        StackPane.setMargin(reply, new Insets(5, 5, 5, 5));
        StackPane.setAlignment(reply, Pos.BOTTOM_RIGHT);
    }

    @Override
    protected void layoutChildren() {
        ObservableList<Node> childrenUnmodifiable = content.getChildrenUnmodifiable();
        for (Node node : childrenUnmodifiable) {
            if (node instanceof ScrollPane) {
                ScrollPane scrollPane = (ScrollPane) node;
                Set<Node> nodes = scrollPane.lookupAll(".scroll-bar");
                for (final Node child : nodes) {
                    if (child instanceof ScrollBar) {
                        ScrollBar sb = (ScrollBar) child;
                        if (sb.getOrientation() == Orientation.VERTICAL) {
                            if (sb.isVisible()) {
                                StackPane.setMargin(reply, new Insets(5, 22, 5, 5));
                            } else {
                                StackPane.setMargin(reply, new Insets(5, 5, 5, 5));
                            }
                        }
                    }
                }
            }
        }
        super.layoutChildren();
    }
}
