package edu.school.rabbits;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SpringLayout;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * Информатика, 8 клас, издателство Просвета, 2017
 * Автори: Светла Бойчева, Николина Николова, Елиза Стефанова, Антон Денев
 * 3.4.1.2. Цикъл, управляван от брояч
 * Урок: Цикъл, управляван от брояч – упражнение
 * Задача:Двойка зайци се размножава по следния начин:
 * през първата от раждането си една двойка
 * е твърде млада, за да има поколение, но от 
 * всяка следваща година нататък тя създава още по
 * една двойка зайци. Напишете програма, която пресмята колко
 * двойки зайци ще има през дадена година.
 * 
 * Вариант 1.
 */
public class RabbitsForm extends JFrame {

	private JPanel contentPane;
	private JTextField txtYears;

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

	/**
	 * Create the frame.
	 */
	public RabbitsForm() {
		setTitle("\u0417\u0430\u0439\u0446\u0438");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 328, 215);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(255, 255, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);

		JLabel lblYears = new JLabel("\u0413\u043E\u0434\u0438\u043D\u0438: ");
		lblYears.setForeground(new Color(102, 0, 0));
		lblYears.setFont(new Font("Tahoma", Font.PLAIN, 14));
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblYears, 26, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblYears, 28, SpringLayout.WEST, contentPane);
		contentPane.add(lblYears);

		txtYears = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, txtYears, 25, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, txtYears, 36, SpringLayout.EAST, lblYears);
		sl_contentPane.putConstraint(SpringLayout.EAST, txtYears, 193, SpringLayout.EAST, lblYears);
		txtYears.setForeground(new Color(102, 0, 0));
		txtYears.setFont(new Font("Tahoma", Font.PLAIN, 14));
		contentPane.add(txtYears);
		txtYears.setColumns(10);

		JLabel lblRabbitsCouples = new JLabel(
				"\u0411\u0440\u043E\u0439 \u0434\u0432\u043E\u0439\u043A\u0438 \u0437\u0430\u0439\u0446\u0438:");
		lblRabbitsCouples.setForeground(new Color(102, 0, 0));
		lblRabbitsCouples.setFont(new Font("Tahoma", Font.BOLD, 14));
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblRabbitsCouples, 37, SpringLayout.SOUTH, lblYears);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblRabbitsCouples, 0, SpringLayout.WEST, lblYears);
		contentPane.add(lblRabbitsCouples);

		JButton btnCalculate = new JButton("\u0418\u0437\u0447\u0438\u0441\u043B\u0438");
		btnCalculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int years = Integer.parseInt(txtYears.getText());

				int a = 1;
				int b = 1;
				int result = 1; //Преди втората година има една двойка

				//От втората година нататък
				for (int year = 2; year <= years; year++) {
					result = a + b;
					a = b;
					b = result;
				}

				lblRabbitsCouples.setText("Брой двойки зайци: " + result);

			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, btnCalculate, 37, SpringLayout.SOUTH, lblRabbitsCouples);
		sl_contentPane.putConstraint(SpringLayout.WEST, btnCalculate, 0, SpringLayout.WEST, lblYears);
		contentPane.add(btnCalculate);

		JButton btnReset = new JButton("\u0418\u0437\u0447\u0438\u0441\u0442\u0438");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtYears.setText("");
				lblRabbitsCouples.setText("Брой двойки зайци: ");
			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, btnReset, 0, SpringLayout.NORTH, btnCalculate);
		sl_contentPane.putConstraint(SpringLayout.WEST, btnReset, 0, SpringLayout.WEST, txtYears);
		contentPane.add(btnReset);

		JButton btnExit = new JButton("\u0418\u0437\u0445\u043E\u0434");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, btnExit, 0, SpringLayout.NORTH, btnCalculate);
		sl_contentPane.putConstraint(SpringLayout.EAST, btnExit, 0, SpringLayout.EAST, txtYears);
		contentPane.add(btnExit);
	}

}
