package GUI;

import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TouchEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import model.SOMEvent;
import model.SOMImage;
import model.SOMLayout;
import workers.ScaleWorker;

import java.util.List;

/**
 * Created by Simon on 13.06.2016.
 */
public class PictureCtl {
    @FXML
    public StackPane pane;

    @FXML
    public GridPane grid;

    @FXML
    public Button singleBtn;
    @FXML
    public Button seriesBtn;
    @FXML
    public Button galleryBtn;

    @FXML
    public ImageView image;

    public void initialize() {

        // Basic Layout setup
        setPane();
        setGrid();

        // Buttons setup
        setGalleryBtn();
        setSeriesBtn();
        setSingleBtn();

        // Image Setup
        setImage();
    }

    private void setPane() {
        pane.setPrefSize(SOMLayout.screenWidth, SOMLayout.screenHeight);
        pane.setBackground(SOMLayout.BLACK_BG);
    }

    private void setGrid() {
        grid.setPrefSize(SOMLayout.screenWidth, SOMLayout.screenHeight);

        grid.setAlignment(Pos.CENTER);

        GridPane.setHalignment(galleryBtn, HPos.CENTER);
        GridPane.setHalignment(singleBtn, HPos.CENTER);
        GridPane.setHalignment(seriesBtn, HPos.CENTER);
    }

    private void setSingleBtn() {
        // get the button height & width
        int height = (SOMLayout.screenHeight / 4);

        // set the Button height & width
        singleBtn.setPrefSize(height, height);

        // Make it a circle
        singleBtn.setBackground(SOMLayout.getTransparentBG10rounded(height / 2));

        //Set Text Color
        singleBtn.setTextFill(SOMLayout.WHITE);
        singleBtn.setText(null);

        // Set Image
        ImageView imageView = new ImageView(SOMLayout.SINGLE_PHOTO_IMAGE);
        singleBtn.setGraphic(imageView);

        // INTERACTION
        // Set the Action to run after Button is Pressed
        EventHandler singleCapturePressedEventHandler = new EventHandler<MouseEvent>() {
            public void handle(MouseEvent event) {
                singleBtn.setBackground(SOMLayout.getTransparentBG20rounded(SOMLayout.screenHeight / 4));
            }
        };

        // Set the Action to run after Button is Released
        EventHandler singleCaptureReleasedEventHandler = new EventHandler<MouseEvent>() {
            public void handle(MouseEvent event) {

                // Feedback (button gets less transparent)
                singleBtn.setBackground(SOMLayout.getTransparentBG10rounded(SOMLayout.screenHeight / 4));

                // Action
                action();
            }

            // Action Performed
            private void action() {
                // Capture one image (false means no series mode)
                SOMGUI.captureCtl.shoot(false);
            }
        };

        // Set Mouse Press Action
        singleBtn.setOnMousePressed(singleCapturePressedEventHandler);

        // Set Mouse Release Action
        singleBtn.setOnMouseReleased(singleCaptureReleasedEventHandler);

        // Set Touch Press Action
        singleBtn.setOnTouchPressed(singleCapturePressedEventHandler);

        // Set Touch Release Action
        singleBtn.setOnTouchReleased(singleCaptureReleasedEventHandler);
    }

    private void setSeriesBtn() {
        // get the button height & width
        int height = (SOMLayout.screenHeight / 4);

        // set the Button height & width
        seriesBtn.setPrefSize(height, height);

        // Make it a circle
        seriesBtn.setBackground(SOMLayout.getTransparentBG10rounded(height / 2));

        //Set Text Color
        seriesBtn.setTextFill(SOMLayout.WHITE);
        seriesBtn.setText(null);

        // Set Image
        ImageView imageView = new ImageView(SOMLayout.SERIES_PHOTO_IMAGE);

        seriesBtn.setGraphic(imageView);

        //INTERACTION
        //Set Action after Series Button is pressed
        EventHandler<MouseEvent> seriesCapturePressedEventHandler = new EventHandler<MouseEvent>() {
            public void handle(MouseEvent event) {
                // Feedback (Button gets lesstransparent)
                seriesBtn.setBackground(SOMLayout.getTransparentBG20rounded(SOMLayout.screenHeight / 4));
            }
        };
        //Set Action after series Button is released
        EventHandler<MouseEvent> seriesCaptureReleasedEventHandler = new EventHandler<MouseEvent>() {
            public void handle(MouseEvent event) {

                // Feedback (Button gets less transparent)
                seriesBtn.setBackground(SOMLayout.getTransparentBG10rounded(SOMLayout.screenHeight / 4));

                // Shoot series (true means series Mode)
                SOMGUI.captureCtl.shoot(true);
            }

        };        //Set Action after Series Button is pressed
        EventHandler<TouchEvent> seriesCapturePressedTouchEventHandler = new EventHandler<TouchEvent>() {
            public void handle(TouchEvent event) {
                // Feedback (Button gets lesstransparent)
                seriesBtn.setBackground(SOMLayout.getTransparentBG20rounded(SOMLayout.screenHeight / 4));
            }
        };
        //Set Action after series Button is released
        EventHandler<TouchEvent> seriesCaptureReleasedTouchEventHandler = new EventHandler<TouchEvent>() {
            public void handle(TouchEvent event) {

                // Feedback (Button gets less transparent)
                seriesBtn.setBackground(SOMLayout.getTransparentBG10rounded(SOMLayout.screenHeight / 4));

                // Shoot series (true means series Mode)
                SOMGUI.captureCtl.shoot(true);
            }

        };
        // Set Mouse Press Action
        seriesBtn.setOnMousePressed(seriesCapturePressedEventHandler);

        // Set Mouse Release Action
        seriesBtn.setOnMouseReleased(seriesCaptureReleasedEventHandler);
        // Set Mouse Press Action
        seriesBtn.setOnTouchPressed(seriesCapturePressedTouchEventHandler);

        // Set Mouse Release Action
        seriesBtn.setOnTouchReleased(seriesCaptureReleasedTouchEventHandler);
    }

    private void setGalleryBtn() {
        // get the button height & width
        int height = (SOMLayout.screenHeight / 4);

        // set the Button height & width
        galleryBtn.setPrefSize(height, height);

        // Make it a circle
        galleryBtn.setBackground(SOMLayout.getTransparentBG10rounded(height / 2));

        //Set Text Color
        galleryBtn.setTextFill(SOMLayout.WHITE);
        galleryBtn.setText(null);

        // Set Image
        ImageView imageView = new ImageView(SOMLayout.GALLERY_IMAGE);

        galleryBtn.setGraphic(imageView);

        // Set Mouse Press Action
        galleryBtn.setOnMousePressed(new EventHandler<MouseEvent>() {
            public void handle(MouseEvent event) {
                // Feedback
                galleryBtn.setBackground(SOMLayout.getTransparentBG20rounded(SOMLayout.screenHeight / 4));
            }
        });

        // Set Mouse Release Action
        galleryBtn.setOnMouseReleased(new EventHandler<MouseEvent>() {
            public void handle(MouseEvent event) {

                // Feedback
                galleryBtn.setBackground(SOMLayout.getTransparentBG10rounded(SOMLayout.screenHeight / 4));

                // Action
                action();
            }

            // Action Performed
            private void action() {
                SOMGUI.changetogallery();
            }
        });
    }

    private void setImage() {
//        // Set the image aspect ratio to be preserved
//        image.setPreserveRatio(true);
//
//        // Set the image to the image View
//        Image splash = SOMLayout.LOGO_IMAGE;
//        image.setImage(splash);
//
//        // make it fit the height of the Screen
//        image.setFitWidth(SOMLayout.screenHeight/2);

        // Set the image aspect ratio to be preserved
        image.setPreserveRatio(true);

        // Set the image to the image View
        image.setImage(SOMLayout.LOGO_IMAGE);

        // make it fit the height of the Screen
//        image.setFitWidth(SOMLayout.screenHeight);

        // Calculate the offset to center it horizontally if the image is wider than the screen (or narrower)
        double aspect = image.getImage().getWidth() / image.getImage().getHeight();
        double scaledWidth = aspect * SOMLayout.screenHeight;
        double offset = (scaledWidth - SOMLayout.screenWidth) / 2;

        image.setX(-offset);

        // Get the Event Images
        List<SOMImage> somImageList = SOMEvent.getInstance().getImages();
        int numberOfImages = somImageList.size();

        if (numberOfImages != 0) {
            // Set the last Image of the session
            SOMImage somImage = somImageList.get(numberOfImages - 1);

            // update the Image State
            // TODO Probably not necessary
            somImage.updateImageState();

            // Scale the Image if needed
            ScaleWorker.scale(somImage, ScaleWorker.PREVIEW);

            // set the Image to UI
            image.setImage(new Image(somImage.getPrv().toURI().toString()));
        }
    }
}
