package controllers;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import model.SceneCode;
import model.User;
import utils.DataValidator;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.regex.Pattern;

/**
 * Created by Gondr on 1/06/2017.
 */
public class RegisterController extends Controller {
    @FXML
    private PasswordField password;
    @FXML
    private TextField address;
    @FXML
    private TextField mobilePhone;
    @FXML
    private PasswordField passwordConfirm;
    @FXML
    private TextField homePhone;
    @FXML
    private ComboBox<String> accountType;
    @FXML
    private TextField email;
    @FXML
    private TextField username;

    public String validateStudentNumber(String studentNumber){
        String failure = "";
        //student number
        if (studentNumber.equals("") || studentNumber == null){
            failure += "Your must enter your Student Number.\n";
        }
        try{
            Integer.parseInt(studentNumber);
        } catch (NumberFormatException e){
            failure += "Your Student Number must be a number.\n";
        }
        return failure;
    }

    public static String validateEmail(String email){
        String failure = "";
        Pattern uclivePattern = Pattern.compile(".*@uclive.ac.nz", Pattern.CASE_INSENSITIVE);
        Pattern canterburyPattern = Pattern.compile(".*@canterbury.ac.nz", Pattern.CASE_INSENSITIVE);
        if (!uclivePattern.matcher(email).matches() && !canterburyPattern.matcher(email).matches()){
            failure += "Your email must end in @uclive.ac.nz or @canterbury.ac.nz.\n";
        }
        return failure;
    }

    public static String validatePassword(String p1, String p2){
        String failure = "";
        if (p1.length() < 6){
            failure += "Your password must be at least 6 letters long.\n";
        }
        if (!p1.equals(p2)){
            failure += "Your passwords entered do not match.\n";
        }
        return failure;
    }

    public static String validateAddress(String address){
        String failure = "";
        if (!DataValidator.addressValidator(address)){
            failure += "Your address must be similar to 1 University Drive or 1/1 University Drive or 1A/2 University Drive or 1A/2A University Drive.\n";
        }
        return failure;
    }

    public static String validatePhone(String hp, String mp){
        String failure = "";
        if ((hp.equals("") || hp == null) && (mp.equals("")||mp == null)){
            failure += "You must enter either a home phone number or a mobile phone number.\n";
        }
        return failure;
    }

    public boolean validate(){
        String failure = "";
        failure += validateStudentNumber(username.getText());
        failure += validateEmail(email.getText());
        failure += validatePassword(password.getText(), passwordConfirm.getText());
        failure += validateAddress(address.getText());
        failure += validatePhone(homePhone.getText(), mobilePhone.getText());
        if (failure.equals("")){
            return true;
        }else{
            popUp(Alert.AlertType.WARNING, "Error!", "Your details are not valid for the following reasons.", failure);
            return false;
        }
    }

    public void uploadPhoto(){
        if (validate()){
            try {
                UploadPhotoController c = (UploadPhotoController) changeScene(SceneCode.ADD_PHOTO);
                c.setUserInformation(Integer.parseInt(username.getText()), email.getText(), password.getText(), address.getText(),
                        homePhone.getText(), mobilePhone.getText(), accountType.getValue());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ObservableList<String> accountTypes = FXCollections.observableArrayList();
        accountTypes.addAll(User.AccountType.DRIVER.name, User.AccountType.PASSENGER.name);
        accountType.setItems(accountTypes);
        accountType.getSelectionModel().select(0);

        //default values just for testing
        username.setText("79984862");
        email.setText("fwy13@uclive.ac.nz");
        password.setText("123456");
        passwordConfirm.setText("123456");
        address.setText("90 Stables Street");
        homePhone.setText("033466057");
        mobilePhone.setText("+642726242114");
    }
}
