/*
 * Copyright (C) 2023 hobbyslotcar.com.br
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package br.com.hobbyslotcar.racedirector;

import br.com.ctecinf.jetty.Config;
import br.com.ctecinf.jetty.audio.Audio;
import br.com.ctecinf.jetty.audio.AudioException;

/**
 *
 * @author Cássio Conceição
 * @since 13/06/2023
 * @version 2306
 * @see http://hobbyslotcar.com.br
 */
public class Driver implements Config.ConfigObject {

    private String name;
    private long raceTime;
    private Long bestTime;
    private Long heatTime;
    private Integer heatLap;
    private int raceLap;
    private long startLap;
    private int sector;

    public Driver() {
    }

    public Driver(String name) {
        this.name = name;
    }

    public void startRace() {
        raceTime = 0;
        heatLap = -1;
        raceLap = -1;
        sector = 0;
        heatTime = null;
    }

    public void newHeat() {
        heatLap = -1;
        heatTime = null;
    }

    public void openLap() {
        startLap = System.currentTimeMillis();
        heatLap++;
        raceLap++;
    }

    public void closeLap() {
        heatTime = System.currentTimeMillis() - startLap;
        if (bestTime == null) {
            bestTime = heatTime;
        }
        if (heatTime < bestTime) {
            bestTime = heatTime;
            try {
                Audio.getInstance().play(Constants.BEST_LAP);
            } catch (AudioException ex) {
                System.err.println(ex);
            }
        } else {
            try {
                Audio.getInstance().play(Constants.BEEP);
            } catch (AudioException ex) {
                System.err.println(ex);
            }
        }
        raceTime += heatTime;
    }

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

    @Override
    public void setName(String name) {
        this.name = name;
    }

    public long getRaceTime() {
        return raceTime;
    }

    public void setRaceTime(long raceTime) {
        this.raceTime = raceTime;
    }

    public Long getBestTime() {
        return bestTime;
    }

    public void setBestLap(Long bestTime) {
        this.bestTime = bestTime;
    }

    public Long getHeatTime() {
        return heatTime;
    }

    public void setHeatTime(Long heatTime) {
        this.heatTime = heatTime;
    }

    public Integer getHeatLap() {
        return heatLap;
    }

    public void setHeatLap(Integer heatLap) {
        this.heatLap = heatLap;
    }

    public int getRaceLap() {
        return raceLap;
    }

    public void setRaceLap(int raceLap) {
        this.raceLap = raceLap;
    }

    public long getStartLap() {
        return startLap;
    }

    public void setStartLap(long startLap) {
        this.startLap = startLap;
    }

    public int getSector() {
        return sector;
    }

    public void setSector(int sector) {
        this.sector = sector;
    }

    @Override
    public boolean equals(Object obj) {
        return obj == null ? false : this.toString().equals(obj.toString());
    }

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