package GUI;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import model.SOMConfig;
import model.SOMImage;
import model.SOMLayout;
import workers.FilesWorker;
import workers.ScaleWorker;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Simon on 13.06.2016.
 */
public class CaptureCtl {

    @FXML
    public StackPane capturePane;
    @FXML
    public Label countdownText;
    @FXML
    public ImageView image;
    @FXML
    public ProgressIndicator pictureLoadIndicator;

    // series switch
    public boolean series = false;
    // Timer for coutdown
    Timer timer;
    // series picture counter
    private int seriesCounter;
    // countdown counter
    private int counter = 0;

    // Initialisation
    public void initialize() {
        setPane();
        setImage();
        setCountdownText();
        setPictureLoadIndicator();
    }

    private void setPictureLoadIndicator() {
        pictureLoadIndicator.setVisible(false);

        pictureLoadIndicator.setPrefHeight(SOMLayout.screenHeight / 4);
        pictureLoadIndicator.setPrefWidth(SOMLayout.screenHeight / 4);

        pictureLoadIndicator.setMaxSize(SOMLayout.screenHeight / 2, SOMLayout.screenHeight / 2);

        pictureLoadIndicator.setStyle("-fx-progress-color: white;");
    }

    private void setCountdownText() {

        capturePane.setAlignment(Pos.CENTER);
        countdownText.setAlignment(Pos.CENTER);

        countdownText.setPrefSize(SOMLayout.screenHeight / 2, SOMLayout.screenHeight / 2);
        countdownText.setBackground(SOMLayout.getTransparentBG20rounded(SOMLayout.screenHeight / 4));

        countdownText.setText(null);
        countdownText.setFont(new Font("Century Gothic Bold", SOMLayout.screenHeight / 4));
        countdownText.setTextFill(SOMLayout.WHITE);
    }

    public void setPane() {
        capturePane.setBackground(SOMLayout.BLACK_BG);
    }

    public void setImage() {
        // 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 / 2);

        // 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);
    }

    public void setSeries(Boolean series) {
        this.series = series;
    }

    /* LOGIC
    * -----------------------------------------------------------------
    * -----------------------------------------------------------------
    * */
    protected void shoot(boolean series) {
        // Check for series mode
        System.out.println("shoot");

        // Change to the capture screen
        SOMGUI.changetocapture();

        if (series)
            // if series mode is on, set the counter to amount of pictures to make
            seriesCounter = SOMConfig.SERIES_COUNTER;
        else
            // else set it to one
            seriesCounter = 1;


        //start the first countdown
        countdown2();
    }

    protected void countdown2() {
        // set the Countdown Value
        counter = SOMConfig.COUNTDOWN_COUNTER;

        // open a Timer
        timer = new Timer();
        // give it a Task
        TimerTask task = new TimerTask() {
            @Override
            public void run() {

                if (seriesCounter == 0)
                    counter = 0;

                System.out.println("countdown " + counter);
                // It feels better if there is still
                // a second after the counter is at
                // 0, so it will count to -1

                // show smile instead of a zero
                if (counter == 0) {

                    Platform.runLater(new Runnable() {
                        public void run() {
                            // Remove Text
                            countdownText.setText(null);
                            // Set Countdown Smile Image
                            countdownText.setGraphic(new ImageView(SOMLayout.SMILE_IMAGE));
                        }
                    });

                }
                // shoot the camera
                else if (counter <= -1) {

                    // Run on Javafx Thread
                    Platform.runLater(new Runnable() {
                        public void run() {
                            // Remove Graphic
                            countdownText.setGraphic(null);
                            // Remove Text
                            countdownText.setText(null);
                        }
                    });

                    // cancel the timer
                    timer.cancel();
                    // show the Load indicator
                    pictureLoadIndicator.setVisible(true);
                    // hide the Countdown Text
                    countdownText.setVisible(false);
                    // actually take the picture
                    takePicture();
                    // Count down one Picture
                    seriesCounter--;

                    // check if pictures are left
                    if (seriesCounter > 0) {
                        // restart if pictures left
                        countdown2();


                    } else {
                        // Change the UI
                        Platform.runLater(new Runnable() {
                            public void run() {
                                // change to Picture scene
                                SOMGUI.changetopicture();
                                // set the Gallery page to the first
                                SOMGUI.galleryCtl.setPage();
                                // update the Gallery
                                SOMGUI.galleryCtl.getGalleryImages();
                            }
                        });
                    }
                }
                // Show the Time left
                else {

                    Platform.runLater(new Runnable() {
                        public void run() {
                            // Set Countdown Text
                            countdownText.setText(String.valueOf(counter + 1));
                            // Set the Counter Visible
                            countdownText.setVisible(true);
                        }
                    });

                }
                counter--;
            }
        };

        // start the timer
        timer.schedule(task, 0, 1000);
    }

    private void takePicture() {
        // Capture the image
        SOMImage newImage = FilesWorker.waitForImageAndCapture();

        //Scale the image to preview
        ScaleWorker.scale(newImage, ScaleWorker.PREVIEW);
        // set te image to UI
        image.setImage(new Image(newImage.getPrv().toURI().toString()));
        SOMGUI.pictureCtl.image.setImage(new Image(newImage.getPrv().toURI().toString()));

        // trigger the thumb calculation
        ScaleWorker.scale(newImage, ScaleWorker.THUMBNAIL);

    }

//
}
