package com.moviesdb;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;

import java.io.IOException;

//For reading properties from .properties file

public class AddMovieController extends Controller {

    @FXML
    Button addActorButton;

    @FXML
    ScrollPane actorsGrid;
    @FXML
    VBox actorsGridAncor;

    @FXML
            TextField urlTextBox;
    @FXML
            TextField titleTextBox;
    @FXML
            TextField genreTextBox;
    @FXML
    TextArea descriptionTextBox;
    @FXML
            TextField yearTextBox;
    @FXML
            TextField timeTextBox;
    @FXML
            TextField studioTextBox;
    @FXML
            TextField directorTextBox;
    @FXML
            TextField rateTextBox;

    Object caller;

    public void open(Object caller) {
        prepare();
        stage.initModality(Modality.WINDOW_MODAL);
        stage.initOwner(((BrowserController)caller).stage.getScene().getWindow());
        this.caller = caller;
        stage.setTitle("Log in");
        stage.show();
        descriptionTextBox.setWrapText(true);
        addActor();
    }

    public void addActor(){
        try {
            AnchorPane row = FXMLLoader.load(getClass().getResource("../../ActorRoleSegment.fxml"));
            //row.setPrefWidth(actorsGrid.getWidth());
            actorsGridAncor.getChildren().add(row);
            actorsGrid.setMinHeight(actorsGrid.getMinHeight() + 35);
            //actorsGrid.getChildren().add(row);
        }
        catch (IOException ex)
        {

        }
        //System.out.println("LoginForm closed");
    }

    public void addMovie(){
        //read all textboxes
        Movie NewMovie = new Movie();
        NewMovie.setCover(urlTextBox.getText());
        NewMovie.setName(titleTextBox.getText());
        NewMovie.setGenre(genreTextBox.getText());
        NewMovie.setYear(yearTextBox.getText());
        NewMovie.setDuration(timeTextBox.getText());
        NewMovie.setStudio(studioTextBox.getText());
        NewMovie.setDirector(directorTextBox.getText());
        NewMovie.setRating(rateTextBox.getText());
        NewMovie.setDescription(descriptionTextBox.getText());

        try{
            //AnchorPane row = (AnchorPane) actorsGrid.getChildren().get(1);
            /*
            for (Node row : actorsGrid.getChildren()) {
                System.out.println("Id: " + row.getId());
                if (row instanceof AnchorPane) {
                    // clear
                    for (Node node : ((AnchorPane)row).getChildren()) {
                        System.out.println("Id: " + node.getId());
                        if (node instanceof TextField) {
                            // clear
                            ((TextField)node).setText("I have access");
                        }
                    }
                }
            }
            */
        }
        catch (Exception ex)
        {

        }
        ((BrowserController)caller).my_connector.addMovie(NewMovie);
    }
}
