package edu.school.roses;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.SpringLayout;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Информатика, 8 клас, издателство Просвета, 2017
 * Автори: Светла Бойчева, Николина Николова, Елиза Стефанова, Антон Денев
 * Урок 3.4.1.1. Цикъл, управляван от условие - упражнение
 * Задача: За 8 март RxC00l купил бели и червени рози.
 * Той решил да направи колкото се може повече
 * еднакви букета от тях, за да зарадва колкото
 * се може повече свои съученички.
 * Помогнете на RxC00l да разбере колко еднакви
 * (с по еднакъв брой бели и червени рози)
 * букета може да направи.
 * Създайте приложение Roses, което позволява
 * въвеждането на общия брой бели рози и общия
 * брой червени рози. Приложението трябва да
 * извежда информация за максималния
 * брой еднакви букети.
 * 
 * Версия 1.
 */

public class RosesForm extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel pnlContent;
	private JTextField txtWhiteRoses;
	private JTextField txtRedRoses;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RosesForm frame = new RosesForm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public RosesForm() {
		setTitle("\u0411\u0443\u043A\u0435\u0442\u0438");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 244);
		pnlContent = new JPanel();
		pnlContent.setBackground(new Color(255, 255, 255));
		pnlContent.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(pnlContent);
		SpringLayout sl_pnlContent = new SpringLayout();
		pnlContent.setLayout(sl_pnlContent);
		
		JLabel lblWhiteRoses = new JLabel("\u0411\u0440\u043E\u0439 \u0431\u0435\u043B\u0438 \u0440\u043E\u0437\u0438:");
		lblWhiteRoses.setFont(new Font("Tahoma", Font.PLAIN, 14));
		sl_pnlContent.putConstraint(SpringLayout.NORTH, lblWhiteRoses, 10, SpringLayout.NORTH, pnlContent);
		sl_pnlContent.putConstraint(SpringLayout.WEST, lblWhiteRoses, 10, SpringLayout.WEST, pnlContent);
		pnlContent.add(lblWhiteRoses);
		
		txtWhiteRoses = new JTextField();
		txtWhiteRoses.setFont(new Font("Tahoma", Font.PLAIN, 14));
		sl_pnlContent.putConstraint(SpringLayout.NORTH, txtWhiteRoses, 0, SpringLayout.NORTH, lblWhiteRoses);
		sl_pnlContent.putConstraint(SpringLayout.WEST, txtWhiteRoses, 31, SpringLayout.EAST, lblWhiteRoses);
		pnlContent.add(txtWhiteRoses);
		txtWhiteRoses.setColumns(10);
		
		JLabel lblRedRoses = new JLabel("\u0411\u0440\u043E\u0439 \u0447\u0435\u0440\u0432\u0435\u043D\u0438 \u0440\u043E\u0437\u0438:");
		lblRedRoses.setFont(new Font("Tahoma", Font.PLAIN, 14));
		sl_pnlContent.putConstraint(SpringLayout.NORTH, lblRedRoses, 20, SpringLayout.SOUTH, txtWhiteRoses);
		sl_pnlContent.putConstraint(SpringLayout.WEST, lblRedRoses, 0, SpringLayout.WEST, lblWhiteRoses);
		pnlContent.add(lblRedRoses);
		
		txtRedRoses = new JTextField();
		txtRedRoses.setFont(new Font("Tahoma", Font.PLAIN, 14));
		sl_pnlContent.putConstraint(SpringLayout.NORTH, txtRedRoses, -3, SpringLayout.NORTH, lblRedRoses);
		sl_pnlContent.putConstraint(SpringLayout.EAST, txtRedRoses, 0, SpringLayout.EAST, txtWhiteRoses);
		txtRedRoses.setColumns(10);
		pnlContent.add(txtRedRoses);
		
		JLabel lblMaxBouquets = new JLabel("\u041C\u0430\u043A\u0441\u0438\u043C\u0430\u043B\u0435\u043D \u0431\u0440\u043E\u0439 \u0431\u0443\u043A\u0435\u0442\u0438: ");
		lblMaxBouquets.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblMaxBouquets.setForeground(new Color(153, 0, 0));
		sl_pnlContent.putConstraint(SpringLayout.NORTH, lblMaxBouquets, 45, SpringLayout.SOUTH, lblRedRoses);
		sl_pnlContent.putConstraint(SpringLayout.WEST, lblMaxBouquets, 0, SpringLayout.WEST, lblWhiteRoses);
		pnlContent.add(lblMaxBouquets);
		
		JLabel lblRosesImage = new JLabel("");
		sl_pnlContent.putConstraint(SpringLayout.SOUTH, lblRosesImage, 0, SpringLayout.SOUTH, lblMaxBouquets);
		sl_pnlContent.putConstraint(SpringLayout.EAST, lblRosesImage, -10, SpringLayout.EAST, pnlContent);
		lblRosesImage.setIcon(new ImageIcon(RosesForm.class.getResource("/edu/school/roses/images/roses-3.jpg")));
		pnlContent.add(lblRosesImage);
		
		JButton btnExit = new JButton("\u0418\u0437\u0445\u043E\u0434");
		sl_pnlContent.putConstraint(SpringLayout.NORTH, btnExit, 34, SpringLayout.SOUTH, lblRosesImage);
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
			
		});
		sl_pnlContent.putConstraint(SpringLayout.EAST, btnExit, -10, SpringLayout.EAST, pnlContent);
		pnlContent.add(btnExit);
		
		JButton btnReset = new JButton("\u0418\u0437\u0447\u0438\u0441\u0442\u0438");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtWhiteRoses.setText("");
				txtRedRoses.setText("");
				lblMaxBouquets.setText("Максимален брой букети: ");
			}
		});
		sl_pnlContent.putConstraint(SpringLayout.NORTH, btnReset, 0, SpringLayout.NORTH, btnExit);
		sl_pnlContent.putConstraint(SpringLayout.EAST, btnReset, -6, SpringLayout.WEST, btnExit);
		pnlContent.add(btnReset);
		
		JButton btnCalculate = new JButton("\u041F\u0440\u0435\u0441\u043C\u0435\u0442\u043D\u0438");
		btnCalculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int whiteRoses = Integer.parseInt(txtWhiteRoses.getText());
				int redRoses = Integer.parseInt(txtRedRoses.getText());
				
				int x = whiteRoses;
				int y = redRoses;
				
				while (x != y){
					if (x > y){
						x -= y;
					}
					else{
						y -= x;
					}
				}
				
				lblMaxBouquets.setText("Максимален брой букети: " + x);
			}
		});
		sl_pnlContent.putConstraint(SpringLayout.SOUTH, btnCalculate, 0, SpringLayout.SOUTH, btnExit);
		sl_pnlContent.putConstraint(SpringLayout.EAST, btnCalculate, -6, SpringLayout.WEST, btnReset);
		pnlContent.add(btnCalculate);
	}
}
