package org.academiadecodigo.bootcamp.fxlogin.model;

import org.academiadecodigo.bootcamp.fxlogin.utils.DateUtils;

import javax.persistence.*;
import java.text.ParseException;
import java.util.Date;
import java.util.Objects;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

@Entity
@Table(name = "codecadets")
public class Cadet extends AbstractModel {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @OneToOne
    private User user;

    private Gender gender;
    private String address;
    private String city;
    private String phonenumber;

    @ManyToOne()
    private Bootcamp bootcamp;

    private Date birthdate;

    public Cadet() {
    }

    public Cadet(User user) {
        this.user = user;
        this.gender = Gender.MALE;
        this.address = "Rua Aires Oliveira, 18";
        this.city = "City of Angels";
        this.phonenumber = "912345678";
        setBirthdate("1987/10/17");
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setBootcamp(Bootcamp bootcamp) {
        this.bootcamp = bootcamp;
    }

    public String getName() {
        return user.getUsername();
    }

    public Gender getGender() {
        return gender;
    }

    public String getAddress() {
        return address;
    }

    public String getCity() {
        return city;
    }

    public String getPhone() {
        return phonenumber;
    }

    public String getBirthdateString() {
        return DateUtils.dateFormat.format(birthdate);
    }

    public Date getBirthdate() {
        return birthdate;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public void setPhone(String phone) {
        this.phonenumber = phone;
    }

    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }

    private void setBirthdate(String birthdate) {
        try {
            this.birthdate = DateUtils.dateFormat.parse(birthdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getPhonenumber() {
        return phonenumber;
    }

    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }

    public Bootcamp getBootcamp() {
        return bootcamp;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Cadet cadet = (Cadet) o;
        return Objects.equals(user, cadet.user);
    }

    @Override
    public int hashCode() {
        return Objects.hash(user);
    }

    @Override
    public String toString() {
        return "Cadet{" +
                "name=" + user.getUsername() + ", " +
                "gender=" + gender + ", " +
                "address=" + address + ", " +
                "city=" + city + ", " +
                "phone=" + phonenumber + ", " +
                "birthdate=" + birthdate +
                "}";
    }

    public enum Gender {
        MALE("Male"),
        FEMALE("Female"),
        OTHER("Other");

        private final String name;

        Gender(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @Override
        public String toString() {
            return name;
        }
    }

}
