import java.awt.*;
import java.awt.event.*;

public class PlayListManager extends Frame {

    // Instance variables

    private PlayList model;  // main PlayList
    private PlayList newPlayList;

    private String outFile;
  
    private List mainList;
    private List newList;

    public PlayListManager( String in, String out ) {
    
        super( "Play List Manager" );
    
        // Taking of the data structures that will be storing our data

        try {
            this.model = PlayList.getSongsFromFile( in );
        } catch ( java.io.IOException e ) {
            System.err.println( e.getMessage() );
            System.exit( 1 );
        }

        newPlayList = new PlayList();  // we'll be adding Songs to this PlayList

        outFile = out;

        // Creating a Panel to visualize the two PlayLists

        Panel listsPanel = new Panel();
        listsPanel.setLayout( new FlowLayout() );
        
        mainList = new List( 10 ); // 10 rows
        mainList.setBackground( Color.WHITE );

        newList = new List( 10 );
        newList.setBackground( Color.WHITE );

        listsPanel.add( mainList );

        //  ------------------------------------------------------

        Button copyButton = new Button( "Copy >>" );

        copyButton.addActionListener( new ActionListener () {  // Listener is an inner class
                public void actionPerformed( ActionEvent e ) { // see supplemental notes on the web.

                    int pos = mainList.getSelectedIndex(); // determining which entry has been selected

                    if ( pos == -1 ) {

                        System.err.println( "Select a song from the list" );

                    } else {

                        newPlayList.addSong( model.getSongAt( pos ) ); // copy the Song
                        displaySongs(); // refresh the view
                    }
                }
            });

        listsPanel.add( copyButton );

        //  ------------------------------------------------------

        listsPanel.add( newList );
    
        add( listsPanel, BorderLayout.CENTER );

        //  ------------------------------------------------------

        //  Creating a Panel to hold the buttons

        Panel controlPanel = new Panel();

        //  ------------------------------------------------------

        Button sortByNameButton = new Button( "Sort By Name" );

        sortByNameButton.addActionListener( new ActionListener () {
                public void actionPerformed( ActionEvent e ) {

                    SortByName c = new SortByName();  // Comparator

                    model.sort( c );
                    newPlayList.sort( c );

                    displaySongs();
                }
            });

        controlPanel.add( sortByNameButton );

        //  ------------------------------------------------------

        Button sortByArtistButton = new Button( "Sort By Artist" );

        sortByArtistButton.addActionListener( new ActionListener () {
                public void actionPerformed( ActionEvent e ) {

                    SortByArtist c = new SortByArtist();

                    model.sort( c );
                    newPlayList.sort( c );

                    displaySongs();
                }
            });

        controlPanel.add( sortByArtistButton );

        //  ------------------------------------------------------

        Button sortByAlbumButton = new Button( "Sort By Album" );

        sortByAlbumButton.addActionListener( new ActionListener () {
                public void actionPerformed( ActionEvent e ) {

                    SortByAlbum c = new SortByAlbum();

                    model.sort( c );
                    newPlayList.sort( c );

                    displaySongs();
                }
            });

        controlPanel.add( sortByAlbumButton );

        //  ------------------------------------------------------

        Button printButton = new Button( "Print" );

        printButton.addActionListener( new ActionListener () {
                public void actionPerformed( ActionEvent e ) {

                    for ( int i=0; i<newPlayList.getSize(); i++ )
                        System.out.println( newPlayList.getSongAt( i ) );
                    
                }
            });

        controlPanel.add( printButton );

        //  ------------------------------------------------------

        Button writeButton = new Button( "Write" );

        writeButton.addActionListener( new ActionListener () {
                public void actionPerformed( ActionEvent event ) {
                    try {
                        newPlayList.writeSongsToFile( outFile );
                    } catch ( java.io.IOException e ) {
                        System.err.println( e.getMessage() );
                        System.exit( 1 );
                    }
                }
            });

        controlPanel.add( writeButton );

        //  ------------------------------------------------------

        add( controlPanel, BorderLayout.SOUTH );

        //  ------------------------------------------------------

        addWindowListener( new WindowAdapter() {
                public void windowClosing( WindowEvent e ) {
                    System.exit( 0 );
                }
            });

        //  ------------------------------------------------------

        displaySongs();
                        
        pack();
        show();
    }

    // Adds all the Songs from model to viewList, preserves the
    // order of the elements.

    private static void displaySongs( PlayList model, List viewList ) {
        viewList.removeAll();

        for ( int i=0; i<model.getSize(); i++ ) {
            viewList.add( model.getSongAt( i ).toString() );
            viewList.makeVisible( i );
        }
    }

    // Used by the listeners to refresh the views

    public void displaySongs() {
        displaySongs( model, mainList );
        displaySongs( newPlayList, newList );
    }
}
