package edu.school.enterpreneur;

import java.awt.EventQueue;

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


	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtMonths;
	private JTextField txtInitialCapital;

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

	/**
	 * Create the frame.
	 */
	public EnterpreneurForm() {
		setTitle("\u041F\u0440\u0435\u0434\u043F\u0440\u0438\u0435\u043C\u0430\u0447");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 336, 298);
		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 lblMonths = new JLabel("\u0411\u0440\u043E\u0439 \u043C\u0435\u0441\u0435\u0446\u0438:");
		lblMonths.setForeground(new Color(0, 102, 102));
		lblMonths.setFont(new Font("Tahoma", Font.PLAIN, 12));
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblMonths, 10, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblMonths, 10, SpringLayout.WEST, contentPane);
		contentPane.add(lblMonths);

		txtMonths = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, txtMonths, 8, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, txtMonths, 58, SpringLayout.EAST, lblMonths);
		txtMonths.setFont(new Font("Tahoma", Font.PLAIN, 12));
		contentPane.add(txtMonths);
		txtMonths.setColumns(10);

		JLabel lblInitialCapital = new JLabel(
				"\u041D\u0430\u0447\u0430\u043B\u043D\u0430 \u0441\u0443\u043C\u0430 (\u043B\u0432.)");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblInitialCapital, 17, SpringLayout.SOUTH, lblMonths);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblInitialCapital, 0, SpringLayout.WEST, lblMonths);
		lblInitialCapital.setForeground(new Color(0, 102, 102));
		lblInitialCapital.setFont(new Font("Tahoma", Font.PLAIN, 12));
		contentPane.add(lblInitialCapital);

		txtInitialCapital = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.WEST, txtInitialCapital, 28, SpringLayout.EAST, lblInitialCapital);
		sl_contentPane.putConstraint(SpringLayout.EAST, txtInitialCapital, -15, SpringLayout.EAST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, txtMonths, -12, SpringLayout.NORTH, txtInitialCapital);
		sl_contentPane.putConstraint(SpringLayout.EAST, txtMonths, 0, SpringLayout.EAST, txtInitialCapital);
		sl_contentPane.putConstraint(SpringLayout.NORTH, txtInitialCapital, -3, SpringLayout.NORTH, lblInitialCapital);
		txtInitialCapital.setFont(new Font("Tahoma", Font.PLAIN, 12));
		txtInitialCapital.setColumns(10);
		contentPane.add(txtInitialCapital);

		JLabel lblResult = new JLabel("\u0420\u0435\u0437\u0443\u043B\u0442\u0430\u0442:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblResult, 28, SpringLayout.SOUTH, lblInitialCapital);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblResult, 10, SpringLayout.WEST, contentPane);
		lblResult.setForeground(new Color(0, 102, 102));
		lblResult.setFont(new Font("Tahoma", Font.BOLD, 12));
		contentPane.add(lblResult);

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

		JTextArea txaResult = new JTextArea();
		// contentPane.add(txaResult);
		JScrollPane scrollPane = new JScrollPane(txaResult);
		sl_contentPane.putConstraint(SpringLayout.NORTH, scrollPane, 6, SpringLayout.SOUTH, lblResult);
		sl_contentPane.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, lblMonths);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, scrollPane, -6, SpringLayout.NORTH, btnExit);
		sl_contentPane.putConstraint(SpringLayout.EAST, scrollPane, 0, SpringLayout.EAST, txtMonths);
		contentPane.add(scrollPane);
		btnExit.setForeground(new Color(0, 0, 0));
		contentPane.add(btnExit);

		JButton btnReset = new JButton("\u0418\u0437\u0447\u0438\u0441\u0442\u0438");
		sl_contentPane.putConstraint(SpringLayout.SOUTH, btnReset, -10, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, btnExit, 21, SpringLayout.EAST, btnReset);
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtInitialCapital.setText("");
				txtMonths.setText("");
				txaResult.setText("");
			}
		});
		btnReset.setForeground(new Color(0, 0, 0));
		contentPane.add(btnReset);

		JButton btnCalculate = new JButton("\u041F\u0440\u0435\u0441\u043C\u0435\u0442\u043D\u0438");
		sl_contentPane.putConstraint(SpringLayout.WEST, btnCalculate, 21, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, btnCalculate, -10, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, btnReset, 21, SpringLayout.EAST, btnCalculate);
		btnCalculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int endMonth = Integer.parseInt(txtMonths.getText());
				double capital = Double.parseDouble(txtInitialCapital.getText());

				final double PROFIT_PERSENTAGE = 30.0;
				final double SAVINGS_PART = 1.0 / 2.0;

				double savings = 0; // спестявания общо
				double monthlyProfit; // месечна печалба
				double monthlySavings; // месечни спестявания

				String result = "Месец    Капитал      Спестявания\n";

				for (int month = 1; month <= endMonth; month++) {
					// Изчислява се печалбата
					monthlyProfit = capital * PROFIT_PERSENTAGE / 100;

					// Заделя се част за спестявания
					monthlySavings = monthlyProfit * SAVINGS_PART;

					// Капиталът и спестяванията се актуализират
					capital += monthlyProfit - monthlySavings;
					savings += monthlySavings;

					result += String.format("%5d%15.2f лв.%10.2f лв.\n", month, capital, savings);
				}

				txaResult.setText(result);
			}
		});
		btnCalculate.setForeground(new Color(0, 0, 0));
		contentPane.add(btnCalculate);

	}
}
