package sklep.gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import sklep.logika.Sklep;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class Okno extends JFrame {
	private static final long serialVersionUID = 1L;
	private final Sklep sklep;
	private final JPanel contentPane;
	private final JTextField textField_Nazwa;
	private final JTextField textField_Liczba;
	private final ButtonGroup buttonGroup;
	private final JTextPane textPane;
	private final JButton btnPobierz;
	private final JRadioButton rdbtnWywietlOpis;
	private final JRadioButton rdbtnKup;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Sklep sklep = Sklep.przykladowySklep();
					Okno frame = new Okno(sklep);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Okno(Sklep aSklep) {
		this.sklep = aSklep;
		setTitle("Sklep");
		setName("Okno_sklep");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(92dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(88dlu;default)"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),}));

		buttonGroup = new ButtonGroup();
		rdbtnWywietlOpis = new JRadioButton("Opis");
		rdbtnWywietlOpis.setName("rdbtnWywietlOpis");
		rdbtnWywietlOpis.setSelected(true);
		rdbtnWywietlOpis.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				if(rdbtnWywietlOpis.isSelected()) {
					btnPobierz.setText("Pobierz");
					textField_Liczba.setEnabled(false);
				}
			}
		});
		
		buttonGroup.add(rdbtnWywietlOpis);
		
		contentPane.add(rdbtnWywietlOpis, "2, 2");
		
		rdbtnKup = new JRadioButton("Zakup");
		rdbtnKup.setName("rdbtnKup");

		rdbtnKup.setSelected(false);
		rdbtnKup.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				if(rdbtnKup.isSelected()) {
					textField_Liczba.setEnabled(true);
					btnPobierz.setText("Kup");
				}
			}
		});
		buttonGroup.add(rdbtnKup);
		contentPane.add(rdbtnKup, "4, 2");
		
		final JLabel lblNazwaTowaru = new JLabel("Nazwa towaru");
		contentPane.add(lblNazwaTowaru, "2, 4");
		
		textField_Nazwa = new JTextField();
		textField_Nazwa.setName("textField_Nazwa");
		contentPane.add(textField_Nazwa, "4, 4, fill, default");
		textField_Nazwa.setColumns(10);
		
		final JLabel lblSztuk = new JLabel("Sztuk");
		contentPane.add(lblSztuk, "2, 6");
		
		textField_Liczba = new JTextField();
		textField_Liczba.setName("textField_Liczba");
		contentPane.add(textField_Liczba, "4, 6, fill, default");
		textField_Liczba.setColumns(10);
		textField_Liczba.setEnabled(false);
		
		btnPobierz = new JButton("Pobierz");
		btnPobierz.setName("button");
		btnPobierz.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				if(rdbtnKup.isSelected()) {
					String nazwa = textField_Nazwa.getText();
					try {
						int ile = Integer.parseInt(textField_Liczba.getText());
						String wynik = sklep.zakup(nazwa, ile);
						textPane.setText(wynik);
					} catch (NumberFormatException e) {
						textPane.setText("Błędny format");
					}
				} else {
					String nazwa = textField_Nazwa.getText();
					String opis = sklep.opisTowaru(nazwa);
					textPane.setText(opis);
				}
			}
		});
		contentPane.add(btnPobierz, "2, 8, 3, 1, center, default");
		
		textPane = new JTextPane();
		textPane.setName("textPane");
		contentPane.add(textPane, "2, 10, 3, 1, fill, fill");
	}

	public void close() {
		this.setVisible(false);
		this.dispose();
	}
}
