/* Filename:    MUTunes.java
 * Author:      Professor Killian
 * Course:      CSCI 161
 * Date:        April 23, 2018
 * Assignment:  Lab 10
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class MUTunes {
	public static void main(String[] args) throws FileNotFoundException {
		Scanner sc = new Scanner(new File("Music.txt"));
		
		int numberOfSongs = sc.nextInt();
		sc.nextLine();
		
		String[] titles = new String[numberOfSongs];
		String[] artists = new String[numberOfSongs];
		int[] lengths = new int[numberOfSongs];
		
		for (int index = 0; index < numberOfSongs; ++index) {
			String line = sc.nextLine();
			String[] data = line.split(":");
			
			lengths[index] = Integer.parseInt(data[0]);
			titles[index] = data[1];
			artists[index] = data[2];
		}
		sc.close();
		
		printTable(titles, artists, lengths);
		System.out.println();

		int totalTime = getTotalTime(lengths);
		System.out.printf("%-48s%4d\n\n", "TOTAL TIME", totalTime);

		int longest = longestSongIndex(lengths);
		System.out.println("LONGEST SONG");
		System.out.println("------------");
		printSong(titles, artists, lengths, longest);
		System.out.println();

		int shortest = shortestSongIndex(lengths);
		System.out.println("SHORTEST SONG");
		System.out.println("-------------");
		printSong(titles, artists, lengths, shortest);
		System.out.println();
		
		String[] copyOfTitles = Arrays.copyOf(titles, numberOfSongs);
		Arrays.sort(copyOfTitles);
		System.out.println("SONGS");
		System.out.println("-----");
		for (int i = 0; i < numberOfSongs; ++i) {
			System.out.println(copyOfTitles[i]);
		}
	}

	/**
	 * Prints a table of songs
	 * @param param1 the song title array
	 * @param param2 the song artist array
	 * @param param3 the song length array
	 */
	public static void printTable(String[] titles, String[] artists, int[] lengths) {
		System.out.printf("%-24s%-24s%4s\n", "TITLE", "ARTIST", "TIME");
		System.out.printf("%-24s%-24s%4s\n", "-----", "------", "----");
		for (int song = 0; song < titles.length; ++song) {
			printSong(titles, artists, lengths, song);
		}
	}

	/**
	 * Prints a single song
	 * @param param1 the song title array
	 * @param param2 the song artist array
	 * @param param3 the song length array
	 * @param param3 the zero-based index of the song to print
	 */
	public static void printSong(String[] titles, String[] artists, int[] lengths, int index) {
		System.out.printf("%-24s%-24s%4d\n", titles[index], artists[index], lengths[index]);
	}

	/**
	 * Calculates the total time of all songs
	 * @param param1 the array of song lengths 
	 * @return the sum of the times for all songs passed
	 */
	public static int getTotalTime(int[] songLengths) {
		int total = 0;
		for (int i = 0; i < songLengths.length; ++i) {
			total += songLengths[i];
		}
		return total;
	}

	/**
	 * Finds the zero-based index of the longest song in the library 
	 * @param param1 the array of song lengths 
	 * @return the index of the song with the longest length
	 */
	public static int longestSongIndex(int[] songLengths) {
		int longest = 0;
		for (int i = 1; i < songLengths.length; ++i) {
			if (songLengths[i] > songLengths[longest]) {
				longest = i;
			}
		}
		return longest;
	}

	/**
	 * Finds the zero-based index of the shortest song in the library 
	 * @param param1 the array of song lengths 
	 * @return the index of the song with the shortest length
	 */
	public static int shortestSongIndex(int[] songLengths) {
		int shortest = 0;
		for (int i = 1; i < songLengths.length; ++i) {
			if (songLengths[i] < songLengths[shortest]) {
				shortest = i;
			}
		}
		return shortest;
	}
}