package org.academiadecodigo.bootcamp.fxlogin.model;

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

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

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

@Entity
@Table(name = "bootcamps")
public class Bootcamp extends AbstractModel {
    @Id
    private Integer id;

    private Location location;
    private Date start;
    private Date end;

    @OneToMany(mappedBy = "bootcamp",
            fetch = FetchType.EAGER,
            orphanRemoval = true,
            cascade = CascadeType.ALL)
    private Set<Cadet> cadets;

    public Bootcamp() {
        cadets = new HashSet<>();
    }

    public Bootcamp(Integer id, Location location, String start, String end) {
        this.id = id;
        this.location = location;
        setStart(start);
        setEnd(end);
        this.cadets = new HashSet<>();
    }

    public boolean contains(Cadet cadet) {
        return cadets.contains(cadet);
    }

    public void addCodeCadet(Cadet cadet) {
        cadet.setBootcamp(this);
        cadets.add(cadet);
    }

    public Set<Cadet> getCadets() {
        return cadets;
    }

    public Integer getId() {
        return id;
    }

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    public String getStartString() {
        return start != null ? DateUtils.format(start) : "TBD";
    }

    public Date getStart() {
        return start;
    }

    public Date getEnd() {
        return end;
    }

    public void setStart(Date start) {
        this.start = start;
    }

    public void setEnd(Date end) {
        this.end = end;
    }

    public String getEndString() {
        return end != null ? DateUtils.format(end) : "TBD";
    }

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

    private void setStart(String start) {
        this.start = DateUtils.parse(start);
    }

    public void setCadets(Set<Cadet> cadets) {
        this.cadets = cadets;
    }

    private void setEnd(String end) {
        this.end = DateUtils.parse(end);
    }

    @Override
    public String toString() {
        return "{" + "Bootcamp #" + id + ", " +
                "Location: " + location + ", " +
                "Start: " + (start != null ? DateUtils.dateFormat.format(start) : "TBD") + ", " +
                "End: " + (end != null ? DateUtils.dateFormat.format(end) : "TBD") +
                "}";
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Bootcamp))
            return false;
        Bootcamp other = Bootcamp.class.cast(obj);
        return this.id.equals(other.getId());
    }

    public enum Location {
        LISBOA("Lisboa"),
        FUNDAO("Fundão"),
        TERCEIRA("Terceira");

        private final String name;

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

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