/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package de.nplusc.izc.izstreamer;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 *
 * @author LH
 */
public class Player extends javax.swing.JFrame implements PlayerStructure
{

    /**
     * Creates new form Player
     */
    public Player()
    {
        initComponents();
    }
    //public static final String StreamTitle = "";
    private static final StreamGUI playmanager = new StreamGUI();
    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        sliderVolume = new javax.swing.JSlider();
        lblSender = new javax.swing.JLabel();
        lblTIT = new javax.swing.JLabel();
        btnPlay = new javax.swing.JButton();
        Info = new javax.swing.JButton();
        btnLastPlayed = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        sliderVolume.setMajorTickSpacing(10);
        sliderVolume.setValue(100);
        sliderVolume.addMouseMotionListener(new java.awt.event.MouseMotionAdapter()
        {
            public void mouseDragged(java.awt.event.MouseEvent evt)
            {
                sliderVolumeMouseDragged(evt);
            }
        });

        lblSender.setText("Sender");

        lblTIT.setText("Now Playing:");

        btnPlay.setText("PLAY");
        btnPlay.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnPlayActionPerformed(evt);
            }
        });

        Info.setText("Info");
        Info.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                InfoActionPerformed(evt);
            }
        });

        btnLastPlayed.setText("Last 20  Tracks");
        btnLastPlayed.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnLastPlayedActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblSender, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(sliderVolume, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblTIT, javax.swing.GroupLayout.PREFERRED_SIZE, 397, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 8, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnPlay, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(Info, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnLastPlayed, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblSender)
                    .addComponent(sliderVolume, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(lblTIT)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Info)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnLastPlayed)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(btnPlay, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public void updateSender(String s)
    {
        lblSender.setText(s);
    }
    
    private boolean isPlaying = false;
    private void btnPlayActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnPlayActionPerformed
    {//GEN-HEADEREND:event_btnPlayActionPerformed
        if(isPlaying)
        {
            playmanager.stopPlay=true;
            isPlaying=false;
            btnPlay.setText("PLAY");
        }
        else
        {
            
            btnPlay.setText("STOP");
            lblSender.setText("Connecting....");
                new Thread(()->
                playmanager.play("http://31.7.177.108:2294",this)).start();
                //http://31.7.177.108:2299/played.html
            isPlaying=true;
        }
    }//GEN-LAST:event_btnPlayActionPerformed

    private void sliderVolumeMouseDragged(java.awt.event.MouseEvent evt)//GEN-FIRST:event_sliderVolumeMouseDragged
    {//GEN-HEADEREND:event_sliderVolumeMouseDragged
        playmanager.m.setVolume(sliderVolume.getValue());
    }//GEN-LAST:event_sliderVolumeMouseDragged

    private void InfoActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_InfoActionPerformed
    {//GEN-HEADEREND:event_InfoActionPerformed
        
    }//GEN-LAST:event_InfoActionPerformed

    private void btnLastPlayedActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnLastPlayedActionPerformed
    {//GEN-HEADEREND:event_btnLastPlayedActionPerformed
        try
        {
            Desktop.getDesktop().browse(new URI("http://31.7.177.108:2299/played.html"));
        }
        catch (URISyntaxException ex)
        {
            ex.printStackTrace();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnLastPlayedActionPerformed
    
    public void setPlaying(String tit)
    {
        lblTIT.setText("Now playing: "+tit);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        

        
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
            {
                if ("Nimbus".equals(info.getName()))
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        }
        catch (ClassNotFoundException ex)
        {
            java.util.logging.Logger.getLogger(Player.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (InstantiationException ex)
        {
            java.util.logging.Logger.getLogger(Player.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(Player.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(Player.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new Player().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Info;
    private javax.swing.JButton btnLastPlayed;
    private javax.swing.JButton btnPlay;
    private javax.swing.JLabel lblSender;
    private javax.swing.JLabel lblTIT;
    private javax.swing.JSlider sliderVolume;
    // End of variables declaration//GEN-END:variables
}
