package com.moviesdb;

import javafx.fxml.FXML;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.control.Alert.AlertType;

import com.moviesdb.Movie;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import com.moviesdb.DataBaseConnector;

import java.awt.event.MouseEvent;
import java.time.chrono.AbstractChronology;

public class Controller {
    @FXML
    AnchorPane browserPane;
    @FXML
    MenuItem menuNew;
    @FXML
    GridPane moviesGrid;
    @FXML
    ScrollPane scrollPane;
    @FXML
    AnchorPane detailsPane;
    @FXML
    TextFlow detailsContent;
    @FXML
    AnchorPane detailsPicPane;
    @FXML
    AnchorPane filterPane;


    Label[] movies;
    Movie my_movie;
    DataBaseConnector my_connector;


    public void initialize(){
        my_connector = new DataBaseConnector();
        my_connector.initConnection("d","1");
    }

    public void loadPosters()
    {
        detailsPane.setMinWidth(300);
        //get movies array
        movies = new Label[10];
        //change grid size for number of movies
        for (int i = 0; i < 10; i++)
        {
            movies[i] = new Label();
            ImageView image = new ImageView(new Image(getClass().getResourceAsStream("pic.jpg")));
            image.setFitHeight(222);
            image.setFitWidth(150);
            movies[i].setGraphic(image);
            GridPane.setValignment(movies[i], VPos.CENTER);
            GridPane.setHalignment(movies[i], HPos.CENTER);
            Label movie = movies[i];
            movies[i].setOnMouseClicked(e-> {
                openDetails(movie);
            });
        }
        //moviesGrid.setPrefWidth(scrollPane.getWidth());
        //clear old data in grid
        moviesGrid.getChildren().clear();
        //moviesGrid.setAlignment(Pos.CENTER);
        //fill grid by movies
        moviesGrid.addRow(0, movies[0], movies[1]);
        moviesGrid.addRow(1,movies[4],movies[5]);
        moviesGrid.addRow(2, movies[6],movies[7]);
        //moviesGrid.minHeight(moviesGrid.getMinHeight() + 222);
        moviesGrid.setPrefWidth(browserPane.getWidth()-26);
        moviesGrid.setPrefHeight(moviesGrid.getHeight()+222);


    }

    public void returnMessage()
    {
        Alert alert = new Alert(AlertType.CONFIRMATION, "Are you sure you want to format your system?");
        alert.showAndWait().ifPresent(response -> {
            if (response == ButtonType.OK) {
            }
        });
    }

    public void openDetails(Label e)
    {
        if (detailsPane.getMinWidth() < 1) {
            //detailsInfoPane = new AnchorPane();
            //detailsPicPane.getChildren().clear();

            detailsPane.setMinWidth(300);

            my_movie= my_connector.getMovieInfo(1);

            //detailsContent.Text(my_movie.name() + "\n" + my_movie.year() + "\n" + my_movie.duration() + "\n" +  my_movie.genre() + "\n" + my_movie.description());
            Text info = new Text("Name:\n    " + my_movie.name() + "\n" +
                                    "Year: \n    " + my_movie.year() + "\n" +
                                    "Duration: \n    " + my_movie.duration() + "\n" +
                                    "Genre: \n    " + my_movie.genre() + "\n" +
                                    "Description: \n    " + my_movie.description());
            info.setStyle("-fx-font-size: 14; -fx-fill: #9f9f9f;");
            detailsContent.getChildren().clear();
            detailsContent.getChildren().add(info);

            detailsPicPane.getChildren().clear();
            //detailsPane.setMaxWidth(300);
            //Label movie = e;
            ImageView detailsImage = new ImageView();


            ImageView image = new ImageView((my_movie.cover()));
            image.setFitHeight(222);
            image.setFitWidth(150);

            detailsImage = ((ImageView) image);
            detailsImage.setX((detailsPane.getWidth() - detailsImage.getFitWidth()) / 2 + 100);
            detailsImage.setY(0);
            //movie.
            detailsPicPane.getChildren().add(detailsImage);
            // 300px is details window width
            scrollPane.setPrefWidth(browserPane.getWidth()-300);
            scrollPane.setPrefHeight(browserPane.getHeight());
            moviesGrid.setPrefWidth(browserPane.getWidth() -300);
            moviesGrid.setPrefHeight(browserPane.getHeight());
            //loadPosters();

        }
        else
        {
            detailsPicPane.getChildren().clear();
            detailsPane.setMinWidth(0);
            detailsPane.setPrefWidth(0);
            detailsPane.setMaxWidth(0);
            // 300px is details window width
            scrollPane.setPrefWidth(browserPane.getWidth()+300);
            scrollPane.setPrefHeight(browserPane.getHeight());
            moviesGrid.setPrefWidth(browserPane.getWidth()+300);
            moviesGrid.setPrefHeight(browserPane.getHeight());
        }
    }

    public void closeDetails()
    {
        detailsPicPane.getChildren().clear();
        detailsPane.setMinWidth(1);
        detailsPane.setPrefWidth(1);
    }

    public void openFilter()
    {
        if (filterPane.getWidth() < 1)
        {
            //filterPane.maxWidth(300);
            filterPane.setMinWidth(300);
            filterPane.setPrefWidth(300);
            filterPane.setMaxWidth(300);
            //loadPosters();
            // 300px is filter window width
            scrollPane.setPrefWidth(browserPane.getWidth()-300);
            scrollPane.setPrefHeight(browserPane.getHeight());
            moviesGrid.setPrefWidth(browserPane.getWidth() -300);
            moviesGrid.setPrefHeight(browserPane.getHeight());
        }
        else
        {
            filterPane.setMinWidth(0);
            filterPane.setPrefWidth(0);
            filterPane.setMaxWidth(0);
            //loadPosters();
            // 300px is filter window width
            scrollPane.setPrefWidth(browserPane.getWidth()+300);
            scrollPane.setPrefHeight(browserPane.getHeight());
            moviesGrid.setPrefWidth(browserPane.getWidth()+300);
            moviesGrid.setPrefHeight(browserPane.getHeight());
        }
    }


}
