package controllers;

import javafx.embed.swing.SwingFXUtils;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.FileChooser;
import model.SceneCode;
import model.Session;
import model.User;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * Created by Gondr on 1/06/2017.
 */
public class UploadPhotoController extends Controller {

    private Image photoStored;
    private int studentNumber;
    private String email;
    private String password;
    private String address;
    private String homephone;
    private String mobilephone;
    private String accountType;
    @FXML
    private ImageView photo;

    public void setUserInformation(int studentNumber, String email, String password, String address, String homephone, String mobilephone, String accountType){
        this.studentNumber = studentNumber;
        this.email = email;
        this.password = password;
        this.address = address;
        this.homephone = homephone;
        this.mobilephone = mobilephone;
        this.accountType = accountType;
    }

    public void upload(){
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Select a Picture for you Profile.");
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"));
        File file = fileChooser.showOpenDialog(parent.getPrimaryStage());
        if (file != null){
            try {
                BufferedImage bufferedImage = ImageIO.read(file);
                photoStored = SwingFXUtils.toFXImage(bufferedImage, null);
                photo.setImage(photoStored);
            } catch (IOException e) {
            }
        }
    }

    public void next(){
        if (photoStored == null){
            popUp(Alert.AlertType.WARNING, "Error!", "Missing Photo.", "You must have a photo with your account.");
            return;
        }
        User.AccountType ac = User.AccountType.getValueOf(accountType);
        if (ac == User.AccountType.PASSENGER){
            User user = new User(studentNumber, email, password, address,homephone, mobilephone, photoStored);
            if (Session.session.getDataManager().addUser(user)){
                popUp(Alert.AlertType.CONFIRMATION, "Success!", "Your Account has been made.", "You can now log in and will be redirected the the title screen after.");
                try {
                    changeScene(SceneCode.MAIN);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else{
                popUp(Alert.AlertType.WARNING, "Error!", "There is already an passenger with this id.", "You will be redirected to the homescreen to log in.");
                try {
                    changeScene(SceneCode.MAIN);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }else{
            try {
                RegisterDriverController c = (RegisterDriverController) changeScene(SceneCode.REGISTER_DRIVER);
                c.setUserInformation(studentNumber, email, password, address,
                        homephone, mobilephone, accountType, photoStored);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}
