package zubil;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.EtchedBorder;

public class CounterPanel extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;
		Counter counter;
    JButton incButton;
    JButton decButton;
    JButton resetButton;
    JLabel valueLabel;
    JLabel timerLabel;
    JTextField setTextField;
    JTextField timerTextField;
    Timer timer;
    JLabel lowLabel;
    int time;
    
    public CounterPanel() {
    	counter = new Counter();
    	setSize(440,200);
    	setTitle("Counter Programm");
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	setLayout(new BorderLayout());
    	
    	JTabbedPane onglet= new JTabbedPane();
    	
    	JPanel counter_panel= new JPanel(new BorderLayout());
    	incButton = new JButton("increase");
    	decButton = new JButton("decrease");
    	resetButton = new JButton("reset");
    	incButton.addActionListener(this);
    	decButton.addActionListener(this);
    	resetButton.addActionListener(this);
    	incButton.setMnemonic(KeyEvent.getExtendedKeyCodeForChar('p'));
    	decButton.setMnemonic(KeyEvent.getExtendedKeyCodeForChar('m'));
    	resetButton.setMnemonic(KeyEvent.getExtendedKeyCodeForChar('o'));
    	JPanel buttonPanel = new JPanel();
    	buttonPanel.setLayout(new GridLayout(1,3));
    	buttonPanel.add(incButton);
    	buttonPanel.add(decButton);
    	buttonPanel.add(resetButton);
    	counter_panel.add(buttonPanel, BorderLayout.SOUTH);
    	
    	JPanel viewPanel = new JPanel();
    	viewPanel.setLayout(new FlowLayout());
    	viewPanel.add(new JLabel("counter is up to :"));
    	valueLabel= new JLabel(counter.toString());
    	valueLabel.setForeground(new Color(255,100,100));
    	valueLabel.setBorder(BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder( EtchedBorder.RAISED, Color.GRAY, Color.DARK_GRAY)));
    	viewPanel.add(valueLabel);
    	viewPanel.setBorder(BorderFactory.createTitledBorder("Counter Statment"));
    	counter_panel.add(viewPanel, BorderLayout.CENTER);
    	
    	JPanel setPanel = new JPanel();
    	setPanel.setLayout(new FlowLayout());
    	setPanel.add(new JLabel("set counter value :"));
    	setTextField= new JTextField();
    	setTextField.setPreferredSize(new Dimension(40, 20));
    	setTextField.addActionListener(this);
    	setPanel.add(setTextField); 	
    	counter_panel.add(setPanel, BorderLayout.NORTH);
    	
    	onglet.add(counter_panel, "counter");
    	add(onglet, BorderLayout.CENTER);
    	
    	JPanel lowPanel = new JPanel();
    	lowPanel.setLayout(new FlowLayout());
    	lowLabel= new JLabel("online since 0 sec");
    	lowLabel.setFont(new Font ("Serif",Font.BOLD | Font.ITALIC, 10));
    	time= 0;
    	lowPanel.add(lowLabel);
    	add(lowPanel, BorderLayout.SOUTH);
    	
    	JPanel chrono_panel= new JPanel(new FlowLayout());
    	chrono_panel.add(new JLabel("set :"));
    	timerTextField= new JTextField("60");
    	timerTextField.setPreferredSize(new Dimension(40, 20));
    	timerTextField.addActionListener(this);
    	chrono_panel.add(timerTextField);
    	timerLabel= new JLabel("60");
    	chrono_panel.add(timerLabel);
    	onglet.add(chrono_panel, "chrono");
    	
    	timer= new Timer(1000, this);
    	timer.start();
    	
    	
    	setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(incButton) ) counter.incC();
    else if (e.getSource().equals(decButton)) counter.decC();
    else if (e.getSource().equals(resetButton)) counter.setC(0);
    else if (e.getSource().equals(setTextField)){
    	try {counter.setC(Integer.parseInt(setTextField.getText()));
  		} catch (NumberFormatException er){System.out.println("Not a number\n");}
    }
    else if (e.getSource().equals(timerTextField)){
    	try {timerLabel.setText(""+Integer.parseInt(timerTextField.getText()));
  		} catch (NumberFormatException er){System.out.println("Not a number\n");}
    }
    else if (e.getSource().equals(timer)){
    	try {
    		int tmp= Integer.parseInt(timerLabel.getText());
    		time++;
    		lowLabel.setText("online since "+time+" sec");
    		tmp--;
    		if(tmp > 0) timerLabel.setText(""+tmp);
    		else System.exit(0);
  		} catch (NumberFormatException er){System.out.println("Not a number\n");}
    }
    valueLabel.setText(""+counter.getC());
  }
    
}