package ch.usi.inf.ajp22;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class Album {

    private List<Track> tracks;
    private Artist artist;
    private String title;

    private static Comparator<Track> byDuration;

    public Album(List<Track> songs, Artist artist, String title) {
        this.tracks = songs;
        this.artist = artist;
        this.title = title;
    }

    public List<Track> getTracks() {
        return tracks;
    }

    public void setTracks(List<Track> tracks) {
        this.tracks = tracks;
    }

    public Artist getArtist() {
        return artist;
    }

    public void setArtist(Artist artist) {
        this.artist = artist;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * 3 Points
     *      create a public Optional<Track> method called "searchLongestSong" that
     *       return the longest Track on the album.
     *       input: void
     *       output: an Optional<Track>
     */
    public Optional<Track> searchLongestSong() {
        return this.tracks.stream()
                .max(Comparator.comparingInt(Track::getLength));
    }

    /**
     * 3 Points
     *      create a public List<Track> method called "orderSongByTitle" that
     *       return the track list ordered lexicographically using the title field.
     *       input: void
     *       output: a List<Track>
     */
    public List<Track> orderSongByTitle() {
        // note: we assume lexicographical order requires letter case to be ignored
        return this.tracks.stream()
                .sorted((a, b) -> String.CASE_INSENSITIVE_ORDER.compare(a.getTitle(), b.getTitle()))
                .collect(Collectors.toList());
    }


    @Override
    public String toString() {
        return "ch.usi.inf.ajp22.Album{" +
                "tracks=" + tracks +
                ", artist='" + artist.getName() + '\'' +
                ", title='" + title + '\'' +
                '}';
    }
}
