package model;

import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * Created by Gondr on 1/06/2017.
 */
public class User {
    protected int studentNumber;
    protected String email;
    protected String password;
    protected String address;
    protected String homephone;
    protected String mobilephone;
    protected AccountType accountType;
    transient protected Image photo;
    private byte[] image;

    public enum AccountType{
        PASSENGER("Passenger"), DRIVER("Driver");
        public String name;
        AccountType(String name){
            this.name = name;
        }
        public static AccountType getValueOf(String s){
            switch(s){
                case "Passenger":
                    return PASSENGER;
                case "Driver":
                    return DRIVER;
                default:
                    return null;
            }
        }
    }

    public User(int studentNumber, String email, String password, String address, String homephone, String mobilephone,
                Image photo){
        this.studentNumber = studentNumber;
        this.email = email;
        this.password = password;
        this.address = address;
        this.homephone = homephone;
        this.mobilephone = mobilephone;
        this.photo = photo;
        this.accountType = AccountType.PASSENGER;

    }

    public void serialise(){
        try {
            BufferedImage bi = SwingFXUtils.fromFXImage(photo, null);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(bi, "png", out);
            out.flush();
            image = out.toByteArray();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void deserialise(){
        ByteArrayInputStream in = new ByteArrayInputStream(image);
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        photo = SwingFXUtils.toFXImage(bi, null);
    }
}
