package ch.zhaw.ads;

import java.util.*;
import java.text.*;
import java.lang.Comparable;
import java.lang.Integer;
import java.lang.Long;

public class Competitor implements Comparable<Competitor> {
    private String name;
    private String country;
    private long time;
    private int jg;
    private int startNr;
    private int rank;

    public Competitor(int startNr, String name, int jg, String country, String time) throws ParseException {
        this.startNr = startNr;
        this.name = name;
        this.jg = jg;
        this.country = country;
        this.time = this.parseTime(time);
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public long getTime() {
        return time;
    }

    public String getName() {
        return name;
    }

    public int getJg() {
        return jg;
    }

    private static long parseTime(String s) throws ParseException {
        DateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");
        Date date = sdf.parse(s);
        return date.getTime();
    }

    public String toString() {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.S");
        StringBuilder sb = new StringBuilder();
        sb.append(rank);sb.append(" ");
        sb.append(name); sb.append(" ");
        sb.append(Integer.toString(jg)); sb.append(" ");
        sb.append(df.format(new Date(time)));
        return sb.toString();
    }

    @Override
    public int compareTo(Competitor other) {
        if (this.equals(other)) {
            return 0;
        }
        return Long.valueOf(this.getTime()).compareTo(Long.valueOf(other.getTime()));
    }

    public boolean equals(Competitor other) {
        return this.getName().equals(other.getName())
            && Integer.valueOf(this.getJg()).equals(Integer.valueOf(other.getJg()))
            && Long.valueOf(this.getTime()).equals(Long.valueOf(other.getTime()));
	}
}
