package lt.ktu.gmj.tasks;

import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.StringTokenizer;
import javax.swing.*;
import javax.swing.text.JTextComponent;
import lt.ktu.gmj.Result;
import lt.ktu.gmj.ResultRepository;
import lt.ktu.gmj.analysis.*;
import lt.ktu.gmj.tasks.*;
import javax.swing.table.*;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.text.*;

public class KnapsackAnalyzer extends WindowAdapter
	implements ResultAnalyzer
{
	JFrame frame;
	JTable table;
	JPanel panel;
	public void prepare(final ResultRepository results)
	{
		if(results.resultCount() <= 0)
		{
			JOptionPane.showMessageDialog(null, "No results available");
		}
		else if(!(results.resultAt(0).value instanceof KnapsackResult))
		{
			JOptionPane.showMessageDialog(null, "No information about knapsack composition found in the results");
		}
		else {
			//pasirenkam reikalinga rezultata
			Result tmp = null;
			int count = results.resultCount();
			for ( int i = 0 ; i < count ; i++ )
				if ( tmp == null || tmp.value.fx() > results.resultAt(i).value.fx() ||
					(results.resultAt(i).value.fx() == tmp.value.fx()) && ((KnapsackResult)(results.resultAt(i).value)).totalWeight() > ((KnapsackResult)(tmp.value)).totalWeight() )
					tmp = results.resultAt(i);
			final KnapsackResult best = (KnapsackResult)(tmp.value);
			frame = new JFrame();
			frame.setTitle("Knapsack results");
			frame.addWindowListener(this);
			frame.setSize(600, 300);
			frame.setDefaultCloseOperation(frame.DO_NOTHING_ON_CLOSE);
			
			TableModel dm = new AbstractTableModel() {
				public int getColumnCount() { return 5; }
				public int getRowCount() {
					return best.items().size()+1;
				}
				public Object getValueAt(int row, int col) {
					Object val = null;
					try {
						if( row == best.items().size() )
						{
							DecimalFormat fmt = new DecimalFormat(".00");
							switch(col)
							{
								case 0: return "Viso";
								case 1: return "";
								case 2: return fmt.format(best.totalCost());
								case 3: return fmt.format(best.totalWeight());
								case 4: return "";
							}
						}
						else
						{
							Item item = best.items().get(row);
							switch(col)
							{
								case 0:
									val = item.name();
									break;
								case 1:
									val = item.amount();
									break;
								case 2:
									val = item.price();
									break;
								case 3:
									val = item.weight();
									break;
								case 4:
									DecimalFormat fmt = new DecimalFormat(".00");
									val = fmt.format(item.ratio());
									break;
								default:
									val = "???";
							}
							return val;
						}
					}
					catch(Exception e)
					{
						//JOptionPane.showMessageDialog(null, "exception while getting table cell");
						//return e.toString();
						return "ex";
					}
					return "err";
				}
				public String getColumnName(int col) {
					String[] names = new String[]{"Pavadinimas", "Kiekis", "Kaina", "Svoris", "Kaina / Svoris"};
        				return names[col].toString();
    				}
			};
			table = new JTable(dm);
			//table.setSize(400,300);
			JScrollPane scrollPane = new JScrollPane(table);
			table.setPreferredScrollableViewportSize(new Dimension(600, 300));
			panel = new JPanel(new BorderLayout());
			panel.setOpaque(true);
			panel.add(scrollPane);
			
			frame.setContentPane(panel);
			frame.pack();
			frame.setLocationRelativeTo(null);
			frame.setVisible(true);
			//frame.show();
		}
	}
	public void dispose()
	{
	}
	public void windowClosing(WindowEvent e)
	{
		frame.dispose();
	}
} 