package edu.school.budget;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ListModel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.AbstractListModel;
import java.awt.Component;
/**
 * Информатика, 8 клас, издателство Просвета, 2017
 * Автори: Светла Бойчева, Николина Николова, Елиза Стефанова, Антон Денев
 * Урок 3.4.1.3. Списъчно поле
 * Задача:Като всички хора, RxC001 също плаща данъци
 * и сметки за комунални услуги, и всеки месец се
 * чуди дали спестяванията ще му стигнат да ги покрие.
 * Създайте програма BUDGET, която, по зададен 
 * списък с разходите за битови нужди, пресмята
 * общата дължима от RxC001 сума.
 * 
 * Вариант 1.
 */

public class BudgetFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;

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

	/**
	 * Create the frame.
	 */
	public BudgetFrame() {
		setForeground(new Color(51, 102, 102));
		setTitle("\u0411\u044E\u0434\u0436\u0435\u0442");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 356, 300);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(255, 255, 204));
		contentPane.setForeground(new Color(51, 102, 102));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[grow][grow]", "[][grow][][]"));
		
		JLabel lblService = new JLabel("\u0423\u0441\u043B\u0443\u0433\u0430 / \u0434\u0430\u043D\u044A\u043A");
		lblService.setForeground(new Color(51, 102, 102));
		lblService.setFont(new Font("Tahoma", Font.BOLD, 12));
		contentPane.add(lblService, "cell 0 0");
		
		JLabel lblPrice = new JLabel("\u0422\u0430\u043A\u0441\u0430");
		lblPrice.setForeground(new Color(51, 102, 102));
		lblPrice.setFont(new Font("Tahoma", Font.BOLD, 12));
		contentPane.add(lblPrice, "cell 1 0");
		
		JList<String> lstServices = new JList<String>();
		lstServices.setVisibleRowCount(6);
		lstServices.setModel(new AbstractListModel<String>() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			String[] values = new String[] {"\u0414\u0430\u043D\u044A\u043A \u0441\u0433\u0440\u0430\u0434\u0438", "\u0415\u043B\u0435\u043A\u0442\u0440\u0438\u0447\u0435\u0441\u0442\u0432\u043E", "\u0412\u043E\u0434\u0430", "\u0422\u0435\u043B\u0435\u0444\u043E\u043D", "\u0418\u043D\u0442\u0435\u0440\u043D\u0435\u0442"};
			public int getSize() {
				return values.length;
			}
			public String getElementAt(int index) {
				return values[index];
			}
		});
		lstServices.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		lstServices.setForeground(new Color(51, 102, 102));
		lstServices.setFont(new Font("Tahoma", Font.PLAIN, 12));
		contentPane.add(lstServices, "flowy,cell 0 1,grow");
		
		JList<String> lstPrices = new JList<String>();
		lstPrices.setVisibleRowCount(6);
		lstPrices.setAlignmentX(Component.RIGHT_ALIGNMENT);
		lstPrices.setModel(new AbstractListModel<String>() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			String[] values = new String[] {"160.85", "133.12", "7.30", "17.25", "25.88"};
			public int getSize() {
				return values.length;
			}
			public String getElementAt(int index) {
				return values[index];
			}
		});
		lstPrices.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		lstPrices.setForeground(new Color(51, 102, 102));
		lstPrices.setFont(new Font("Tahoma", Font.PLAIN, 12));
		contentPane.add(lstPrices, "cell 1 1,grow");
		
		JLabel lblTextTotal = new JLabel("\u041E\u0431\u0449\u043E:");
		lblTextTotal.setForeground(new Color(51, 102, 102));
		lblTextTotal.setFont(new Font("Tahoma", Font.BOLD, 12));
		contentPane.add(lblTextTotal, "cell 0 2,alignx right");
		
		JLabel lblTotal = new JLabel("");
		lblTotal.setForeground(new Color(51, 102, 102));
		lblTotal.setFont(new Font("Tahoma", Font.BOLD, 12));
		contentPane.add(lblTotal, "cell 1 2");
		
		JButton btnCalculate = new JButton("\u041F\u0440\u0435\u0441\u043C\u0435\u0442\u043D\u0438");
		btnCalculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//създаване на модел за списъчното поле
				// за вземане на стойностите 
				ListModel< String> model = lstPrices.getModel();
				int size = model.getSize();
				
				//последователно обхождане на елементите на списъчното поле
				//и натрупване на сумата от таксите в total
				double total = 0;
				for (int i =0; i< size; i++){
					total += Double.parseDouble(model.getElementAt(i));
				}
				
				// извеждане на форматиран резултат с точност до втория знак след десетичната запетая
				String result = String.format("%.2f", total);
				lblTotal.setText(result);
			}
		});
		

		btnCalculate.setForeground(UIManager.getColor("Button.foreground"));
		btnCalculate.setBackground(UIManager.getColor("Button.background"));
		contentPane.add(btnCalculate, "cell 0 3,growx");
		
		JButton btnExit = new JButton("\u0418\u0437\u0445\u043E\u0434");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		btnExit.setBackground(UIManager.getColor("Button.background"));
		contentPane.add(btnExit, "cell 1 3,growx");
	}

}
